From 3e36380874aa40932fa629fc4f53bdd50c4f2311 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Sat, 24 Oct 2026 11:21:21 +0530 Subject: Attach the respective metatables to loaded the config. This simplifies calling pack() on respective tables. --- mkelf.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'mkelf.lua') diff --git a/mkelf.lua b/mkelf.lua index 6edeebb..7dd32c6 100644 --- a/mkelf.lua +++ b/mkelf.lua @@ -11,9 +11,12 @@ local function usage() end ---@param fn string|nil ----@return Header, Segment[], Section[] +---@return Header, Segments, Sections 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 env = { -- Keep in sync with types/elf.d.lua Class = elf.Class, @@ -25,9 +28,9 @@ local function loadconfig(fn) SegmentFlags = elf.SegmentFlags, ELFVERSION = elf.VERSION, - header = function(t) header = t end, - segments = function(t) segments = t end, - sections = function(t) sections = t end, + header = function(t) header = setmetatable(t, header_mt) end, + segments = function(t) segments = setmetatable(t, segments_mt) end, + sections = function(t) sections = setmetatable(t, sections_mt) end, } -- TODO error check for multiple headers local chunk, err = loadfile(fn, "t", env) @@ -56,10 +59,10 @@ local function main() header.phentsize = (header.class == ec.Class.bit64) and 0x38 or 0x20 -- TODO write to a temp file and atomic rename to outfn. local out = assert(io.open(outfn, "wb")) - out:write(elf.header.pack(header)) - out:write(elf.segment.pack(segments, header.data, header.class)) + out:write(header:pack()) + out:write(segments:pack(header.data, header.class)) -- TODO finish section.pack - -- out:write(elf.section.pack(c.sections, c.header.data)) + out:write(sections:pack(header.data)) end main() -- cgit v1.3