authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-26 20:57:22+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-26 21:07:47+02:00
loge30f396b732f7fcb68a701f1b385bb47eba48b89
tree1cc9dd4111307feff3ba80ddf1265dd174fc4a82
parent7617486f1d3d79f08d9d7a8641674c2525e83235

elf: properly close the output file when linking


1 files changed, 19 insertions(+), 2 deletions(-)

src/link.zig+19-2
...@@ -456,9 +456,10 @@ pub const File = struct {...@@ -456,9 +456,10 @@ pub const File = struct {
456 .Exe => {},456 .Exe => {},
457 }457 }
458 switch (base.tag) {458 switch (base.tag) {
459 .coff, .elf, .macho, .plan9, .wasm => if (base.file) |f| {459 .elf => if (base.file) |f| {
460 if (build_options.only_c) unreachable;460 if (build_options.only_c) unreachable;
461 if (base.intermediary_basename != null) {461 const use_lld = build_options.have_llvm and base.options.use_lld;
462 if (base.intermediary_basename != null and use_lld) {
462 // The file we have open is not the final file that we want to463 // The file we have open is not the final file that we want to
463 // make executable, so we don't have to close it.464 // make executable, so we don't have to close it.
464 return;465 return;
...@@ -471,6 +472,22 @@ pub const File = struct {...@@ -471,6 +472,22 @@ pub const File = struct {
471 .linux => std.os.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0) catch |err| {472 .linux => std.os.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0) catch |err| {
472 log.warn("ptrace failure: {s}", .{@errorName(err)});473 log.warn("ptrace failure: {s}", .{@errorName(err)});
473 },474 },
475 else => return error.HotSwapUnavailableOnHostOperatingSystem,
476 }
477 }
478 },
479 .coff, .macho, .plan9, .wasm => if (base.file) |f| {
480 if (build_options.only_c) unreachable;
481 if (base.intermediary_basename != null) {
482 // The file we have open is not the final file that we want to
483 // make executable, so we don't have to close it.
484 return;
485 }
486 f.close();
487 base.file = null;
488
489 if (base.child_pid) |pid| {
490 switch (builtin.os.tag) {
474 .macos => base.cast(MachO).?.ptraceDetach(pid) catch |err| {491 .macos => base.cast(MachO).?.ptraceDetach(pid) catch |err| {
475 log.warn("detaching failed with error: {s}", .{@errorName(err)});492 log.warn("detaching failed with error: {s}", .{@errorName(err)});
476 },493 },