From 51c97bd5d40ea84e44c26b6fcf5d93edc957a020 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Wed, 4 Nov 2026 19:01:07 +0530 Subject: emmylua_ls has trouble working without the suffix. --- mkelf | 121 -------------------------------------------------------------- mkelf.lua | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 121 deletions(-) delete mode 100755 mkelf create mode 100644 mkelf.lua diff --git a/mkelf b/mkelf deleted file mode 100755 index 5c8eb2c..0000000 --- a/mkelf +++ /dev/null @@ -1,121 +0,0 @@ -#! /usr/bin/env lua -local elf = require("elf") -local ec = require("elf.constants") - -local function die(fmt, ...) - io.stderr:write("mkelf: ", string.format(fmt, ...), "\n") - os.exit(1) -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.header == nil then - state.header = elf.header.normalize(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.segments, elf.segment.normalize(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.sections, elf.section.normalize(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 - -local function loadconfig(fn) - local state = { header, segments = {}, sections = {} } - 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 - return state.header, state.segments, state.sections -end - -local function 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 = loadconfig(infn) - if not header then die("header missing") end - -- TODO check against headerOptions not normalized header. - if header.phnum == 0 then header.phnum = #segments end - if header.shnum == 0 then header.shnum = #sections end - local out = io.open(outfn, "wb") - if out == nil then die("failed to open %s", outfn) end - out:write(elf.header.pack(header)) - for _, segment in ipairs(segments) do - out:write(elf.segment.pack(segment, header.data, header.class)) - end - for _, section in ipairs(sections) do - out:write(elf.section.pack(section, header.data, header.class)) - end -end - -main() diff --git a/mkelf.lua b/mkelf.lua new file mode 100644 index 0000000..2bbc2da --- /dev/null +++ b/mkelf.lua @@ -0,0 +1,120 @@ +local elf = require("elf") +local ec = require("elf.constants") + +local function die(fmt, ...) + io.stderr:write("mkelf: ", string.format(fmt, ...), "\n") + os.exit(1) +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.header == nil then + state.header = elf.header.normalize(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.segments, elf.segment.normalize(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.sections, elf.section.normalize(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 + +local function loadconfig(fn) + local state = { header, segments = {}, sections = {} } + 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 + return state.header, state.segments, state.sections +end + +local function 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 = loadconfig(infn) + if not header then die("header missing") end + -- TODO check against headerOptions not normalized header. + if header.phnum == 0 then header.phnum = #segments end + if header.shnum == 0 then header.shnum = #sections end + local out = io.open(outfn, "wb") + if out == nil then die("failed to open %s", outfn) end + out:write(elf.header.pack(header)) + for _, segment in ipairs(segments) do + out:write(elf.segment.pack(segment, header.data, header.class)) + end + for _, section in ipairs(sections) do + out:write(elf.section.pack(section, header.data, header.class)) + end +end + +main() -- cgit v1.3