zig/std/bootstrap.zig
Andrew Kelley 07a71fc322 improved behavior on debug safety crash
* instead of emitting a breakpoint for a debug safety crash,
   zig calls a panic function which prints an error message
   and a stack trace and then calls abort.
 * on freestanding OS, this panic function has a default
   implementation of a simple infinite loop.
 * users can override the panic implementation by providing
   `pub fn panic(message: []const u8) -> unreachable { }`
 * workaround for LLVM segfaulting when you try to use cold
   calling convention on ARM.

closes #245
2017-02-06 03:10:32 -05:00

56 lines
1.4 KiB
Zig

// This file is in a package which has the root source file exposed as "@root".
const root = @import("@root");
const std = @import("std");
const linux = std.linux;
const cstr = std.cstr;
const want_start_symbol = switch(@compileVar("os")) {
Os.linux => true,
else => false,
};
const want_main_symbol = !want_start_symbol;
var argc: usize = undefined;
var argv: &&u8 = undefined;
export nakedcc fn _start() -> unreachable {
@setFnVisible(this, want_start_symbol);
switch (@compileVar("arch")) {
Arch.x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
},
Arch.i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
},
else => @compileError("unsupported arch"),
}
callMainAndExit()
}
fn callMain() -> %void {
const args = @alloca([]u8, argc);
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0...cstr.len(ptr)];
}
return root.main(args);
}
fn callMainAndExit() -> unreachable {
callMain() %% linux.exit(1);
linux.exit(0);
}
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
@setFnVisible(this, want_main_symbol);
argc = usize(c_argc);
argv = c_argv;
callMain() %% return 1;
return 0;
}