diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-05-13 16:29:45 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-05-13 16:29:45 +0800 |
| commit | 1ca36835b3bf2d029d9e56e96c1f77791daa4983 (patch) | |
| tree | 8bda61a6df51bafc1ff0b38a87da0ab10603a25b | |
| parent | fb4512afbec76e91cfaf8725d2d39a11c3969943 (diff) | |
| download | lume-1ca36835b3bf2d029d9e56e96c1f77791daa4983.tar.gz lume-1ca36835b3bf2d029d9e56e96c1f77791daa4983.tar.xz | |
feat: add self-test for relocation engine
| -rw-r--r-- | na/test/test_reloc.cc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/na/test/test_reloc.cc b/na/test/test_reloc.cc new file mode 100644 index 0000000..54e7144 --- /dev/null +++ b/na/test/test_reloc.cc @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +/* + * Boot Self-Test: relocation engine + */ + +#include <lume/relocate.h> +#include <lume/selftest.h> + +struct RelocBlob { + RelocTableHeader hdr; + RelocEntry entries[2]; +}; + +void selftest_reloc() +{ + st_begin("reloc: relative + abs64"); + + alignas(8) uint8 buf[64] = {}; + uint64 base = reinterpret_cast<uint64>(buf); + uint64 link_base = base - 0x1000; + + RelocBlob blob = {}; + blob.hdr.magic = kRelocTableMagic; + blob.hdr.version = kRelocTableVersion; + blob.hdr.arch = RELOC_ARCH_RISCV; + blob.hdr.entry_size = sizeof(RelocEntry); + blob.hdr.entry_count = 2; + + blob.entries[0].offset = 8; + blob.entries[0].addend = 0x200; + blob.entries[0].type = RELOC_RISCV_RELATIVE; + + blob.entries[1].offset = 16; + blob.entries[1].addend = link_base + 0x1234; + blob.entries[1].type = RELOC_RISCV_64; + + RelocStatus st = relocate_apply(base, sizeof(buf), link_base, + &blob, sizeof(blob)); + ST_ASSERT_EQ(st, RELOC_OK); + ST_ASSERT_EQ(*reinterpret_cast<uint64*>(base + 8), base + 0x200); + ST_ASSERT_EQ(*reinterpret_cast<uint64*>(base + 16), base + 0x1234); + + st_pass(); +} |
