aboutsummaryrefslogtreecommitdiff
path: root/src/link.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2025-09-21 23:14:28 -0400
committerJacob Young <jacobly0@users.noreply.github.com>2025-10-02 17:44:52 -0400
commite1f3fc6ce289502cde1e52fa946476ff8e3bcaac (patch)
tree42231ed7f2348c3c19b495bf8d4eac3acbfce21b /src/link.zig
parentd5f09f56e0327685dfcaeb889efd9d6a26461886 (diff)
downloadzig-e1f3fc6ce289502cde1e52fa946476ff8e3bcaac.tar.gz
zig-e1f3fc6ce289502cde1e52fa946476ff8e3bcaac.zip
Coff2: create a new linker from scratch
Diffstat (limited to 'src/link.zig')
-rw-r--r--src/link.zig74
1 files changed, 35 insertions, 39 deletions
diff --git a/src/link.zig b/src/link.zig
index 277013efb3..df9c152aef 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -610,27 +610,20 @@ pub const File = struct {
}
}
}
- const output_mode = comp.config.output_mode;
- const link_mode = comp.config.link_mode;
- base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{
- .truncate = false,
- .read = true,
- .mode = determineMode(output_mode, link_mode),
- });
+ base.file = try emit.root_dir.handle.openFile(emit.sub_path, .{ .mode = .read_write });
},
- .elf2 => {
- const elf = base.cast(.elf2).?;
- if (base.file == null) {
- elf.mf.file = try base.emit.root_dir.handle.createFile(base.emit.sub_path, .{
- .truncate = false,
- .read = true,
- .mode = determineMode(comp.config.output_mode, comp.config.link_mode),
- });
- base.file = elf.mf.file;
- try elf.mf.ensureTotalCapacity(
- @intCast(elf.mf.nodes.items[0].location().resolve(&elf.mf)[1]),
- );
- }
+ .elf2, .coff2 => if (base.file == null) {
+ const mf = if (base.cast(.elf2)) |elf|
+ &elf.mf
+ else if (base.cast(.coff2)) |coff|
+ &coff.mf
+ else
+ unreachable;
+ mf.file = try base.emit.root_dir.handle.openFile(base.emit.sub_path, .{
+ .mode = .read_write,
+ });
+ base.file = mf.file;
+ try mf.ensureTotalCapacity(@intCast(mf.nodes.items[0].location().resolve(mf)[1]));
},
.c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }),
.plan9 => unreachable,
@@ -654,12 +647,9 @@ pub const File = struct {
pub fn makeExecutable(base: *File) !void {
dev.check(.make_executable);
const comp = base.comp;
- const output_mode = comp.config.output_mode;
- const link_mode = comp.config.link_mode;
-
- switch (output_mode) {
+ switch (comp.config.output_mode) {
.Obj => return,
- .Lib => switch (link_mode) {
+ .Lib => switch (comp.config.link_mode) {
.static => return,
.dynamic => {},
},
@@ -702,15 +692,18 @@ pub const File = struct {
}
}
},
- .elf2 => {
- const elf = base.cast(.elf2).?;
- if (base.file) |f| {
- elf.mf.unmap();
- assert(elf.mf.file.handle == f.handle);
- elf.mf.file = undefined;
- f.close();
- base.file = null;
- }
+ .elf2, .coff2 => if (base.file) |f| {
+ const mf = if (base.cast(.elf2)) |elf|
+ &elf.mf
+ else if (base.cast(.coff2)) |coff|
+ &coff.mf
+ else
+ unreachable;
+ mf.unmap();
+ assert(mf.file.handle == f.handle);
+ mf.file = undefined;
+ f.close();
+ base.file = null;
},
.c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }),
.plan9 => unreachable,
@@ -828,7 +821,7 @@ pub const File = struct {
.spirv => {},
.goff, .xcoff => {},
.plan9 => unreachable,
- .elf2 => {},
+ .elf2, .coff2 => {},
inline else => |tag| {
dev.check(tag.devFeature());
return @as(*tag.Type(), @fieldParentPtr("base", base)).updateLineNumber(pt, ti_id);
@@ -864,7 +857,7 @@ pub const File = struct {
pub fn idle(base: *File, tid: Zcu.PerThread.Id) !bool {
switch (base.tag) {
else => return false,
- inline .elf2 => |tag| {
+ inline .elf2, .coff2 => |tag| {
dev.check(tag.devFeature());
return @as(*tag.Type(), @fieldParentPtr("base", base)).idle(tid);
},
@@ -874,7 +867,7 @@ pub const File = struct {
pub fn updateErrorData(base: *File, pt: Zcu.PerThread) !void {
switch (base.tag) {
else => {},
- inline .elf2 => |tag| {
+ inline .elf2, .coff2 => |tag| {
dev.check(tag.devFeature());
return @as(*tag.Type(), @fieldParentPtr("base", base)).updateErrorData(pt);
},
@@ -1155,7 +1148,7 @@ pub const File = struct {
if (base.zcu_object_basename != null) return;
switch (base.tag) {
- inline .elf2, .wasm => |tag| {
+ inline .elf2, .coff2, .wasm => |tag| {
dev.check(tag.devFeature());
return @as(*tag.Type(), @fieldParentPtr("base", base)).prelink(base.comp.link_prog_node);
},
@@ -1165,6 +1158,7 @@ pub const File = struct {
pub const Tag = enum {
coff,
+ coff2,
elf,
elf2,
macho,
@@ -1179,6 +1173,7 @@ pub const File = struct {
pub fn Type(comptime tag: Tag) type {
return switch (tag) {
.coff => Coff,
+ .coff2 => Coff2,
.elf => Elf,
.elf2 => Elf2,
.macho => MachO,
@@ -1194,7 +1189,7 @@ pub const File = struct {
fn fromObjectFormat(ofmt: std.Target.ObjectFormat, use_new_linker: bool) Tag {
return switch (ofmt) {
- .coff => .coff,
+ .coff => if (use_new_linker) .coff2 else .coff,
.elf => if (use_new_linker) .elf2 else .elf,
.macho => .macho,
.wasm => .wasm,
@@ -1280,6 +1275,7 @@ pub const File = struct {
pub const Lld = @import("link/Lld.zig");
pub const C = @import("link/C.zig");
pub const Coff = @import("link/Coff.zig");
+ pub const Coff2 = @import("link/Coff2.zig");
pub const Elf = @import("link/Elf.zig");
pub const Elf2 = @import("link/Elf2.zig");
pub const MachO = @import("link/MachO.zig");