allow custom OS entrypoint

Also:

 * Expose `std.start.callMain`.
 * Other fixes added to fix issues found in development.
master
Christine Dodrill 2019-12-12 01:31:32 +00:00 committed by Andrew Kelley
parent 81f1f72197
commit b37acc4d68
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
6 changed files with 25 additions and 18 deletions

View File

@ -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) {

View File

@ -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;

1
lib/std/special.zig Normal file
View File

@ -0,0 +1 @@
pub const start = @import("special/start.zig");

View File

@ -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) {}

View File

@ -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();

View File

@ -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());