aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x[-rw-r--r--]mkelf.lua95
1 files changed, 52 insertions, 43 deletions
diff --git a/mkelf.lua b/mkelf.lua
index f348ef9..40a1415 100644..100755
--- a/mkelf.lua
+++ b/mkelf.lua
@@ -10,16 +10,49 @@ local function usage()
die("[-o output] [input]")
end
----@param fn string | nil
----@return Header, Segment[], Section[]
-local function loadconfig(fn)
- ---@type Header
- local header
- ---@type Segment[]
- local segments = {}
- ---@type Section[]
- local sections = {}
- local env = setmetatable({}, {
+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,
@@ -31,50 +64,26 @@ local function loadconfig(fn)
SectionType = ec.SectionType,
SectionFlags = ec.SectionFlags,
ELFVERSION = ec.VERSION,
-
- header = function (t)
- if type(t) ~= "table" then
- error("header must be a table", 2)
- end
- if header ~= nil then
- error("multiple headers are disallowed", 2)
- end
- header = elf.header.normalize(t)
- end,
- segments = 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(segments, elf.segment.normalize(s))
- end
- end,
- sections = 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(sections, elf.section.normalize(s))
- end
- end,
+ 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 header, segments, sections
+ return state.header, state.segments, state.sections
end
local function main()