diff --git a/build.zig.zon b/build.zig.zon index 2c00d5d..16bb646 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "elphin", - .version = "0.1.5", + .version = "0.1.6", .minimum_zig_version = "0.12.0", .paths = .{ "build.zig", diff --git a/src/buildlib.zig b/src/buildlib.zig index ed4e639..e661af3 100644 --- a/src/buildlib.zig +++ b/src/buildlib.zig @@ -60,7 +60,7 @@ fn make(step: *std.Build.Step, _: *std.Progress.Node) !void { defer output.close(); // Run conversion - try lib.convert(input, output); + try lib.convert(input, output, false); // Copy file to destination const cwd = std.fs.cwd(); diff --git a/src/dol.zig b/src/dol.zig index 0501136..777f86e 100644 --- a/src/dol.zig +++ b/src/dol.zig @@ -83,7 +83,7 @@ pub fn createDOLMapping(segments: elf.ELFSegments) DolMap { return dolMap; } -pub fn writeDOL(map: DolMap, input: std.fs.File, output: std.fs.File) !void { +pub fn writeDOL(map: DolMap, input: std.fs.File, output: std.fs.File, verbose: bool) !void { const reader = input.reader(); const writer = output.writer(); @@ -106,7 +106,9 @@ pub fn writeDOL(map: DolMap, input: std.fs.File, output: std.fs.File) !void { try output.seekTo(map.header.textOffsets[i]); // Copy segment - std.log.debug("Copying text segment {d} at 0x{x} -> 0x{x}", .{ i, map.header.textAddress[i], map.header.textOffsets[i] }); + if (verbose) { + std.log.debug("Copying text segment {d} at 0x{x} -> 0x{x}", .{ i, map.header.textAddress[i], map.header.textOffsets[i] }); + } try fifo.pump(&limitedReader, writer); } @@ -122,7 +124,9 @@ pub fn writeDOL(map: DolMap, input: std.fs.File, output: std.fs.File) !void { try output.seekTo(map.header.dataOffsets[i]); // Copy segment - std.log.debug("Copying data segment {d} at 0x{x} -> 0x{x}", .{ i, map.header.dataAddress[i], map.header.dataOffsets[i] }); + if (verbose) { + std.log.debug("Copying data segment {d} at 0x{x} -> 0x{x}", .{ i, map.header.dataAddress[i], map.header.dataOffsets[i] }); + } try fifo.pump(&limitedReader, writer); } } diff --git a/src/elf.zig b/src/elf.zig index e3c16d8..f64bce3 100644 --- a/src/elf.zig +++ b/src/elf.zig @@ -78,7 +78,7 @@ pub const ELFSegments = struct { bssSize: u32, }; -pub fn readELF(file: std.fs.File) !ELFSegments { +pub fn readELF(file: std.fs.File, verbose: bool) !ELFSegments { // Create segment map var elfMap: ELFSegments = .{ .entryPoint = undefined, @@ -156,10 +156,14 @@ pub fn readELF(file: std.fs.File) !ELFSegments { if (programHeader.p_filesz < programHeader.p_memsz) { // Add as BSS segment of whatever is left between the file and memory sizes addOrExtendBSS(&elfMap, programHeader.p_paddr + programHeader.p_filesz, programHeader.p_memsz - programHeader.p_filesz); - std.log.debug("Found bss segment (TEXT) at 0x{x}", .{programHeader.p_paddr + programHeader.p_filesz}); + if (verbose) { + std.log.debug("Found bss segment (TEXT) at 0x{x}", .{programHeader.p_paddr + programHeader.p_filesz}); + } } - std.log.debug("Found text segment at 0x{x}", .{programHeader.p_vaddr}); + if (verbose) { + std.log.debug("Found text segment at 0x{x}", .{programHeader.p_vaddr}); + } elfMap.text[elfMap.textCount] = .{ .address = programHeader.p_paddr, @@ -170,11 +174,12 @@ pub fn readELF(file: std.fs.File) !ELFSegments { elfMap.textCount += 1; } else { // DATA or BSS segment - - // TODO: ???? if (programHeader.p_filesz == 0) { addOrExtendBSS(&elfMap, programHeader.p_paddr, programHeader.p_memsz); - std.log.debug("Found bss segment (DATA) at 0x{x}", .{programHeader.p_vaddr}); + + if (verbose) { + std.log.debug("Found bss segment (DATA) at 0x{x}", .{programHeader.p_vaddr}); + } continue; } @@ -183,7 +188,9 @@ pub fn readELF(file: std.fs.File) !ELFSegments { return ELFError.TooManyDataSegments; } - std.log.debug("Found data segment at 0x{x}", .{programHeader.p_vaddr}); + if (verbose) { + std.log.debug("Found data segment at 0x{x}", .{programHeader.p_vaddr}); + } elfMap.data[elfMap.dataCount] = .{ .address = programHeader.p_paddr, diff --git a/src/lib.zig b/src/lib.zig index 4c3a22a..23132f7 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -10,13 +10,13 @@ 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 { +pub fn convert(input: anytype, output: anytype, verbose: bool) !void { // Read input - const map = try elf.readELF(input); + const map = try elf.readELF(input, verbose); // 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); + try dol.writeDOL(dolMap, input, output, verbose); } diff --git a/src/main.zig b/src/main.zig index 0a8b286..44cdab8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -27,5 +27,5 @@ pub fn main() !void { const output = try std.fs.cwd().createFile(outputPath, .{}); defer output.close(); - try lib.convert(input, output); + try lib.convert(input, output, true); }