Skip to content

Commit

Permalink
coff: Add initial Coff support
Browse files Browse the repository at this point in the history
  • Loading branch information
Pombal authored and kubkon committed Sep 24, 2021
1 parent 67a1f63 commit 5c608b7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
59 changes: 59 additions & 0 deletions src/Coff.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const Coff = @This();

const std = @import("std");
const assert = std.debug.assert;
const coff = std.coff;
const fs = std.fs;
const log = std.log.scoped(.coff);
const mem = std.mem;

const Allocator = mem.Allocator;
const Zld = @import("Zld.zig");

pub const base_tag = Zld.Tag.coff;

base: Zld,

pub fn openPath(allocator: *Allocator, options: Zld.Options) !*Coff {
const file = try options.emit.directory.createFile(options.emit.sub_path, .{
.truncate = true,
.read = true,
.mode = if (std.Target.current.os.tag == .windows) 0 else 0o777,
});
errdefer file.close();

const self = try createEmpty(allocator, options);
errdefer allocator.destroy(self);

self.base.file = file;

return self;
}

fn createEmpty(gpa: *Allocator, options: Zld.Options) !*Coff {
const self = try gpa.create(Coff);

self.* = .{
.base = .{
.tag = .coff,
.options = options,
.allocator = gpa,
.file = undefined,
},
};

return self;
}

pub fn deinit(self: *Coff) void {
_ = self;
}

pub fn closeFiles(self: Coff) void {
_ = self;
}

pub fn flush(self: *Coff) !void {
_ = self;
return error.TODOFlushInCoffLinker;
}
9 changes: 5 additions & 4 deletions src/Zld.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const process = std.process;
const Allocator = mem.Allocator;
const Elf = @import("Elf.zig");
const MachO = @import("MachO.zig");
const Coff = @import("Coff.zig");

tag: Tag,
allocator: *Allocator,
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn openPath(allocator: *Allocator, options: Options) !*Zld {
return switch (options.target.os.tag) {
.linux => &(try Elf.openPath(allocator, options)).base,
.macos => &(try MachO.openPath(allocator, options)).base,
.windows => error.TODOCoffLinker,
.windows => &(try Coff.openPath(allocator, options)).base,
else => error.Unimplemented,
};
}
Expand All @@ -59,15 +60,15 @@ pub fn deinit(base: *Zld) void {
switch (base.tag) {
.elf => @fieldParentPtr(Elf, "base", base).deinit(),
.macho => @fieldParentPtr(MachO, "base", base).deinit(),
else => {},
.coff => @fieldParentPtr(Coff, "base", base).deinit(),
}
}

pub fn closeFiles(base: Zld) void {
switch (base.tag) {
.elf => @fieldParentPtr(Elf, "base", &base).closeFiles(),
.macho => @fieldParentPtr(MachO, "base", &base).closeFiles(),
else => {},
.coff => @fieldParentPtr(Coff, "base", &base).closeFiles(),
}
base.file.close();
}
Expand All @@ -76,6 +77,6 @@ pub fn flush(base: *Zld) !void {
switch (base.tag) {
.elf => try @fieldParentPtr(Elf, "base", base).flush(),
.macho => try @fieldParentPtr(MachO, "base", base).flush(),
.coff => return error.TODOCoffLinker,
.coff => try @fieldParentPtr(Coff, "base", base).flush(),
}
}

0 comments on commit 5c608b7

Please sign in to comment.