From b8af8595171ed82e2923399aac561e45d3396d50 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sun, 5 May 2024 16:00:47 +0200 Subject: [PATCH] it fucking works lmao --- src/elf.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 10 ++++++++-- src/oldzig.zig | 13 +++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/oldzig.zig diff --git a/src/elf.zig b/src/elf.zig index a0e406e..cc4aa15 100644 --- a/src/elf.zig +++ b/src/elf.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const oldzig = @import("oldzig.zig"); // Cool website for ELF info: // https://www.man7.org/linux/man-pages/man5/elf.5.html @@ -253,6 +254,50 @@ pub fn alignSegments(map: *DolMap) void { } } +pub fn writeDOL(map: DolMap, input: std.fs.File, output: std.fs.File) !void { + const reader = input.reader(); + const writer = output.writer(); + + // Write header + try oldzig.writeStructEndian(writer, map.header, std.builtin.Endian.big); + + // Create buffer for pump + var fifo = std.fifo.LinearFifo(u8, .{ .Static = 1024 * 1024 }).init(); + defer fifo.deinit(); + + // Copy over text segments + for (0..map.text_cnt) |i| { + // Seek to text segment + try input.seekTo(map.text_elf_off[i]); + + // Create limited reader to segment size + var limitedReader = std.io.limitedReader(reader, map.header.text_size[i]); + + // Seek to destination + try output.seekTo(map.header.text_off[i]); + + // Copy segment + std.debug.print("Copying text segment {d} at 0x{x} -> 0x{x}\n", .{ i, map.header.text_addr[i], map.header.text_off[i] }); + try fifo.pump(&limitedReader, writer); + } + + // Copy over data segments + for (0..map.data_cnt) |i| { + // Seek to text segment + try input.seekTo(map.data_elf_off[i]); + + // Create limited reader to segment size + var limitedReader = std.io.limitedReader(reader, map.header.data_size[i]); + + // Seek to destination + try output.seekTo(map.header.data_off[i]); + + // Copy segment + std.debug.print("Copying data segment {d} at 0x{x} -> 0x{x}\n", .{ i, map.header.data_addr[i], map.header.data_off[i] }); + try fifo.pump(&limitedReader, writer); + } +} + fn checkELFHeader(header: ELFHeader) !void { // Check magic if (!std.mem.eql(u8, header.e_ident[0..4], ELFMagic)) { diff --git a/src/main.zig b/src/main.zig index 2977590..7c5fdfc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -18,11 +18,17 @@ pub fn main() !void { const inputPath = args[1]; const outputPath = args[2]; - _ = outputPath; // autofix // Read input const input = try std.fs.cwd().openFile(inputPath, .{}); - + defer input.close(); var map = try elf.readELF(input); + + // Align segments to 64 byte boundaries elf.alignSegments(&map); + + // Write header and copy over segments from input + const output = try std.fs.cwd().createFile(outputPath, .{}); + defer output.close(); + try elf.writeDOL(map, input, output); } diff --git a/src/oldzig.zig b/src/oldzig.zig new file mode 100644 index 0000000..2314b55 --- /dev/null +++ b/src/oldzig.zig @@ -0,0 +1,13 @@ +const native_endian = @import("builtin").target.cpu.arch.endian(); +const std = @import("std"); + +// This is in nightly but they haven't been making builds for a while +pub fn writeStructEndian(writer: anytype, value: anytype, endian: std.builtin.Endian) anyerror!void { + if (native_endian == endian) { + return writer.writeStruct(value); + } else { + var copy = value; + std.mem.byteSwapAllFields(@TypeOf(value), ©); + return writer.writeStruct(copy); + } +}