From 8c9919ec7b409d11ca73ca5764f44282dec0fe25 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 30 Nov 2020 17:46:10 -0700 Subject: [PATCH] fix regression on wasm targets The previous commit broke wasm targets because the linking step would look for the compiler-rt lib in the wrong place. Fixed in this commit. --- src/Compilation.zig | 10 +++++++--- src/link/Wasm.zig | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index b7660718de..df48e5a391 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -981,16 +981,20 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { // Once it is capable this condition should be removed. if (build_options.is_stage1) { if (comp.bin_file.options.include_compiler_rt) { - if (is_exe_or_dyn_lib) { + if (is_exe_or_dyn_lib or comp.getTarget().isWasm()) { try comp.work_queue.writeItem(.{ .compiler_rt_lib = {} }); } else { try comp.work_queue.writeItem(.{ .compiler_rt_obj = {} }); - if (comp.bin_file.options.object_format != .elf) { + if (comp.bin_file.options.object_format != .elf and + comp.bin_file.options.output_mode == .Obj) + { // For ELF we can rely on using -r to link multiple objects together into one, // but to truly support `build-obj -fcompiler-rt` will require virtually // injecting `_ = @import("compiler_rt.zig")` into the root source file of // the compilation. - fatal("Embedding compiler-rt into non-ELF objects is not yet implemented.", .{}); + fatal("Embedding compiler-rt into {s} objects is not yet implemented.", .{ + @tagName(comp.bin_file.options.object_format), + }); } } } diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 580a5df576..51535d47ed 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -282,6 +282,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { break :blk full_obj_path; } else null; + const compiler_rt_path: ?[]const u8 = if (self.base.options.include_compiler_rt) + comp.compiler_rt_static_lib.?.full_object_path + else + null; + const target = self.base.options.target; const id_symlink_basename = "lld.id"; @@ -302,6 +307,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { _ = try man.addFile(entry.key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); + try man.addOptionalFile(compiler_rt_path); man.hash.addOptional(self.base.options.stack_size_override); man.hash.addListOfBytes(self.base.options.extra_lld_args); @@ -381,11 +387,15 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { try argv.append(p); } - if (self.base.options.output_mode != .Obj and !self.base.options.is_compiler_rt_or_libc) { - if (!self.base.options.link_libc) { - try argv.append(comp.libc_static_lib.?.full_object_path); - } - try argv.append(comp.compiler_rt_static_lib.?.full_object_path); + if (self.base.options.output_mode != .Obj and + !self.base.options.is_compiler_rt_or_libc and + !self.base.options.link_libc) + { + try argv.append(comp.libc_static_lib.?.full_object_path); + } + + if (compiler_rt_path) |p| { + try argv.append(p); } if (self.base.options.verbose_link) {