authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-07 17:03:29+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-09 01:25:38+02:00
logdd84ccda5daf63373b2b5df3ac02025017b7efbf
treec85dbd8048ffdac85962d4e8ac83f94ce2b94cc0
parent95745f77da6cd3ca30aea6aa47253c1e0a4f5a75

wasi: clean up linking logic

Do not try to link WASI libc or emulated subcomponents when not targeting WASI; e.g., when targeting `wasm32-freestanding`.

1 files changed, 20 insertions(+), 23 deletions(-)

src/link/Wasm.zig+20-23
...@@ -574,7 +574,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -574,7 +574,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
574 null;574 null;
575575
576 const target = self.base.options.target;576 const target = self.base.options.target;
577 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
578577
579 const id_symlink_basename = "lld.id";578 const id_symlink_basename = "lld.id";
580579
...@@ -653,8 +652,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -653,8 +652,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
653 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});652 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
654 }653 }
655 } else {654 } else {
656 const is_obj = self.base.options.output_mode == .Obj;
657
658 // Create an LLD command line and invoke it.655 // Create an LLD command line and invoke it.
659 var argv = std.ArrayList([]const u8).init(self.base.allocator);656 var argv = std.ArrayList([]const u8).init(self.base.allocator);
660 defer argv.deinit();657 defer argv.deinit();
...@@ -662,10 +659,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -662,10 +659,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
662 // This is necessary because LLD does not behave properly as a library -659 // This is necessary because LLD does not behave properly as a library -
663 // it calls exit() and does not reset all global data between invocations.660 // it calls exit() and does not reset all global data between invocations.
664 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });661 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });
665 if (is_obj) {
666 try argv.append("-r");
667 }
668
669 try argv.append("-error-limit=0");662 try argv.append("-error-limit=0");
670663
671 if (self.base.options.lto) {664 if (self.base.options.lto) {
...@@ -697,25 +690,29 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -697,25 +690,29 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
697 full_out_path,690 full_out_path,
698 });691 });
699692
700 if (link_in_crt) {693 if (target.os.tag == .wasi) {
701 // TODO work out if we want standard crt, a reactor or a command694 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
702 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));695 // TODO work out if we want standard crt, a reactor or a command
703 }696 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
704
705 if (!is_obj) {
706 const system_libs = self.base.options.system_libs.keys();
707 for (system_libs) |link_lib| {
708 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
709 }697 }
710698
711 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;699 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
712 for (wasi_emulated_libs) |lib_name| {700 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
713 const full_lib_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name});701 if (is_exe_or_dyn_lib) {
714 try argv.append(try comp.get_libc_crt_file(arena, full_lib_name));702 const system_libs = self.base.options.system_libs.keys();
715 }703 for (system_libs) |link_lib| {
704 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{link_lib}));
705 }
716706
717 if (self.base.options.link_libc) {707 const wasi_emulated_libs = self.base.options.wasi_emulated_libs;
718 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));708 for (wasi_emulated_libs) |lib_name| {
709 const full_lib_name = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name});
710 try argv.append(try comp.get_libc_crt_file(arena, full_lib_name));
711 }
712
713 if (self.base.options.link_libc) {
714 try argv.append(try comp.get_libc_crt_file(arena, "libc.a"));
715 }
719 }716 }
720 }717 }
721718