diff options
| author | Sunil Nimmagadda <sunil@nimmagadda.net> | 2026-11-04 11:11:32 +0530 |
|---|---|---|
| committer | Sunil Nimmagadda <sunil@nimmagadda.net> | 2026-11-04 11:11:32 +0530 |
| commit | f45ff5c60b7464dc250671d58c969a6320aad9b6 (patch) | |
| tree | 11c01905bf576df18fee42fc5fe7a97f66bda286 /mkelf.lua | |
| parent | 96e49359ded717d7e3953875a50e59eb06a5233e (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 'mkelf.lua')
| -rw-r--r-- | mkelf.lua | 48 |
1 files changed, 21 insertions, 27 deletions
@@ -10,13 +10,15 @@ local function usage() die("[-o output] [input]") end ----@param fn string|nil +---@param fn string | nil ---@return Header, Segment[], Section[] local function loadconfig(fn) - local header, segments, sections = {}, {}, {} - local header_mt = { __index = elf.header } - local segment_mt = { __index = elf.segment } - local section_mt = { __index = elf.section } + ---@type Header + local header + ---@type Segment[] + local segments = {} + ---@type Section[] + local sections = {} local env = setmetatable({}, { __index = { Class = ec.Class, @@ -30,44 +32,36 @@ local function loadconfig(fn) SectionFlags = ec.SectionFlags, ELFVERSION = ec.VERSION, - header = function(t) + header = function (t) if type(t) ~= "table" then error("header must be a table", 2) end - header = setmetatable(t, header_mt) - t:normalize() - t:validate() + header = elf.header.normalize(t) end, - segments = function(t) + segments = function (t) if type(t) ~= "table" then error("segments must be a table", 2) end - for i, segment in ipairs(t) do - if type(segment) ~= "table" then + for i, s in ipairs(t) do + if type(s) ~= "table" then error(("segment #%d must be a table"):format(i), 2) end - segment = setmetatable(segment, segment_mt) - segment:normalize() - segment:validate() + table.insert(segments, elf.segment.normalize(s)) end - segments = t end, - sections = function(t) + sections = function (t) if type(t) ~= "table" then error("sections must be a table", 2) end - for i, section in ipairs(t) do - if type(section) ~= "table" then + for i, s in ipairs(t) do + if type(s) ~= "table" then error(("section #%d must be a table"):format(i), 2) end - section = setmetatable(section, section_mt) - section:normalize() - section:validate() + table.insert(sections, elf.section.normalize(s)) end - sections = t end, }, - __newindex = function(_, name) + __newindex = function (_, name) error(("may not define global %q"):format(name), 2) end, __metatable = false, @@ -101,12 +95,12 @@ local function main() if header.phnum == 0 then header.phnum = #segments end if header.shnum == 0 then header.shnum = #sections end local out <close> = assert(io.open(outfn, "wb")) - out:write(header:pack()) + out:write(elf.header.pack(header)) for _, segment in ipairs(segments) do - out:write(segment:pack(header.data, header.class)) + out:write(elf.segment.pack(segment, header.data, header.class)) end for _, section in ipairs(sections) do - out:write(section:pack(header.data, header.class)) + out:write(elf.section.pack(section, header.data, header.class)) end end |
