authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-09 02:26:13-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-09 00:32:30-07:00
log85c1db9222f6aa9abbc50309dfb7ce56b3cb6cf0
tree5e58184b91fe86ce5fc060b5bfda6b9be99b709b
parent40e37d4324dd4e74c2a3285a73942b6ae50d003e

Merge pull request #7355 from ziglang/lld-child-process

invoke LLD as a child process rather than a library

11 files changed, 332 insertions(+), 254 deletions(-)

lib/std/testing.zig+21
......@@ -247,6 +247,7 @@ test "expectWithinEpsilon" {
247247/// This function is intended to be used only in tests. When the two slices are not
248248/// equal, prints diagnostics to stderr to show exactly how they are not equal,
249249/// then aborts.
250/// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead.
250251pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void {
251252 // TODO better printing of the difference
252253 // If the arrays are small enough we could print the whole thing
......@@ -368,6 +369,26 @@ pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void {
368369 }
369370}
370371
372pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8) void {
373 if (std.mem.endsWith(u8, actual, expected_ends_with))
374 return;
375
376 const shortened_actual = if (actual.len >= expected_ends_with.len)
377 actual[0..expected_ends_with.len]
378 else
379 actual;
380
381 print("\n====== expected to end with: =========\n", .{});
382 printWithVisibleNewlines(expected_ends_with);
383 print("\n====== instead ended with: ===========\n", .{});
384 printWithVisibleNewlines(shortened_actual);
385 print("\n========= full output: ==============\n", .{});
386 printWithVisibleNewlines(actual);
387 print("\n======================================\n", .{});
388
389 @panic("test failure");
390}
391
371392fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
372393 const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
373394 line_begin + 1
src/Compilation.zig+1-1
......@@ -1756,7 +1756,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_comp_progress_node: *
17561756 if (comp.clang_preprocessor_mode == .stdout)
17571757 std.process.exit(0);
17581758 },
1759 else => std.process.exit(1),
1759 else => std.process.abort(),
17601760 }
17611761 } else {
17621762 child.stdin_behavior = .Ignore;
src/link/Coff.zig+60-52
......@@ -907,8 +907,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
907907 // Create an LLD command line and invoke it.
908908 var argv = std.ArrayList([]const u8).init(self.base.allocator);
909909 defer argv.deinit();
910 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
911 try argv.append("lld");
910 // We will invoke ourselves as a child process to gain access to LLD.
911 // This is necessary because LLD does not behave properly as a library -
912 // it calls exit() and does not reset all global data between invocations.
913 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "lld-link" });
912914
913915 try argv.append("-ERRORLIMIT:0");
914916 try argv.append("-NOLOGO");
......@@ -1146,45 +1148,65 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
11461148 }
11471149
11481150 if (self.base.options.verbose_link) {
1149 Compilation.dump_argv(argv.items);
1151 // Skip over our own name so that the LLD linker name is the first argv item.
1152 Compilation.dump_argv(argv.items[1..]);
11501153 }
11511154
1152 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
1153 for (argv.items) |arg, i| {
1154 new_argv[i] = try arena.dupeZ(u8, arg);
1155 }
1155 // Sadly, we must run LLD as a child process because it does not behave
1156 // properly as a library.
1157 const child = try std.ChildProcess.init(argv.items, arena);
1158 defer child.deinit();
1159
1160 if (comp.clang_passthrough_mode) {
1161 child.stdin_behavior = .Inherit;
1162 child.stdout_behavior = .Inherit;
1163 child.stderr_behavior = .Inherit;
1164
1165 const term = child.spawnAndWait() catch |err| {
1166 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
1167 return error.UnableToSpawnSelf;
1168 };
1169 switch (term) {
1170 .Exited => |code| {
1171 if (code != 0) {
1172 // TODO https://github.com/ziglang/zig/issues/6342
1173 std.process.exit(1);
1174 }
1175 },
1176 else => std.process.abort(),
1177 }
1178 } else {
1179 child.stdin_behavior = .Ignore;
1180 child.stdout_behavior = .Ignore;
1181 child.stderr_behavior = .Pipe;
1182
1183 try child.spawn();
1184
1185 const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024);
1186
1187 const term = child.wait() catch |err| {
1188 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
1189 return error.UnableToSpawnSelf;
1190 };
1191
1192 switch (term) {
1193 .Exited => |code| {
1194 if (code != 0) {
1195 // TODO parse this output and surface with the Compilation API rather than
1196 // directly outputting to stderr here.
1197 std.debug.print("{s}", .{stderr});
1198 return error.LLDReportedFailure;
1199 }
1200 },
1201 else => {
1202 log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr });
1203 return error.LLDCrashed;
1204 },
1205 }
11561206
1157 var stderr_context: LLDContext = .{
1158 .coff = self,
1159 .data = std.ArrayList(u8).init(self.base.allocator),
1160 };
1161 defer stderr_context.data.deinit();
1162 var stdout_context: LLDContext = .{
1163 .coff = self,
1164 .data = std.ArrayList(u8).init(self.base.allocator),
1165 };
1166 defer stdout_context.data.deinit();
1167 const llvm = @import("../llvm.zig");
1168 const ok = llvm.Link(
1169 .COFF,
1170 new_argv.ptr,
1171 new_argv.len,
1172 append_diagnostic,
1173 @ptrToInt(&stdout_context),
1174 @ptrToInt(&stderr_context),
1175 );
1176 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
1177 if (stdout_context.data.items.len != 0) {
1178 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
1179 }
1180 if (!ok) {
1181 // TODO parse this output and surface with the Compilation API rather than
1182 // directly outputting to stderr here.
1183 std.debug.print("{}", .{stderr_context.data.items});
1184 return error.LLDReportedFailure;
1185 }
1186 if (stderr_context.data.items.len != 0) {
1187 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
1207 if (stderr.len != 0) {
1208 std.log.warn("unexpected LLD stderr:\n{s}", .{stderr});
1209 }
11881210 }
11891211 }
11901212
......@@ -1204,20 +1226,6 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
12041226 }
12051227}
12061228
1207const LLDContext = struct {
1208 data: std.ArrayList(u8),
1209 coff: *Coff,
1210 oom: bool = false,
1211};
1212
1213fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void {
1214 const lld_context = @intToPtr(*LLDContext, context);
1215 const msg = ptr[0..len];
1216 lld_context.data.appendSlice(msg) catch |err| switch (err) {
1217 error.OutOfMemory => lld_context.oom = true,
1218 };
1219}
1220
12211229pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 {
12221230 return self.text_section_virtual_address + decl.link.coff.text_offset;
12231231}
src/link/Elf.zig+60-53
......@@ -1360,8 +1360,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13601360 // Create an LLD command line and invoke it.
13611361 var argv = std.ArrayList([]const u8).init(self.base.allocator);
13621362 defer argv.deinit();
1363 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
1364 try argv.append("lld");
1363 // We will invoke ourselves as a child process to gain access to LLD.
1364 // This is necessary because LLD does not behave properly as a library -
1365 // it calls exit() and does not reset all global data between invocations.
1366 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "ld.lld" });
13651367 if (is_obj) {
13661368 try argv.append("-r");
13671369 }
......@@ -1621,46 +1623,65 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16211623 }
16221624
16231625 if (self.base.options.verbose_link) {
1624 Compilation.dump_argv(argv.items);
1626 // Skip over our own name so that the LLD linker name is the first argv item.
1627 Compilation.dump_argv(argv.items[1..]);
16251628 }
16261629
1627 // Oh, snapplesauce! We need null terminated argv.
1628 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
1629 for (argv.items) |arg, i| {
1630 new_argv[i] = try arena.dupeZ(u8, arg);
1631 }
1630 // Sadly, we must run LLD as a child process because it does not behave
1631 // properly as a library.
1632 const child = try std.ChildProcess.init(argv.items, arena);
1633 defer child.deinit();
16321634
1633 var stderr_context: LLDContext = .{
1634 .elf = self,
1635 .data = std.ArrayList(u8).init(self.base.allocator),
1636 };
1637 defer stderr_context.data.deinit();
1638 var stdout_context: LLDContext = .{
1639 .elf = self,
1640 .data = std.ArrayList(u8).init(self.base.allocator),
1641 };
1642 defer stdout_context.data.deinit();
1643 const llvm = @import("../llvm.zig");
1644 const ok = llvm.Link(
1645 .ELF,
1646 new_argv.ptr,
1647 new_argv.len,
1648 append_diagnostic,
1649 @ptrToInt(&stdout_context),
1650 @ptrToInt(&stderr_context),
1651 );
1652 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
1653 if (stdout_context.data.items.len != 0) {
1654 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
1655 }
1656 if (!ok) {
1657 // TODO parse this output and surface with the Compilation API rather than
1658 // directly outputting to stderr here.
1659 std.debug.print("{}", .{stderr_context.data.items});
1660 return error.LLDReportedFailure;
1661 }
1662 if (stderr_context.data.items.len != 0) {
1663 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
1635 if (comp.clang_passthrough_mode) {
1636 child.stdin_behavior = .Inherit;
1637 child.stdout_behavior = .Inherit;
1638 child.stderr_behavior = .Inherit;
1639
1640 const term = child.spawnAndWait() catch |err| {
1641 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
1642 return error.UnableToSpawnSelf;
1643 };
1644 switch (term) {
1645 .Exited => |code| {
1646 if (code != 0) {
1647 // TODO https://github.com/ziglang/zig/issues/6342
1648 std.process.exit(1);
1649 }
1650 },
1651 else => std.process.abort(),
1652 }
1653 } else {
1654 child.stdin_behavior = .Ignore;
1655 child.stdout_behavior = .Ignore;
1656 child.stderr_behavior = .Pipe;
1657
1658 try child.spawn();
1659
1660 const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024);
1661
1662 const term = child.wait() catch |err| {
1663 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
1664 return error.UnableToSpawnSelf;
1665 };
1666
1667 switch (term) {
1668 .Exited => |code| {
1669 if (code != 0) {
1670 // TODO parse this output and surface with the Compilation API rather than
1671 // directly outputting to stderr here.
1672 std.debug.print("{s}", .{stderr});
1673 return error.LLDReportedFailure;
1674 }
1675 },
1676 else => {
1677 log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr });
1678 return error.LLDCrashed;
1679 },
1680 }
1681
1682 if (stderr.len != 0) {
1683 std.log.warn("unexpected LLD stderr:\n{s}", .{stderr});
1684 }
16641685 }
16651686
16661687 if (!self.base.options.disable_lld_caching) {
......@@ -1679,20 +1700,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16791700 }
16801701}
16811702
1682const LLDContext = struct {
1683 data: std.ArrayList(u8),
1684 elf: *Elf,
1685 oom: bool = false,
1686};
1687
1688fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void {
1689 const lld_context = @intToPtr(*LLDContext, context);
1690 const msg = ptr[0..len];
1691 lld_context.data.appendSlice(msg) catch |err| switch (err) {
1692 error.OutOfMemory => lld_context.oom = true,
1693 };
1694}
1695
16961703fn writeDwarfAddrAssumeCapacity(self: *Elf, buf: *std.ArrayList(u8), addr: u64) void {
16971704 const target_endian = self.base.options.target.cpu.arch.endian();
16981705 switch (self.ptr_width) {
src/link/MachO.zig+61-52
......@@ -544,8 +544,10 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
544544 if (self.base.options.system_linker_hack) {
545545 try argv.append("ld");
546546 } else {
547 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
548 try argv.append("lld");
547 // We will invoke ourselves as a child process to gain access to LLD.
548 // This is necessary because LLD does not behave properly as a library -
549 // it calls exit() and does not reset all global data between invocations.
550 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "ld64.lld" });
549551
550552 try argv.append("-error-limit");
551553 try argv.append("0");
......@@ -711,7 +713,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
711713 }
712714
713715 if (self.base.options.verbose_link) {
714 Compilation.dump_argv(argv.items);
716 // Potentially skip over our own name so that the LLD linker name is the first argv item.
717 const adjusted_argv = if (self.base.options.system_linker_hack) argv.items else argv.items[1..];
718 Compilation.dump_argv(adjusted_argv);
715719 }
716720
717721 // TODO https://github.com/ziglang/zig/issues/6971
......@@ -736,42 +740,61 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
736740 return error.LDReportedFailure;
737741 }
738742 } else {
739 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
740 for (argv.items) |arg, i| {
741 new_argv[i] = try arena.dupeZ(u8, arg);
742 }
743 // Sadly, we must run LLD as a child process because it does not behave
744 // properly as a library.
745 const child = try std.ChildProcess.init(argv.items, arena);
746 defer child.deinit();
747
748 if (comp.clang_passthrough_mode) {
749 child.stdin_behavior = .Inherit;
750 child.stdout_behavior = .Inherit;
751 child.stderr_behavior = .Inherit;
752
753 const term = child.spawnAndWait() catch |err| {
754 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
755 return error.UnableToSpawnSelf;
756 };
757 switch (term) {
758 .Exited => |code| {
759 if (code != 0) {
760 // TODO https://github.com/ziglang/zig/issues/6342
761 std.process.exit(1);
762 }
763 },
764 else => std.process.abort(),
765 }
766 } else {
767 child.stdin_behavior = .Ignore;
768 child.stdout_behavior = .Ignore;
769 child.stderr_behavior = .Pipe;
770
771 try child.spawn();
772
773 const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024);
774
775 const term = child.wait() catch |err| {
776 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
777 return error.UnableToSpawnSelf;
778 };
779
780 switch (term) {
781 .Exited => |code| {
782 if (code != 0) {
783 // TODO parse this output and surface with the Compilation API rather than
784 // directly outputting to stderr here.
785 std.debug.print("{s}", .{stderr});
786 return error.LLDReportedFailure;
787 }
788 },
789 else => {
790 log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr });
791 return error.LLDCrashed;
792 },
793 }
743794
744 var stderr_context: LLDContext = .{
745 .macho = self,
746 .data = std.ArrayList(u8).init(self.base.allocator),
747 };
748 defer stderr_context.data.deinit();
749 var stdout_context: LLDContext = .{
750 .macho = self,
751 .data = std.ArrayList(u8).init(self.base.allocator),
752 };
753 defer stdout_context.data.deinit();
754 const llvm = @import("../llvm.zig");
755 const ok = llvm.Link(
756 .MachO,
757 new_argv.ptr,
758 new_argv.len,
759 append_diagnostic,
760 @ptrToInt(&stdout_context),
761 @ptrToInt(&stderr_context),
762 );
763 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
764 if (stdout_context.data.items.len != 0) {
765 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
766 }
767 if (!ok) {
768 // TODO parse this output and surface with the Compilation API rather than
769 // directly outputting to stderr here.
770 std.debug.print("{}", .{stderr_context.data.items});
771 return error.LLDReportedFailure;
772 }
773 if (stderr_context.data.items.len != 0) {
774 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
795 if (stderr.len != 0) {
796 std.log.warn("unexpected LLD stderr:\n{s}", .{stderr});
797 }
775798 }
776799 }
777800 }
......@@ -792,20 +815,6 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
792815 }
793816}
794817
795const LLDContext = struct {
796 data: std.ArrayList(u8),
797 macho: *MachO,
798 oom: bool = false,
799};
800
801fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void {
802 const lld_context = @intToPtr(*LLDContext, context);
803 const msg = ptr[0..len];
804 lld_context.data.appendSlice(msg) catch |err| switch (err) {
805 error.OutOfMemory => lld_context.oom = true,
806 };
807}
808
809818fn darwinArchString(arch: std.Target.Cpu.Arch) []const u8 {
810819 return switch (arch) {
811820 .aarch64, .aarch64_be, .aarch64_32 => "arm64",
src/link/Wasm.zig+60-52
......@@ -345,8 +345,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
345345 // Create an LLD command line and invoke it.
346346 var argv = std.ArrayList([]const u8).init(self.base.allocator);
347347 defer argv.deinit();
348 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
349 try argv.append("lld");
348 // We will invoke ourselves as a child process to gain access to LLD.
349 // This is necessary because LLD does not behave properly as a library -
350 // it calls exit() and does not reset all global data between invocations.
351 try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" });
350352 if (is_obj) {
351353 try argv.append("-r");
352354 }
......@@ -396,45 +398,65 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
396398 }
397399
398400 if (self.base.options.verbose_link) {
399 Compilation.dump_argv(argv.items);
401 // Skip over our own name so that the LLD linker name is the first argv item.
402 Compilation.dump_argv(argv.items[1..]);
400403 }
401404
402 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
403 for (argv.items) |arg, i| {
404 new_argv[i] = try arena.dupeZ(u8, arg);
405 }
405 // Sadly, we must run LLD as a child process because it does not behave
406 // properly as a library.
407 const child = try std.ChildProcess.init(argv.items, arena);
408 defer child.deinit();
406409
407 var stderr_context: LLDContext = .{
408 .wasm = self,
409 .data = std.ArrayList(u8).init(self.base.allocator),
410 };
411 defer stderr_context.data.deinit();
412 var stdout_context: LLDContext = .{
413 .wasm = self,
414 .data = std.ArrayList(u8).init(self.base.allocator),
415 };
416 defer stdout_context.data.deinit();
417 const llvm = @import("../llvm.zig");
418 const ok = llvm.Link(
419 .Wasm,
420 new_argv.ptr,
421 new_argv.len,
422 append_diagnostic,
423 @ptrToInt(&stdout_context),
424 @ptrToInt(&stderr_context),
425 );
426 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
427 if (stdout_context.data.items.len != 0) {
428 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
429 }
430 if (!ok) {
431 // TODO parse this output and surface with the Compilation API rather than
432 // directly outputting to stderr here.
433 std.debug.print("{}", .{stderr_context.data.items});
434 return error.LLDReportedFailure;
435 }
436 if (stderr_context.data.items.len != 0) {
437 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
410 if (comp.clang_passthrough_mode) {
411 child.stdin_behavior = .Inherit;
412 child.stdout_behavior = .Inherit;
413 child.stderr_behavior = .Inherit;
414
415 const term = child.spawnAndWait() catch |err| {
416 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
417 return error.UnableToSpawnSelf;
418 };
419 switch (term) {
420 .Exited => |code| {
421 if (code != 0) {
422 // TODO https://github.com/ziglang/zig/issues/6342
423 std.process.exit(1);
424 }
425 },
426 else => std.process.abort(),
427 }
428 } else {
429 child.stdin_behavior = .Ignore;
430 child.stdout_behavior = .Ignore;
431 child.stderr_behavior = .Pipe;
432
433 try child.spawn();
434
435 const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024);
436
437 const term = child.wait() catch |err| {
438 log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) });
439 return error.UnableToSpawnSelf;
440 };
441
442 switch (term) {
443 .Exited => |code| {
444 if (code != 0) {
445 // TODO parse this output and surface with the Compilation API rather than
446 // directly outputting to stderr here.
447 std.debug.print("{s}", .{stderr});
448 return error.LLDReportedFailure;
449 }
450 },
451 else => {
452 log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr });
453 return error.LLDCrashed;
454 },
455 }
456
457 if (stderr.len != 0) {
458 std.log.warn("unexpected LLD stderr:\n{s}", .{stderr});
459 }
438460 }
439461
440462 if (!self.base.options.disable_lld_caching) {
......@@ -453,20 +475,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
453475 }
454476}
455477
456const LLDContext = struct {
457 data: std.ArrayList(u8),
458 wasm: *Wasm,
459 oom: bool = false,
460};
461
462fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void {
463 const lld_context = @intToPtr(*LLDContext, context);
464 const msg = ptr[0..len];
465 lld_context.data.appendSlice(msg) catch |err| switch (err) {
466 error.OutOfMemory => lld_context.oom = true,
467 };
468}
469
470478/// Get the current index of a given Decl in the function list
471479/// TODO: we could maintain a hash map to potentially make this
472480fn getFuncidx(self: Wasm, decl: *Module.Decl) ?u32 {
src/llvm.zig+9-9
......@@ -1,15 +1,15 @@
11//! We do this instead of @cImport because the self-hosted compiler is easier
22//! to bootstrap if it does not depend on translate-c.
33
4pub const Link = ZigLLDLink;
5extern fn ZigLLDLink(
6 oformat: ObjectFormatType,
7 args: [*:null]const ?[*:0]const u8,
8 arg_count: usize,
9 append_diagnostic: fn (context: usize, ptr: [*]const u8, len: usize) callconv(.C) void,
10 context_stdout: usize,
11 context_stderr: usize,
12) bool;
4extern fn ZigLLDLinkCOFF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
5extern fn ZigLLDLinkELF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
6extern fn ZigLLDLinkMachO(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
7extern fn ZigLLDLinkWasm(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
8
9pub const LinkCOFF = ZigLLDLinkCOFF;
10pub const LinkELF = ZigLLDLinkELF;
11pub const LinkMachO = ZigLLDLinkMachO;
12pub const LinkWasm = ZigLLDLinkWasm;
1313
1414pub const ObjectFormatType = extern enum(c_int) {
1515 Unknown,
src/main.zig+39
......@@ -176,6 +176,12 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
176176 mem.eql(u8, cmd, "-cc1") or mem.eql(u8, cmd, "-cc1as"))
177177 {
178178 return punt_to_clang(arena, args);
179 } else if (mem.eql(u8, cmd, "ld.lld") or
180 mem.eql(u8, cmd, "ld64.lld") or
181 mem.eql(u8, cmd, "lld-link") or
182 mem.eql(u8, cmd, "wasm-ld"))
183 {
184 return punt_to_lld(arena, args);
179185 } else if (mem.eql(u8, cmd, "build")) {
180186 return cmdBuild(gpa, arena, cmd_args);
181187 } else if (mem.eql(u8, cmd, "fmt")) {
......@@ -2786,6 +2792,39 @@ fn punt_to_clang(arena: *Allocator, args: []const []const u8) error{OutOfMemory}
27862792 process.exit(@bitCast(u8, @truncate(i8, exit_code)));
27872793}
27882794
2795/// The first argument determines which backend is invoked. The options are:
2796/// * `ld.lld` - ELF
2797/// * `ld64.lld` - Mach-O
2798/// * `lld-link` - COFF
2799/// * `wasm-ld` - WebAssembly
2800/// TODO https://github.com/ziglang/zig/issues/3257
2801pub fn punt_to_lld(arena: *Allocator, args: []const []const u8) error{OutOfMemory} {
2802 if (!build_options.have_llvm)
2803 fatal("`zig {s}` unavailable: compiler built without LLVM extensions", .{args[0]});
2804 // Convert the args to the format LLD expects.
2805 // We subtract 1 to shave off the zig binary from args[0].
2806 const argv = try arena.allocSentinel(?[*:0]const u8, args.len - 1, null);
2807 for (args[1..]) |arg, i| {
2808 argv[i] = try arena.dupeZ(u8, arg); // TODO If there was an argsAllocZ we could avoid this allocation.
2809 }
2810 const exit_code = rc: {
2811 const llvm = @import("llvm.zig");
2812 const argc = @intCast(c_int, argv.len);
2813 if (mem.eql(u8, args[1], "ld.lld")) {
2814 break :rc llvm.LinkELF(argc, argv.ptr, true);
2815 } else if (mem.eql(u8, args[1], "ld64.lld")) {
2816 break :rc llvm.LinkMachO(argc, argv.ptr, true);
2817 } else if (mem.eql(u8, args[1], "lld-link")) {
2818 break :rc llvm.LinkCOFF(argc, argv.ptr, true);
2819 } else if (mem.eql(u8, args[1], "wasm-ld")) {
2820 break :rc llvm.LinkWasm(argc, argv.ptr, true);
2821 } else {
2822 unreachable;
2823 }
2824 };
2825 process.exit(@bitCast(u8, @truncate(i8, exit_code)));
2826}
2827
27892828const clang_args = @import("clang_options.zig").list;
27902829
27912830pub const ClangArgIterator = struct {
src/zig_llvm.cpp+15-30
......@@ -1048,39 +1048,24 @@ bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size
10481048 return false;
10491049}
10501050
1051int ZigLLDLinkCOFF(int argc, const char **argv, bool can_exit_early) {
1052 std::vector<const char *> args(argv, argv + argc);
1053 return lld::coff::link(args, can_exit_early, llvm::outs(), llvm::errs());
1054}
10511055
1052bool ZigLLDLink(ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count,
1053 void (*append_diagnostic)(void *, const char *, size_t),
1054 void *context_stdout, void *context_stderr)
1055{
1056 ArrayRef<const char *> array_ref_args(args, arg_count);
1057
1058 MyOStream diag_stdout(append_diagnostic, context_stdout);
1059 MyOStream diag_stderr(append_diagnostic, context_stderr);
1060
1061 switch (oformat) {
1062 case ZigLLVM_UnknownObjectFormat:
1063 case ZigLLVM_XCOFF:
1064 assert(false); // unreachable
1065 break;
1066
1067 case ZigLLVM_COFF:
1068 return lld::coff::link(array_ref_args, false, diag_stdout, diag_stderr);
1069
1070 case ZigLLVM_ELF:
1071 return lld::elf::link(array_ref_args, false, diag_stdout, diag_stderr);
1072
1073 case ZigLLVM_MachO:
1074 return lld::mach_o::link(array_ref_args, false, diag_stdout, diag_stderr);
1056int ZigLLDLinkELF(int argc, const char **argv, bool can_exit_early) {
1057 std::vector<const char *> args(argv, argv + argc);
1058 return lld::elf::link(args, can_exit_early, llvm::outs(), llvm::errs());
1059}
10751060
1076 case ZigLLVM_Wasm:
1077 return lld::wasm::link(array_ref_args, false, diag_stdout, diag_stderr);
1061int ZigLLDLinkMachO(int argc, const char **argv, bool can_exit_early) {
1062 std::vector<const char *> args(argv, argv + argc);
1063 return lld::mach_o::link(args, can_exit_early, llvm::outs(), llvm::errs());
1064}
10781065
1079 default:
1080 break;
1081 }
1082 assert(false); // unreachable
1083 abort();
1066int ZigLLDLinkWasm(int argc, const char **argv, bool can_exit_early) {
1067 std::vector<const char *> args(argv, argv + argc);
1068 return lld::wasm::link(args, can_exit_early, llvm::outs(), llvm::errs());
10841069}
10851070
10861071static AtomicRMWInst::BinOp toLLVMRMWBinOp(enum ZigLLVM_AtomicRMWBinOp BinOp) {
src/zig_llvm.h+4-3
......@@ -505,9 +505,10 @@ ZIG_EXTERN_C const char *ZigLLVMGetVendorTypeName(enum ZigLLVM_VendorType vendor
505505ZIG_EXTERN_C const char *ZigLLVMGetOSTypeName(enum ZigLLVM_OSType os);
506506ZIG_EXTERN_C const char *ZigLLVMGetEnvironmentTypeName(enum ZigLLVM_EnvironmentType abi);
507507
508ZIG_EXTERN_C bool ZigLLDLink(enum ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_count,
509 void (*append_diagnostic)(void *, const char *, size_t),
510 void *context_stdout, void *context_stderr);
508ZIG_EXTERN_C int ZigLLDLinkCOFF(int argc, const char **argv, bool can_exit_early);
509ZIG_EXTERN_C int ZigLLDLinkELF(int argc, const char **argv, bool can_exit_early);
510ZIG_EXTERN_C int ZigLLDLinkMachO(int argc, const char **argv, bool can_exit_early);
511ZIG_EXTERN_C int ZigLLDLinkWasm(int argc, const char **argv, bool can_exit_early);
511512
512513ZIG_EXTERN_C bool ZigLLVMWriteArchive(const char *archive_name, const char **file_names, size_t file_name_count,
513514 enum ZigLLVM_OSType os_type);
test/cli.zig+2-2
......@@ -92,13 +92,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
9292fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
9393 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
9494 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
95 testing.expect(std.mem.endsWith(u8, test_result.stderr, "All 1 tests passed.\n"));
95 testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
9696}
9797
9898fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
9999 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
100100 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
101 testing.expect(std.mem.eql(u8, run_result.stderr, "info: All your codebase are belong to us.\n"));
101 testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
102102}
103103
104104fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {