From 6f83d9020a1788b0fa1d1b1fac658f2c8d704bd9 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Thu, 5 Nov 2026 23:01:53 +0530 Subject: layout scaffolding. Make room for layout calculations. Prioritize user specified options first for layout calculations so that layout gets first hand decision if an option was specified or missing. This makes current normalize() just a dumb defaults setter that are not layout calculated or fell through. --- mkelf.lua | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'mkelf.lua') diff --git a/mkelf.lua b/mkelf.lua index 201fb7a..f4dd67d 100644 --- a/mkelf.lua +++ b/mkelf.lua @@ -15,8 +15,8 @@ local function setup_header(state) if type(t) ~= "table" then error("header must be a table", 2) end - if state.header == nil then - state.header = elf.header.normalize(t) + if state.hopts == nil then + state.hopts = t else error("multiple headers are disallowed", 2) end @@ -32,7 +32,7 @@ local function setup_segments(state) if type(s) ~= "table" then error(("segment #%d must be a table"):format(i), 2) end - table.insert(state.segments, elf.segment.normalize(s)) + table.insert(state.sgopts, s) end end end @@ -46,7 +46,7 @@ local function setup_sections(state) if type(s) ~= "table" then error(("section #%d must be a table"):format(i), 2) end - table.insert(state.sections, elf.section.normalize(s)) + table.insert(state.scopts, s) end end end @@ -76,13 +76,13 @@ local function setup_env(state) end ---@param fn string | nil ----@return Header, Segment[], Section[] +---@return HeaderOptions, SegmentOptions[], SectionOptions[] local function loadconfig(fn) local state = { - ---@type Header? - header = nil, - segments = {}, - sections = {}, + ---@type HeaderOptions? + hopts = nil, + sgopts = {}, + scopts = {}, } local env = setup_env(state) local chunk, err = loadfile(fn, "t", env) @@ -90,8 +90,30 @@ local function loadconfig(fn) -- TODO sandbox against malformed configs local ok, perr = pcall(chunk) if not ok then die("%s", perr) end - if not state.header then die("header missing") end - return state.header, state.segments, state.sections + if not state.hopts then die("header missing") end + return state.hopts, state.sgopts, state.scopts +end + +---@param hopts HeaderOptions +---@param sgopts SegmentOptions[] +---@param scopts SectionOptions[] +---@return Header, Segment[], Section[] +local function elf_layout(hopts, sgopts, scopts) + -- TODO layout calculations with user config as first preference. + ---@type Header? + local header = nil + ---@type Segment[] + local segments = {} + ---@type Section[] + local sections = {} + header = elf.header.normalize(hopts) + for _, sgopt in ipairs(sgopts) do + table.insert(segments, elf.segment.normalize(sgopt)) + end + for _, scopt in ipairs(scopts) do + table.insert(sections, elf.section.normalize(scopt)) + end + return header, segments, sections end ---@param header Header @@ -132,7 +154,7 @@ function M.main() end i = i + 1 end - local header, segments, sections = loadconfig(infn) + local header, segments, sections = elf_layout(loadconfig(infn)) local ok, binary = pcall(elf_pack, header, segments, sections) if not ok then die("packing failed: %s", binary) end local out , err = io.open(outfn, "wb") -- cgit v1.3