Commit b37acc4d68

Christine Dodrill <me@christine.website>
2019-12-12 02:31:32
allow custom OS entrypoint
Also: * Expose `std.start.callMain`. * Other fixes added to fix issues found in development.
1 parent 81f1f72
Changed files (6)
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) {}
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();
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) {
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;
lib/std/special.zig
@@ -0,0 +1,1 @@
+pub const start = @import("special/start.zig");
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());