diff options
| -rw-r--r-- | elf/test.lua | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/elf/test.lua b/elf/test.lua index 2955d20..e49c828 100644 --- a/elf/test.lua +++ b/elf/test.lua @@ -337,6 +337,120 @@ test("segment.normalize preserve zero values", function () assert(s.align == 0, "align mismatch") end) +test("segment.pack 32_le", function () + local s = { + type = ec.SegmentType.dynamic, + flags = 0x11121314, + offset = 0x21222324, + vaddr = 0x31323334, + paddr = 0x41424344, + filesz = 0x51525354, + memsz = 0x61626364, + align = 0x71727374, + } + local expected = hex_to_bytes(table.concat({ + -- string.rep(fmt.word(ec.Class.bit32), 8) + "02000000", -- type + "24232221", -- offset + "34333231", -- vaddr + "44434241", -- paddr + "54535251", -- filesz + "64636261", -- memsz + "14131211", -- flags + "74737271", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.little, ec.Class.bit32) + assert(#actual == 32, "invalid ELF32 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 32_be", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x21222324, + vaddr = 0x31323334, + paddr = 0x41424344, + filesz = 0x51525354, + memsz = 0x61626364, + align = 0x71727374, + } + local expected = hex_to_bytes(table.concat({ + -- string.rep(fmt.word(ec.Class.bit32), 8) where I4 is selected + "01020304", -- type + "21222324", -- offset + "31323334", -- vaddr + "41424344", -- paddr + "51525354", -- filesz + "61626364", -- memsz + "11121314", -- flags + "71727374", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.big, ec.Class.bit32) + assert(#actual == 32, "invalid ELF32 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 64_le", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x2122232425262728, + vaddr = 0x3132333435363738, + paddr = 0x4142434445464748, + filesz = 0x5152535455565758, + memsz = 0x6162636465666768, + align = 0x7172737475767778, + } + local expected = hex_to_bytes(table.concat({ + -- "I4I4" + "04030201", -- type + "14131211", -- flags + -- string.rep(fmt.word(ec.Class.bit64), 6) where I8 is selected + "2827262524232221", -- offset + "3837363534333231", -- vaddr + "4847464544434241", -- paddr + "5857565554535251", -- filesz + "6867666564636261", -- memsz + "7877767574737271", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.little, ec.Class.bit64) + assert(#actual == 56, "invalid ELF64 segment header size") + assert(actual == expected, "mismatch") +end) + +test("segment.pack 64_be", function () + local s = { + type = 0x01020304, + flags = 0x11121314, + offset = 0x2122232425262728, + vaddr = 0x3132333435363738, + paddr = 0x4142434445464748, + filesz = 0x5152535455565758, + memsz = 0x6162636465666768, + align = 0x7172737475767778, + } + local expected = hex_to_bytes(table.concat({ + -- "I4I4" + "01020304", -- type + "11121314", -- flags + -- string.rep(fmt.word(ec.Class.bit64), 6) where I8 is selected + "2122232425262728", -- offset + "3132333435363738", -- vaddr + "4142434445464748", -- paddr + "5152535455565758", -- filesz + "6162636465666768", -- memsz + "7172737475767778", -- align + })) + local ns = elf_segment.normalize(s) + local actual = elf_segment.pack(ns, ec.Endian.big, ec.Class.bit64) + assert(#actual == 56, "invalid ELF64 segment header size") + assert(actual == expected, "mismatch") +end) + -- Test section.lua -- Test layout.lua |
