2016-02-28 13:06:46 +08:00
|
|
|
// This file is in a package which has the root source file exposed as "@root".
|
2017-03-22 23:26:30 +08:00
|
|
|
// It is included in the compilation unit when exporting an executable.
|
2015-12-11 06:34:38 +08:00
|
|
|
|
2016-02-28 13:06:46 +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
|
|
|
|
2017-12-18 22:59:57 +08:00
|
|
|
comptime {
|
2017-12-19 15:39:43 +08:00
|
|
|
const strong_linkage = builtin.GlobalLinkage.Strong;
|
2017-12-18 22:59:57 +08:00
|
|
|
if (builtin.link_libc) {
|
2017-12-19 15:39:43 +08:00
|
|
|
@export("main", main, strong_linkage);
|
2017-12-18 22:59:57 +08:00
|
|
|
} else if (builtin.os == builtin.Os.windows) {
|
2017-12-19 15:39:43 +08:00
|
|
|
@export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
|
2017-12-18 22:59:57 +08:00
|
|
|
} else {
|
2017-12-19 15:39:43 +08:00
|
|
|
@export("_start", _start, strong_linkage);
|
2017-03-23 14:59:58 +08:00
|
|
|
}
|
2017-12-18 22:59:57 +08:00
|
|
|
}
|
2016-09-28 14:33:32 +08:00
|
|
|
|
2018-01-25 17:10:11 +08:00
|
|
|
nakedcc fn _start() noreturn {
|
2017-05-02 01:12:38 +08:00
|
|
|
switch (builtin.arch) {
|
2018-12-18 10:19:20 +08:00
|
|
|
builtin.Arch.x86_64 => {
|
|
|
|
argc_ptr = asm ("lea (%%rsp), %[argc]"
|
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
2016-02-12 17:04:46 +08:00
|
|
|
},
|
2017-05-02 01:12:38 +08:00
|
|
|
builtin.Arch.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
|
|
|
},
|
2018-08-30 08:37:58 +08:00
|
|
|
builtin.Arch.aarch64v8 => {
|
|
|
|
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) {
|
|
|
|
_ = @import("../os/windows/tls.zig");
|
|
|
|
}
|
2018-01-15 13:01:02 +08:00
|
|
|
std.os.windows.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];
|
2018-04-23 06:11:50 +08:00
|
|
|
if (builtin.os == builtin.Os.linux) {
|
2019-02-07 02:48:04 +08:00
|
|
|
// Scan auxiliary vector.
|
|
|
|
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
|
2019-02-07 03:32:20 +08:00
|
|
|
std.os.linux_elf_aux_maybe = auxv;
|
2019-02-07 02:48:04 +08:00
|
|
|
var i: usize = 0;
|
|
|
|
var at_phdr: usize = 0;
|
|
|
|
var at_phnum: usize = 0;
|
|
|
|
var at_phent: usize = 0;
|
|
|
|
while (auxv[i].a_un.a_val != 0) : (i += 1) {
|
|
|
|
switch (auxv[i].a_type) {
|
|
|
|
std.elf.AT_PAGESZ => assert(auxv[i].a_un.a_val == std.os.page_size),
|
|
|
|
std.elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
|
|
|
|
std.elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
|
|
|
|
std.elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!builtin.single_threaded) linuxInitializeThreadLocalStorage(at_phdr, at_phnum, at_phent);
|
2018-04-23 06:11:50 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 13:01:02 +08:00
|
|
|
std.os.posix.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-02-07 03:32:20 +08:00
|
|
|
std.os.ArgIteratorPosix.raw = argv[0..argc];
|
|
|
|
std.os.posix_environ_raw = envp;
|
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)) {
|
|
|
|
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-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();
|
|
|
|
},
|
|
|
|
builtin.TypeId.ErrorUnion => {
|
|
|
|
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| {
|
|
|
|
std.debug.dumpStackTrace(trace);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
var tls_end_addr: usize = undefined;
|
|
|
|
const main_thread_tls_align = 32;
|
|
|
|
var main_thread_tls_bytes: [64]u8 align(main_thread_tls_align) = [1]u8{0} ** 64;
|
|
|
|
|
|
|
|
fn linuxInitializeThreadLocalStorage(at_phdr: usize, at_phnum: usize, at_phent: usize) void {
|
|
|
|
var phdr_addr = at_phdr;
|
|
|
|
var n = at_phnum;
|
|
|
|
var base: usize = 0;
|
|
|
|
while (n != 0) : ({n -= 1; phdr_addr += at_phent;}) {
|
|
|
|
const phdr = @intToPtr(*std.elf.Phdr, phdr_addr);
|
|
|
|
// TODO look for PT_DYNAMIC when we have https://github.com/ziglang/zig/issues/1917
|
|
|
|
switch (phdr.p_type) {
|
|
|
|
std.elf.PT_PHDR => base = at_phdr - phdr.p_vaddr,
|
2019-02-07 03:32:20 +08:00
|
|
|
std.elf.PT_TLS => std.os.linux_tls_phdr = phdr,
|
2019-02-07 02:48:04 +08:00
|
|
|
else => continue,
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 03:32:20 +08:00
|
|
|
const tls_phdr = std.os.linux_tls_phdr orelse return;
|
|
|
|
std.os.linux_tls_img_src = @intToPtr([*]const u8, base + tls_phdr.p_vaddr);
|
2019-02-07 02:48:04 +08:00
|
|
|
assert(main_thread_tls_bytes.len >= tls_phdr.p_memsz); // not enough preallocated Thread Local Storage
|
|
|
|
assert(main_thread_tls_align >= tls_phdr.p_align); // preallocated Thread Local Storage not aligned enough
|
2019-02-07 03:32:20 +08:00
|
|
|
@memcpy(&main_thread_tls_bytes, std.os.linux_tls_img_src, tls_phdr.p_filesz);
|
2019-02-07 02:48:04 +08:00
|
|
|
tls_end_addr = @ptrToInt(&main_thread_tls_bytes) + tls_phdr.p_memsz;
|
|
|
|
linuxSetThreadArea(@ptrToInt(&tls_end_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn linuxSetThreadArea(addr: usize) void {
|
|
|
|
switch (builtin.arch) {
|
|
|
|
builtin.Arch.x86_64 => {
|
|
|
|
const ARCH_SET_FS = 0x1002;
|
|
|
|
const rc = std.os.linux.syscall2(std.os.linux.SYS_arch_prctl, ARCH_SET_FS, addr);
|
|
|
|
// acrh_prctl is documented to never fail
|
|
|
|
assert(rc == 0);
|
|
|
|
},
|
|
|
|
else => @compileError("Unsupported architecture"),
|
|
|
|
}
|
|
|
|
}
|