diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 36fa46e953..5b7051eafb 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -424,6 +424,10 @@ pub const panic: PanicFn = if (@hasDecl(root, "panic")) root.panic else default_ /// therefore must be kept in sync with the compiler implementation. pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn { @setCold(true); + if (@hasDecl(root, "os") and @hasDecl(root.os, "panic")) { + root.os.panic(msg, error_return_trace); + unreachable; + } switch (os) { .freestanding => { while (true) { diff --git a/lib/std/os.zig b/lib/std/os.zig index 04de59d6e1..6d49bcd38e 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -187,19 +187,23 @@ pub fn abort() noreturn { } windows.kernel32.ExitProcess(3); } - if (builtin.link_libc) { - system.abort(); + if (!builtin.link_libc and builtin.os == .linux) { + raise(SIGABRT) catch {}; + + // TODO the rest of the implementation of abort() from musl libc here + + raise(SIGKILL) catch {}; + exit(127); } if (builtin.os == .uefi) { exit(0); // TODO choose appropriate exit code } + if (builtin.os == .wasi) { + @breakpoint(); + exit(1); + } - raise(SIGABRT) catch {}; - - // TODO the rest of the implementation of abort() from musl libc here - - raise(SIGKILL) catch {}; - exit(127); + system.abort(); } pub const RaiseError = UnexpectedError; diff --git a/lib/std/special.zig b/lib/std/special.zig new file mode 100644 index 0000000000..03012d5a1c --- /dev/null +++ b/lib/std/special.zig @@ -0,0 +1 @@ +pub const start = @import("special/start.zig"); diff --git a/lib/std/special/c.zig b/lib/std/special/c.zig index 4c2eb2a958..0895b1e6f9 100644 --- a/lib/std/special/c.zig +++ b/lib/std/special/c.zig @@ -83,7 +83,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn @setCold(true); std.debug.panic("{}", msg); } - if (builtin.os != .freestanding) { + if (builtin.os != .freestanding and builtin.os != .other) { std.os.abort(); } while (true) {} diff --git a/lib/std/special/start.zig b/lib/std/special/start.zig index 60745dab7f..ee53254098 100644 --- a/lib/std/special/start.zig +++ b/lib/std/special/start.zig @@ -17,6 +17,7 @@ const is_mips = switch (builtin.arch) { .mips, .mipsel, .mips64, .mips64el => true, else => false, }; +const start_sym_name = if (is_mips) "__start" else "_start"; comptime { if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) { @@ -34,14 +35,10 @@ comptime { } } else if (builtin.os == .uefi) { if (!@hasDecl(root, "EfiMain")) @export("EfiMain", EfiMain, .Strong); - } else if (builtin.os != .freestanding) { - if (is_mips) { - if (!@hasDecl(root, "__start")) @export("__start", _start, .Strong); - } else { - if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong); - } - } else if (is_wasm) { - if (!@hasDecl(root, "_start")) @export("_start", wasm_freestanding_start, .Strong); + } else if (is_wasm and builtin.os == .freestanding) { + if (!@hasDecl(root, start_sym_name)) @export(start_sym_name, wasm_freestanding_start, .Strong); + } else if (builtin.os != .other and builtin.os != .freestanding) { + if (!@hasDecl(root, start_sym_name)) @export(start_sym_name, _start, .Strong); } } } @@ -247,7 +244,7 @@ async fn callMainAsync(loop: *std.event.Loop) u8 { // This is not marked inline because it is called with @asyncCall when // there is an event loop. -fn callMain() u8 { +pub fn callMain() u8 { switch (@typeInfo(@TypeOf(root.main).ReturnType)) { .NoReturn => { root.main(); diff --git a/lib/std/std.zig b/lib/std/std.zig index 09db489604..48b3c27987 100644 --- a/lib/std/std.zig +++ b/lib/std/std.zig @@ -65,6 +65,7 @@ pub const time = @import("time.zig"); pub const unicode = @import("unicode.zig"); pub const valgrind = @import("valgrind.zig"); pub const zig = @import("zig.zig"); +pub const special = @import("special.zig"); test "" { meta.refAllDecls(@This());