it fucking works lmao
This commit is contained in:
parent
29a6e5e858
commit
b8af859517
3 changed files with 66 additions and 2 deletions
45
src/elf.zig
45
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)) {
|
||||
|
|
10
src/main.zig
10
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);
|
||||
}
|
||||
|
|
13
src/oldzig.zig
Normal file
13
src/oldzig.zig
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue