local elf = require("elf") local ec = require("elf.constants") local function die(fmt, ...) io.stderr:write("mkelf: ", string.format(fmt, ...), "\n") os.exit(1, true) end local function usage() die("[-o output] [input]") end local function setup_header(state) return function (t) if type(t) ~= "table" then error("header must be a table", 2) end if state.hopts == nil then state.hopts = t else error("multiple headers are disallowed", 2) end end end local function setup_segments(state) return function (t) if type(t) ~= "table" then error("segments must be a table", 2) end for i, s in ipairs(t) do if type(s) ~= "table" then error(("segment #%d must be a table"):format(i), 2) end table.insert(state.sgopts, s) end end end local function setup_sections(state) return function (t) if type(t) ~= "table" then error("sections must be a table", 2) end for i, s in ipairs(t) do if type(s) ~= "table" then error(("section #%d must be a table"):format(i), 2) end table.insert(state.scopts, s) end end end local function setup_env(state) return setmetatable({}, { __index = { Class = ec.Class, Endian = ec.Endian, Osabi = ec.Osabi, Type = ec.Type, Machine = ec.Machine, SegmentType = ec.SegmentType, SegmentFlags = ec.SegmentFlags, SectionType = ec.SectionType, SectionFlags = ec.SectionFlags, ELFVERSION = ec.VERSION, header = setup_header(state), segments = setup_segments(state), sections = setup_sections(state), }, __newindex = function (_, name) error(("may not define global %q"):format(name), 2) end, __metatable = false, }) end ---@param fn string | nil ---@return HeaderOptions, SegmentOptions[], SectionOptions[] local function loadconfig(fn) local state = { ---@type HeaderOptions? hopts = nil, sgopts = {}, scopts = {}, } local env = setup_env(state) local chunk, err = loadfile(fn, "t", env) if not chunk then die("%s", err) end -- TODO sandbox against malformed configs local ok, perr = pcall(chunk) if not ok then die("%s", perr) end 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 ---@param segments Segment[] ---@param sections Section[] ---@return string local function elf_pack(header, segments, sections) local ok, b = pcall(elf.header.pack, header) if not ok then error(("header: %s"):format(b), 0) end local parts = { b } for i, sg in ipairs(segments) do local sgok, sgb = pcall(elf.segment.pack, sg, header.data, header.class) if not sgok then error(("segment #%d: %s"):format(i, sgb), 0) end parts[#parts + 1] = sgb end for i, sc in ipairs(sections) do local scok, scb = pcall(elf.section.pack, sc, header.data, header.class) if not scok then error(("section #%d: %s"):format(i, scb), 0) end parts[#parts + 1] = scb end return table.concat(parts) end local M = {} function M.main() local outfn = "a.out" local infn local i = 1 while arg[i] do if arg[i] == "-o" then i = i + 1 outfn = arg[i] or die("missing filename after -o") elseif infn == nil then infn = arg[i] else usage() end i = i + 1 end 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") if out == nil then die("failed to open: %s", err) end local wok, werr = out:write(binary) if not wok then die("write failed: %s", werr) end end return M