Commit edd592d371

Andrew Kelley <andrew@ziglang.org>
2024-12-13 02:47:40
fix compilation when enabling llvm
1 parent 968941b
Changed files (4)
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;
+    const diags = &comp.link_diags;
     if (use_lld) {
-        return coff.linkWithLLD(arena, tid, prog_node);
+        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)}),
+        };
     }
-    const diags = &comp.link_diags;
     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", .{}),
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);
 }
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 });
                 }
             }
         }
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 });
         },
     }