From cc5e2ae695cfe199044ef614b2dfc1991095bcc0 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Fri, 30 Oct 2026 11:35:52 +0530 Subject: Untangle segment vs segments. Let the elf module handle just a single segment/section while the wrapper does the iteration. --- mkelf.lua | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'mkelf.lua') diff --git a/mkelf.lua b/mkelf.lua index 518b765..97530b4 100644 --- a/mkelf.lua +++ b/mkelf.lua @@ -11,12 +11,12 @@ local function usage() end ---@param fn string|nil ----@return Header, Segments, Sections +---@return Header, Segment[], Section[] local function loadconfig(fn) local header, segments, sections = {}, {}, {} local header_mt = { __index = elf.header } - local segments_mt = { __index = elf.segment } - local sections_mt = { __index = elf.section } + local segment_mt = { __index = elf.segment } + local section_mt = { __index = elf.section } local env = setmetatable({}, { __index = { -- Keep in sync with types/elf.d.lua @@ -41,11 +41,22 @@ local function loadconfig(fn) if type(t) ~= "table" then error("segments must be a table", 2) end - segments = setmetatable(t, segments_mt) - t:normalize() - t:validate() + for _, segment in ipairs(t) do + segment = setmetatable(segment, segment_mt) + segment:normalize() + segment:validate() + end + end, + sections = function(t) + if type(t) ~= "table" then + error("sections must be a table", 2) + end + for _, section in ipairs(t) do + section = setmetatable(t, section_mt) + section:normalize() + section:validate() + end end, - sections = function(t) sections = setmetatable(t, sections_mt) end, }, __newindex = function(_, name) error(("may not define global %q"):format(name), 2) @@ -80,8 +91,12 @@ local function main() header.phentsize = (header.class == ec.Class.bit64) and 0x38 or 0x20 local out = assert(io.open(outfn, "wb")) out:write(header:pack()) - out:write(segments:pack(header.data, header.class)) - -- out:write(sections:pack(header.data, header.class)) + for _, segment in ipairs(segments) do + out:write(segment:pack(header.data, header.class)) + end + for _, section in ipairs(sections) do + out:write(section:pack(header.data, header.class)) + end end main() -- cgit v1.3