This commit is contained in:
Hamcha 2024-05-05 21:29:09 +02:00
parent 882f02bfd0
commit 7d3604957e
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
4 changed files with 33 additions and 1 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
zig-cache zig-cache
zig-out zig-out
*.elf *.elf
*.dol *.dol
!testdata/*.elf

View file

@ -23,4 +23,16 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
// Testing
const elf_unit_tests = b.addTest(.{
.root_source_file = b.path("src/elf.zig"),
.target = target,
.optimize = optimize,
});
const run_elf_unit_tests = b.addRunArtifact(elf_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_elf_unit_tests.step);
} }

View file

@ -255,3 +255,22 @@ fn checkELFHeader(header: ELFHeader) !void {
return ELFError.NoEntrypoint; return ELFError.NoEntrypoint;
} }
} }
test "readELF: parses known .elf" {
const input = try std.fs.cwd().openFile("testdata/example.elf", .{});
defer input.close();
const map = try readELF(input);
try std.testing.expectEqual(0x80003100, map.entryPoint);
try std.testing.expectEqual(1, map.textCount);
try std.testing.expectEqual(1, map.dataCount);
try std.testing.expectEqual(0x80003100, map.text[0].address);
try std.testing.expectEqual(0x2DD0, map.text[0].size);
try std.testing.expectEqual(0x3100, map.text[0].offset);
try std.testing.expectEqual(0x80005ed0, map.data[0].address);
try std.testing.expectEqual(0xD0, map.data[0].size);
try std.testing.expectEqual(0x5ed0, map.data[0].offset);
try std.testing.expectEqual(true, map.hasBSS);
try std.testing.expectEqual(0x80005fa0, map.bssAddress);
try std.testing.expectEqual(0x160, map.bssSize);
}

BIN
testdata/example.elf vendored Normal file

Binary file not shown.