aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--elf/header.lua14
-rw-r--r--elf/test.lua111
2 files changed, 114 insertions, 11 deletions
diff --git a/elf/header.lua b/elf/header.lua
index 179e3bd..751ff9a 100644
--- a/elf/header.lua
+++ b/elf/header.lua
@@ -90,15 +90,17 @@ end
---@return string
---@nodiscard
function M.pack(hdr)
- return string.pack(
- "BBBBBBBBBxxxxxxx" .. fmt.endian(hdr.data) .. "I2I2I4"
- .. string.rep(fmt.word(hdr.class), 3) .. "I4I2I2I2I2I2I2",
- hdr.magic[1], hdr.magic[2], hdr.magic[3], hdr.magic[4], hdr.class,
+ return string.pack(table.concat({
+ "BBBBBBBBBxxxxxxx",
+ fmt.endian(hdr.data),
+ "I2I2I4",
+ string.rep(fmt.word(hdr.class), 3),
+ "I4I2I2I2I2I2I2",
+ }), hdr.magic[1], hdr.magic[2], hdr.magic[3], hdr.magic[4], hdr.class,
hdr.data, hdr.ident_version, hdr.osabi, hdr.abiversion, hdr.type,
hdr.machine, hdr.version, hdr.entry, hdr.phoff, hdr.shoff, hdr.flags,
hdr.ehsize, hdr.phentsize, hdr.phnum, hdr.shentsize, hdr.shnum,
- hdr.shstrndx
- )
+ hdr.shstrndx)
end
return M
diff --git a/elf/test.lua b/elf/test.lua
index 000d795..a0f7a38 100644
--- a/elf/test.lua
+++ b/elf/test.lua
@@ -1,4 +1,5 @@
local fmt = require("elf.fmt")
+local elf_header = require("elf.header")
local ec = require("elf.constants")
local nfail = 0
@@ -10,6 +11,7 @@ local function test(name, fn)
end
end
+-- Test fmt.lua
test("fmt.endian big", function ()
assert(fmt.endian(ec.Endian.big) == ">", "invalid format string")
end)
@@ -18,6 +20,12 @@ test("fmt.endian little", function ()
assert(fmt.endian(ec.Endian.little) == "<", "invalid format string")
end)
+test("fmt.endian", function ()
+ ---@diagnostic disable-next-line: param-type-mismatch
+ local ok = pcall(function () fmt.endian(42) end)
+ assert(not ok, "invalid input accepted")
+end)
+
test("fmt.word 64", function ()
assert(fmt.word(ec.Class.bit64) == "I8", "invalid format string")
end)
@@ -26,16 +34,109 @@ test("fmt.word 32", function ()
assert(fmt.word(ec.Class.bit32) == "I4", "invalid format string")
end)
-test("fmt.endian", function ()
- local ok = pcall(function () fmt.endian(42) end)
- assert(not ok, "invalid input accepted")
-end)
-
test("fmt.word", function ()
+ ---@diagnostic disable-next-line: param-type-mismatch
local ok = pcall(function () fmt.word(42) end)
assert(not ok, "invalid input accepted")
end)
+-- Test header.lua
+test("header.normalize ELF64 defaults", function ()
+ local h = elf_header.normalize({
+ class = ec.Class.bit64,
+ data = ec.Endian.little,
+ type = ec.Type.exec,
+ machine = ec.Machine.x86_64,
+ })
+
+ assert(h.magic[1] == 0x7f, "magic[1] mismatch")
+ assert(h.magic[2] == 0x45, "magic[2] mismatch")
+ assert(h.magic[3] == 0x4c, "magic[3] mismatch")
+ assert(h.magic[4] == 0x46, "magic[4] mismatch")
+ assert(h.class == ec.Class.bit64, "class mismatch")
+ assert(h.data == ec.Endian.little, "data mismatch")
+ assert(h.type == ec.Type.exec, "type mismatch")
+ assert(h.machine == ec.Machine.x86_64, "machine mismatch")
+ assert(h.ident_version == ec.VERSION, "ident_version mismatch")
+ assert(h.osabi == ec.Osabi.sysv, "osabi mismatch")
+ assert(h.abiversion == 0, "abiversion mismatch")
+ assert(h.version == ec.VERSION, "version mismatch")
+ assert(h.entry == 0, "entry mismatch")
+ assert(h.phoff == 0x40, "phoff mismatch")
+ assert(h.shoff == 0, "shoff mismatch")
+ assert(h.flags == 0, "flags mismatch")
+ assert(h.ehsize == 0x40, "ehsize mismatch")
+ assert(h.phentsize == 0x38, "phentsize mismatch")
+ assert(h.phnum == 0, "phnum mismatch")
+ assert(h.shentsize == 0x40, "shentsize mismatch")
+ assert(h.shnum == 0, "shnum mismatch")
+ assert(h.shstrndx == 0, "shstrndx mismatch")
+end)
+
+test("header.normalize preserve specified values", function ()
+ local h = elf_header.normalize({
+ class = ec.Class.bit64,
+ data = ec.Endian.little,
+ type = ec.Type.exec,
+ machine = ec.Machine.x86_64,
+ entry = 0x401000,
+ flags = 3,
+ phnum = 2,
+ shnum = 4,
+ shstrndx = 3,
+ })
+ assert(h.entry == 0x401000, "entry mismatch")
+ assert(h.flags == 3, "flags mismatch")
+ assert(h.phnum == 2, "phnum mismatch")
+ assert(h.shnum == 4, "shnum mismatch")
+ assert(h.shstrndx == 3, "shstrndx mismatch")
+ -- Layout defaults remain ELF64 values.
+ assert(h.phoff == 0x40, "phoff mismatch")
+ assert(h.ehsize == 0x40, "ehsize mismatch")
+ assert(h.phentsize == 0x38, "phentsize mismatch")
+ assert(h.shentsize == 0x40, "shentsize mismatch")
+end)
+
+test("header.normalize ELF32 layout", function ()
+ local h = elf_header.normalize({
+ class = ec.Class.bit32,
+ data = ec.Endian.little,
+ type = ec.Type.exec,
+ machine = ec.Machine.i386,
+ })
+ assert(h.phoff == 0x34, "phoff mismatch")
+ assert(h.ehsize == 0x34, "ehsize mismatch")
+ assert(h.phentsize == 0x20, "phentsize mismatch")
+ assert(h.shentsize == 0x28, "shentsize mismatch")
+end)
+
+local function hex_to_bytes(hex)
+ return (hex:gsub("..", function (pair)
+ local byte = assert(tonumber(pair, 16), "Invalid hex")
+ return string.char(byte)
+ end))
+end
+
+test("header.pack 64_le_x86-64", function ()
+ local header = {
+ class = ec.Class.bit64,
+ data = ec.Endian.little,
+ type = ec.Type.exec,
+ machine = ec.Machine.x86_64,
+ }
+ local expected = hex_to_bytes(table.concat({
+ "7f454c46020101000000000000000000",
+ "02003e0001000000",
+ "000000000000000040000000000000000000000000000000",
+ "00000000400038000000400000000000",
+ }))
+ assert(elf_header.pack(elf_header.normalize(header)) == expected, "mismatch")
+end)
+
+-- Test segment.lua
+-- Test section.lua
+-- Test layout.lua
+
if nfail > 0 then
print(("%d %s failed"):format(nfail, (nfail == 1) and "test" or "tests"))
os.exit(1)