aboutsummaryrefslogtreecommitdiff
path: root/elf/header.lua
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2026-11-04 11:11:32 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2026-11-04 11:11:32 +0530
commitf45ff5c60b7464dc250671d58c969a6320aad9b6 (patch)
tree11c01905bf576df18fee42fc5fe7a97f66bda286 /elf/header.lua
parent96e49359ded717d7e3953875a50e59eb06a5233e (diff)
Refactor normalize() to prevent in-place mutation.
We would need to distinguish user configuration options from normalization. Let normalize() return a new table instead of in-place update. Split annotations into xxxOptions and xxx for header, segments and sections, delineating optional vs normalized table. Leave header, segments and sections as just a data table for now and stop trying to make them objects. Leave data alone.
Diffstat (limited to 'elf/header.lua')
-rw-r--r--elf/header.lua153
1 files changed, 82 insertions, 71 deletions
diff --git a/elf/header.lua b/elf/header.lua
index a5fc78a..7251d99 100644
--- a/elf/header.lua
+++ b/elf/header.lua
@@ -3,91 +3,102 @@ local ec = require("elf.constants")
---@alias Magic [integer, integer, integer, integer]
----@class Header
----@field magic? Magic
----@field class Class
----@field data Endian
+---@class HeaderOptions
+---@field magic? Magic
+---@field class Class
+---@field data Endian
---@field ident_version? integer
----@field osabi? Osabi
----@field abiversion? integer
----@field type Type
----@field machine Machine
----@field version? integer
----@field entry? integer
----@field phoff? integer
----@field shoff? integer
----@field flags? integer
----@field ehsize? integer
----@field phentsize? integer
----@field phnum? integer
----@field shentsize? integer
----@field shnum? integer
----@field shstrndx? integer
-local M = {}
+---@field osabi? Osabi
+---@field abiversion? integer
+---@field type Type
+---@field machine Machine
+---@field version? integer
+---@field entry? integer
+---@field phoff? integer
+---@field shoff? integer
+---@field flags? integer
+---@field ehsize? integer
+---@field phentsize? integer
+---@field phnum? integer
+---@field shentsize? integer
+---@field shnum? integer
+---@field shstrndx? integer
-function M.normalize(hdr)
- if not hdr.class then error("class is required in header", 3) end
- if not hdr.data then error("data is required in header", 3) end
- if not hdr.type then error("type is required in header", 3) end
- if not hdr.machine then error("machine is required in header", 3) end
+---@class Header
+---@field magic Magic
+---@field class Class
+---@field data Endian
+---@field ident_version integer
+---@field osabi Osabi
+---@field abiversion integer
+---@field type Type
+---@field machine Machine
+---@field version integer
+---@field entry integer
+---@field phoff integer
+---@field shoff integer
+---@field flags integer
+---@field ehsize integer
+---@field phentsize integer
+---@field phnum integer
+---@field shentsize integer
+---@field shnum integer
+---@field shstrndx integer
- hdr.magic = hdr.magic or { 0x7f, 0x45, 0x4c, 0x46 }
- hdr.ident_version = hdr.ident_version or ec.VERSION
- hdr.osabi = hdr.osabi or ec.Osabi.sysv
- hdr.abiversion = hdr.abiversion or 0
- hdr.version = hdr.version or ec.VERSION
- hdr.entry = hdr.entry or 0
- hdr.phoff = hdr.phoff or ((hdr.class == ec.Class.bit32) and 0x34 or 0x40)
- hdr.shoff = hdr.shoff or 0
- hdr.flags = hdr.flags or 0
- hdr.ehsize = hdr.ehsize or ((hdr.class == ec.Class.bit32) and 0x34 or 0x40)
- hdr.phentsize = hdr.phentsize or
- ((hdr.class == ec.Class.bit32) and 0x20 or 0x38)
- hdr.phnum = hdr.phnum or 0
- hdr.shentsize = hdr.shentsize or
- ((hdr.class == ec.Class.bit32) and 0x28 or 0x40)
- hdr.shnum = hdr.shnum or 0
- hdr.shstrndx = hdr.shstrndx or 0
-end
+local M = {}
+
+---@param hdropts HeaderOptions
+---@return Header
+function M.normalize(hdropts)
+ if not hdropts.class then error("class is required in header", 3) end
+ if not hdropts.data then error("data is required in header", 3) end
+ if not hdropts.type then error("type is required in header", 3) end
+ if not hdropts.machine then error("machine is required in header", 3) end
-function M.validate(hdr)
- if hdr.class ~= ec.Class.bit32 and hdr.class ~= ec.Class.bit64 then
+ -- Can't pack without these values being valid.
+ if hdropts.class ~= ec.Class.bit32 and hdropts.class ~= ec.Class.bit64 then
error("class must be Class.bit32 or Class.bit64 in header", 3)
end
- if hdr.data ~= ec.Endian.little and hdr.data ~= ec.Endian.big then
+ if hdropts.data ~= ec.Endian.little and hdropts.data ~= ec.Endian.big then
error("data must be Endian.little or Endian.big in header", 3)
end
- -- TODO additional validations
+
+ return {
+ magic = hdropts.magic or { 0x7f, 0x45, 0x4c, 0x46 },
+ class = hdropts.class,
+ data = hdropts.data,
+ ident_version = hdropts.ident_version or ec.VERSION,
+ osabi = hdropts.osabi or ec.Osabi.sysv,
+ abiversion = hdropts.abiversion or 0,
+ type = hdropts.type,
+ machine = hdropts.machine,
+ version = hdropts.version or ec.VERSION,
+ entry = hdropts.entry or 0,
+ shoff = hdropts.shoff or 0,
+ flags = hdropts.flags or 0,
+ phnum = hdropts.phnum or 0,
+ shnum = hdropts.shnum or 0,
+ shstrndx = hdropts.shstrndx or 0,
+ phoff = (hdropts.class == ec.Class.bit32) and 0x34 or 0x40,
+ ehsize = (hdropts.class == ec.Class.bit32) and 0x34 or 0x40,
+ phentsize = (hdropts.class == ec.Class.bit32) and 0x20 or 0x38,
+ shentsize = (hdropts.class == ec.Class.bit32) and 0x28 or 0x40
+ }
end
---@param hdr Header
---@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,
- 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)
+ 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,
+ 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
+ )
end
return M