authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 11:15:12+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 11:15:12+02:00
log5fa5fba3ce57b128b37b371eb3daa8ca39221613
tree177ff2d1a4a011a47b320f6726bf9ddfd47a9005
parenta6ae5a77dac6466617cdf57357aa79c2b79403b3
parent9f8de83d931f1f59ffa7010afc52a1bd54778447

Merge branch 'mathetake-cc-wasi-execmodel'


7 files changed, 49 insertions(+), 9 deletions(-)

src/Compilation.zig+5-3
......@@ -725,6 +725,8 @@ pub const InitOptions = struct {
725725 test_filter: ?[]const u8 = null,
726726 test_name_prefix: ?[]const u8 = null,
727727 subsystem: ?std.Target.SubSystem = null,
728 /// WASI-only. Type of WASI execution model ("command" or "reactor").
729 wasi_exec_model: ?wasi_libc.CRTFile = null,
728730};
729731
730732fn addPackageTableToCacheHash(
......@@ -1340,6 +1342,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
13401342 .disable_lld_caching = options.disable_lld_caching,
13411343 .subsystem = options.subsystem,
13421344 .is_test = options.is_test,
1345 .wasi_exec_model = options.wasi_exec_model,
13431346 });
13441347 errdefer bin_file.destroy();
13451348 comp.* = .{
......@@ -1441,9 +1444,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14411444 .wasi_libc_crt_file = crt_file,
14421445 });
14431446 }
1444 // TODO add logic deciding which crt1 we want here.
14451447 comp.work_queue.writeAssumeCapacity(&[_]Job{
1446 .{ .wasi_libc_crt_file = .crt1_o },
1448 .{ .wasi_libc_crt_file = comp.bin_file.options.wasi_exec_model orelse .crt1_o },
14471449 .{ .wasi_libc_crt_file = .libc_a },
14481450 });
14491451 }
......@@ -1868,7 +1870,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
18681870
18691871 for (keys[1..]) |key, i| {
18701872 err_msg.notes[i] = .{
1871 .src_loc = key.nodeOffsetSrcLoc(values[i+1]),
1873 .src_loc = key.nodeOffsetSrcLoc(values[i + 1]),
18721874 .msg = "also here",
18731875 };
18741876 }
src/clang_options_data.zig+8-1
......@@ -5257,7 +5257,14 @@ jspd1("fxray-modes="),
52575257 .psl = false,
52585258},
52595259jspd1("iwithsysroot"),
5260joinpd1("mexec-model="),
5260.{
5261 .name = "mexec-model=",
5262 .syntax = .joined,
5263 .zig_equivalent = .exec_model,
5264 .pd1 = true,
5265 .pd2 = false,
5266 .psl = false,
5267},
52615268joinpd1("mharden-sls="),
52625269joinpd1("mhvx-length="),
52635270jspd1("objc-isystem"),
src/link.zig+3
......@@ -118,6 +118,9 @@ pub const Options = struct {
118118 version: ?std.builtin.Version,
119119 libc_installation: ?*const LibCInstallation,
120120
121 /// WASI-only. Type of WASI execution model ("command" or "reactor").
122 wasi_exec_model: ?wasi_libc.CRTFile = null,
123
121124 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
122125 return if (options.use_lld) .Obj else options.output_mode;
123126 }
src/link/Wasm.zig+10-5
......@@ -681,6 +681,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
681681 // Put stack before globals so that stack overflow results in segfault immediately
682682 // before corrupting globals. See https://github.com/ziglang/zig/issues/4496
683683 try argv.append("--stack-first");
684
685 // Reactor execution model does not have _start so lld doesn't look for it.
686 if (self.base.options.wasi_exec_model) |exec_model| blk: {
687 if (exec_model != .crt1_reactor_o) break :blk;
688 try argv.append("--no-entry");
689 }
684690 } else {
685691 try argv.append("--no-entry"); // So lld doesn't look for _start.
686692 try argv.append("--export-all");
......@@ -692,11 +698,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
692698 });
693699
694700 if (target.os.tag == .wasi) {
695 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
696 // TODO work out if we want standard crt, a reactor or a command
697 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
698 }
699
700701 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
701702 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
702703 if (is_exe_or_dyn_lib) {
......@@ -720,6 +721,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
720721 }
721722
722723 if (self.base.options.link_libc) {
724 try argv.append(try comp.get_libc_crt_file(
725 arena,
726 wasi_libc.crtFileFullName(self.base.options.wasi_exec_model orelse .crt1_o),
727 ));
723728 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));
724729 }
725730 }
src/main.zig+10
......@@ -613,6 +613,7 @@ fn buildOutputType(
613613 var subsystem: ?std.Target.SubSystem = null;
614614 var major_subsystem_version: ?u32 = null;
615615 var minor_subsystem_version: ?u32 = null;
616 var wasi_exec_model: ?wasi_libc.CRTFile = null;
616617
617618 var system_libs = std.ArrayList([]const u8).init(gpa);
618619 defer system_libs.deinit();
......@@ -1254,6 +1255,13 @@ fn buildOutputType(
12541255 .framework => try frameworks.append(it.only_arg),
12551256 .nostdlibinc => want_native_include_dirs = false,
12561257 .strip => strip = true,
1258 .exec_model => {
1259 if (std.mem.eql(u8, it.only_arg, "reactor")) {
1260 wasi_exec_model = .crt1_reactor_o;
1261 } else if (std.mem.eql(u8, it.only_arg, "command")) {
1262 wasi_exec_model = .crt1_command_o;
1263 }
1264 },
12571265 }
12581266 }
12591267 // Parse linker args.
......@@ -1969,6 +1977,7 @@ fn buildOutputType(
19691977 .test_name_prefix = test_name_prefix,
19701978 .disable_lld_caching = !have_enable_cache,
19711979 .subsystem = subsystem,
1980 .wasi_exec_model = wasi_exec_model,
19721981 }) catch |err| {
19731982 fatal("unable to create compilation: {s}", .{@errorName(err)});
19741983 };
......@@ -3341,6 +3350,7 @@ pub const ClangArgIterator = struct {
33413350 red_zone,
33423351 no_red_zone,
33433352 strip,
3353 exec_model,
33443354 };
33453355
33463356 const Args = struct {
src/wasi_libc.zig+9
......@@ -45,6 +45,15 @@ pub fn emulatedLibCRFileLibName(crt_file: CRTFile) []const u8 {
4545 };
4646}
4747
48pub fn crtFileFullName(crt_file: CRTFile) []const u8 {
49 return switch (crt_file) {
50 .crt1_o => "crt1.o",
51 .crt1_reactor_o => "crt1-reactor.o",
52 .crt1_command_o => "crt1-command.o",
53 else => unreachable,
54 };
55}
56
4857pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
4958 if (!build_options.have_llvm) {
5059 return error.ZigCompilerNotBuiltWithLLVMExtensions;
tools/update_clang_options.zig+4
......@@ -336,6 +336,10 @@ const known_options = [_]KnownOpt{
336336 .name = "dynamiclib",
337337 .ident = "shared",
338338 },
339 .{
340 .name = "mexec-model",
341 .ident = "exec_model",
342 }
339343};
340344
341345const blacklisted_options = [_][]const u8{};