From edd592d3710801d263ac2d82ef867db6214e4f47 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 12 Dec 2024 17:47:40 -0800 Subject: [PATCH] fix compilation when enabling llvm --- src/link.zig | 20 +++++++++++++++----- src/link/Coff.zig | 10 +++++++--- src/link/Elf.zig | 10 ++++++++-- src/link/Wasm.zig | 20 ++++++++++++-------- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/link.zig b/src/link.zig index eef6e82700..f3685a6b8b 100644 --- a/src/link.zig +++ b/src/link.zig @@ -1030,6 +1030,17 @@ pub const File = struct { const tracy = trace(@src()); defer tracy.end(); + const comp = base.comp; + const diags = &comp.link_diags; + + return linkAsArchiveInner(base, arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link as archive: {s}", .{@errorName(e)}), + }; + } + + fn linkAsArchiveInner(base: *File, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { const comp = base.comp; const directory = base.emit.root_dir; // Just an alias to make it shorter to type. @@ -1528,7 +1539,7 @@ pub fn spawnLld( const exit_code = try lldMain(arena, argv, false); if (exit_code == 0) return; if (comp.clang_passthrough_mode) std.process.exit(exit_code); - return error.LLDReportedFailure; + return error.LinkFailure; } var stderr: []u8 = &.{}; @@ -1605,17 +1616,16 @@ pub fn spawnLld( return error.UnableToSpawnSelf; }; + const diags = &comp.link_diags; switch (term) { .Exited => |code| if (code != 0) { if (comp.clang_passthrough_mode) std.process.exit(code); - const diags = &comp.link_diags; diags.lockAndParseLldStderr(argv[1], stderr); - return error.LLDReportedFailure; + return error.LinkFailure; }, else => { if (comp.clang_passthrough_mode) std.process.abort(); - log.err("{s} terminated with stderr:\n{s}", .{ argv[0], stderr }); - return error.LLDCrashed; + return diags.fail("{s} terminated with stderr:\n{s}", .{ argv[0], stderr }); }, } diff --git a/src/link/Coff.zig b/src/link/Coff.zig index b1dda455b7..9bd3bd76e9 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -1683,10 +1683,14 @@ fn resolveGlobalSymbol(coff: *Coff, current: SymbolWithLoc) !void { pub fn flush(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { const comp = coff.base.comp; const use_lld = build_options.have_llvm and comp.config.use_lld; - if (use_lld) { - return coff.linkWithLLD(arena, tid, prog_node); - } const diags = &comp.link_diags; + if (use_lld) { + return coff.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; + } switch (comp.config.output_mode) { .Exe, .Obj => return coff.flushModule(arena, tid, prog_node), .Lib => return diags.fail("writing lib files not yet implemented for COFF", .{}), diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 47145762de..cfd6bcd6c7 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -795,9 +795,15 @@ pub fn loadInput(self: *Elf, input: link.Input) !void { } pub fn flush(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { - const use_lld = build_options.have_llvm and self.base.comp.config.use_lld; + const comp = self.base.comp; + const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; if (use_lld) { - return self.linkWithLLD(arena, tid, prog_node); + return self.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; } try self.flushModule(arena, tid, prog_node); } diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index c175c624d4..6078b7c7d4 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -2134,9 +2134,14 @@ pub fn loadInput(wasm: *Wasm, input: link.Input) !void { pub fn flush(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { const comp = wasm.base.comp; const use_lld = build_options.have_llvm and comp.config.use_lld; + const diags = &comp.link_diags; if (use_lld) { - return wasm.linkWithLLD(arena, tid, prog_node); + return wasm.linkWithLLD(arena, tid, prog_node) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.LinkFailure => return error.LinkFailure, + else => |e| return diags.fail("failed to link with LLD: {s}", .{@errorName(e)}), + }; } return wasm.flushModule(arena, tid, prog_node); } @@ -2415,6 +2420,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: defer tracy.end(); const comp = wasm.base.comp; + const diags = &comp.link_diags; const shared_memory = comp.config.shared_memory; const export_memory = comp.config.export_memory; const import_memory = comp.config.import_memory; @@ -2468,7 +2474,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: } try man.addOptionalFile(module_obj_path); try man.addOptionalFilePath(compiler_rt_path); - man.hash.addOptionalBytes(wasm.optionalStringSlice(wasm.entry_name)); + man.hash.addOptionalBytes(wasm.entry_name.slice(wasm)); man.hash.add(wasm.base.stack_size); man.hash.add(wasm.base.build_id); man.hash.add(import_memory); @@ -2617,7 +2623,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: try argv.append("--export-dynamic"); } - if (wasm.optionalStringSlice(wasm.entry_name)) |entry_name| { + if (wasm.entry_name.slice(wasm)) |entry_name| { try argv.appendSlice(&.{ "--entry", entry_name }); } else { try argv.append("--no-entry"); @@ -2759,14 +2765,12 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: switch (term) { .Exited => |code| { if (code != 0) { - const diags = &comp.link_diags; diags.lockAndParseLldStderr(linker_command, stderr); - return error.LLDReportedFailure; + return error.LinkFailure; } }, else => { - log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); - return error.LLDCrashed; + return diags.fail("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); }, } @@ -2780,7 +2784,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: if (comp.clang_passthrough_mode) { std.process.exit(exit_code); } else { - return error.LLDReportedFailure; + return diags.fail("{s} returned exit code {d}:\n{s}", .{ argv.items[0], exit_code }); } } }