authorgravatar for me@christine.websiteChristine Dodrill <me@christine.website> 2019-12-12 01:31:32+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-12 16:00:23-05:00
logb37acc4d6870a090c3501d81d3f647bc30220e4b
tree5b906c0cc4c9bb358d64324b25492a4b6cabd48a
parent81f1f72197113a45e827d5c984e219a28aa28083
signature Commit is signed but in an unrecognized format.

allow custom OS entrypoint

Also: * Expose `std.start.callMain`. * Other fixes added to fix issues found in development.

6 files changed, 25 insertions(+), 18 deletions(-)

lib/std/builtin.zig+4
......@@ -424,6 +424,10 @@ pub const panic: PanicFn = if (@hasDecl(root, "panic")) root.panic else default_
424424/// therefore must be kept in sync with the compiler implementation.
425425pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn {
426426 @setCold(true);
427 if (@hasDecl(root, "os") and @hasDecl(root.os, "panic")) {
428 root.os.panic(msg, error_return_trace);
429 unreachable;
430 }
427431 switch (os) {
428432 .freestanding => {
429433 while (true) {
lib/std/os.zig+12-8
......@@ -187,19 +187,23 @@ pub fn abort() noreturn {
187187 }
188188 windows.kernel32.ExitProcess(3);
189189 }
190 if (builtin.link_libc) {
191 system.abort();
190 if (!builtin.link_libc and builtin.os == .linux) {
191 raise(SIGABRT) catch {};
192
193 // TODO the rest of the implementation of abort() from musl libc here
194
195 raise(SIGKILL) catch {};
196 exit(127);
192197 }
193198 if (builtin.os == .uefi) {
194199 exit(0); // TODO choose appropriate exit code
195200 }
201 if (builtin.os == .wasi) {
202 @breakpoint();
203 exit(1);
204 }
196205
197 raise(SIGABRT) catch {};
198
199 // TODO the rest of the implementation of abort() from musl libc here
200
201 raise(SIGKILL) catch {};
202 exit(127);
206 system.abort();
203207}
204208
205209pub const RaiseError = UnexpectedError;
lib/std/special.zig created+1
......@@ -0,0 +1 @@
1pub const start = @import("special/start.zig");
lib/std/special/c.zig+1-1
......@@ -83,7 +83,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
8383 @setCold(true);
8484 std.debug.panic("{}", msg);
8585 }
86 if (builtin.os != .freestanding) {
86 if (builtin.os != .freestanding and builtin.os != .other) {
8787 std.os.abort();
8888 }
8989 while (true) {}
lib/std/special/start.zig+6-9
......@@ -17,6 +17,7 @@ const is_mips = switch (builtin.arch) {
1717 .mips, .mipsel, .mips64, .mips64el => true,
1818 else => false,
1919};
20const start_sym_name = if (is_mips) "__start" else "_start";
2021
2122comptime {
2223 if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) {
......@@ -34,14 +35,10 @@ comptime {
3435 }
3536 } else if (builtin.os == .uefi) {
3637 if (!@hasDecl(root, "EfiMain")) @export("EfiMain", EfiMain, .Strong);
37 } else if (builtin.os != .freestanding) {
38 if (is_mips) {
39 if (!@hasDecl(root, "__start")) @export("__start", _start, .Strong);
40 } else {
41 if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong);
42 }
43 } else if (is_wasm) {
44 if (!@hasDecl(root, "_start")) @export("_start", wasm_freestanding_start, .Strong);
38 } else if (is_wasm and builtin.os == .freestanding) {
39 if (!@hasDecl(root, start_sym_name)) @export(start_sym_name, wasm_freestanding_start, .Strong);
40 } else if (builtin.os != .other and builtin.os != .freestanding) {
41 if (!@hasDecl(root, start_sym_name)) @export(start_sym_name, _start, .Strong);
4542 }
4643 }
4744}
......@@ -247,7 +244,7 @@ async fn callMainAsync(loop: *std.event.Loop) u8 {
247244
248245// This is not marked inline because it is called with @asyncCall when
249246// there is an event loop.
250fn callMain() u8 {
247pub fn callMain() u8 {
251248 switch (@typeInfo(@TypeOf(root.main).ReturnType)) {
252249 .NoReturn => {
253250 root.main();
lib/std/std.zig+1
......@@ -65,6 +65,7 @@ pub const time = @import("time.zig");
6565pub const unicode = @import("unicode.zig");
6666pub const valgrind = @import("valgrind.zig");
6767pub const zig = @import("zig.zig");
68pub const special = @import("special.zig");
6869
6970test "" {
7071 meta.refAllDecls(@This());