aboutsummaryrefslogtreecommitdiff
path: root/mkelf.lua
diff options
context:
space:
mode:
Diffstat (limited to 'mkelf.lua')
-rw-r--r--mkelf.lua48
1 files changed, 21 insertions, 27 deletions
diff --git a/mkelf.lua b/mkelf.lua
index cd02626..d77ac0f 100644
--- a/mkelf.lua
+++ b/mkelf.lua
@@ -10,13 +10,15 @@ local function usage()
die("[-o output] [input]")
end
----@param fn string|nil
+---@param fn string | nil
---@return Header, Segment[], Section[]
local function loadconfig(fn)
- local header, segments, sections = {}, {}, {}
- local header_mt = { __index = elf.header }
- local segment_mt = { __index = elf.segment }
- local section_mt = { __index = elf.section }
+ ---@type Header
+ local header
+ ---@type Segment[]
+ local segments = {}
+ ---@type Section[]
+ local sections = {}
local env = setmetatable({}, {
__index = {
Class = ec.Class,
@@ -30,44 +32,36 @@ local function loadconfig(fn)
SectionFlags = ec.SectionFlags,
ELFVERSION = ec.VERSION,
- header = function(t)
+ header = function (t)
if type(t) ~= "table" then
error("header must be a table", 2)
end
- header = setmetatable(t, header_mt)
- t:normalize()
- t:validate()
+ header = elf.header.normalize(t)
end,
- segments = function(t)
+ segments = function (t)
if type(t) ~= "table" then
error("segments must be a table", 2)
end
- for i, segment in ipairs(t) do
- if type(segment) ~= "table" then
+ for i, s in ipairs(t) do
+ if type(s) ~= "table" then
error(("segment #%d must be a table"):format(i), 2)
end
- segment = setmetatable(segment, segment_mt)
- segment:normalize()
- segment:validate()
+ table.insert(segments, elf.segment.normalize(s))
end
- segments = t
end,
- sections = function(t)
+ sections = function (t)
if type(t) ~= "table" then
error("sections must be a table", 2)
end
- for i, section in ipairs(t) do
- if type(section) ~= "table" then
+ for i, s in ipairs(t) do
+ if type(s) ~= "table" then
error(("section #%d must be a table"):format(i), 2)
end
- section = setmetatable(section, section_mt)
- section:normalize()
- section:validate()
+ table.insert(sections, elf.section.normalize(s))
end
- sections = t
end,
},
- __newindex = function(_, name)
+ __newindex = function (_, name)
error(("may not define global %q"):format(name), 2)
end,
__metatable = false,
@@ -101,12 +95,12 @@ local function main()
if header.phnum == 0 then header.phnum = #segments end
if header.shnum == 0 then header.shnum = #sections end
local out <close> = assert(io.open(outfn, "wb"))
- out:write(header:pack())
+ out:write(elf.header.pack(header))
for _, segment in ipairs(segments) do
- out:write(segment:pack(header.data, header.class))
+ out:write(elf.segment.pack(segment, header.data, header.class))
end
for _, section in ipairs(sections) do
- out:write(section:pack(header.data, header.class))
+ out:write(elf.section.pack(section, header.data, header.class))
end
end