diff --git a/build.zig b/build.zig index 212747a..5d7802d 100644 --- a/build.zig +++ b/build.zig @@ -24,6 +24,8 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + addModule(b); + // Testing const elf_unit_tests = b.addTest(.{ .root_source_file = b.path("src/elf.zig"), @@ -36,3 +38,10 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_elf_unit_tests.step); } + +/// Module function for depending on the elphin module +pub fn addModule(b: *std.Build) void { + _ = b.addModule("elphin", .{ + .root_source_file = .{ .path = "src/lib.zig" }, + }); +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..4c3a22a --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,22 @@ +const elf = @import("elf.zig"); +const dol = @import("dol.zig"); + +pub const ELFError = elf.ELFError; +pub const ELFSegments = elf.ELFSegments; + +pub const readELF = elf.readELF; +pub const createDOLMapping = dol.createDOLMapping; +pub const writeDOL = dol.writeDOL; + +/// Converts an ELF file to a DOL file. +/// `input` and `output` must be open files. +pub fn convert(input: anytype, output: anytype) !void { + // Read input + const map = try elf.readELF(input); + + // Align segments to 64 byte boundaries + const dolMap = dol.createDOLMapping(map); + + // Write header and copy over segments from input + try dol.writeDOL(dolMap, input, output); +} diff --git a/src/main.zig b/src/main.zig index 5254699..0a8b286 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,5 @@ const std = @import("std"); -const elf = @import("elf.zig"); -const dol = @import("dol.zig"); +const lib = @import("lib.zig"); pub fn main() !void { // Get allocator @@ -23,13 +22,10 @@ pub fn main() !void { // Read input const input = try std.fs.cwd().openFile(inputPath, .{}); defer input.close(); - const map = try elf.readELF(input); - - // Align segments to 64 byte boundaries - const dolMap = dol.createDOLMapping(map); // Write header and copy over segments from input const output = try std.fs.cwd().createFile(outputPath, .{}); defer output.close(); - try dol.writeDOL(dolMap, input, output); + + try lib.convert(input, output); }