aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2026-10-30 11:35:52 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2026-10-30 11:35:52 +0530
commitcc5e2ae695cfe199044ef614b2dfc1991095bcc0 (patch)
treea23da9366a49cd82e0cec425cbf5c55061e71ea4
parent7d496278cc9b36f3a593df137586204d3d766a3e (diff)
Untangle segment vs segments.
Let the elf module handle just a single segment/section while the wrapper does the iteration.
-rw-r--r--elf/section.lua48
-rw-r--r--elf/segment.lua43
-rw-r--r--mkelf.lua33
3 files changed, 65 insertions, 59 deletions
diff --git a/elf/section.lua b/elf/section.lua
index ca9953e..8f84b35 100644
--- a/elf/section.lua
+++ b/elf/section.lua
@@ -11,34 +11,36 @@ local fmt = require("elf.fmt")
---@field info? integer
---@field addralign? integer
---@field entsize? integer
-
----@class Sections
local M = {}
----@param sections Sections
+function M.normalize(section)
+ -- TODO
+end
+
+function M.validate(section)
+ --TODO
+end
+
+---@param section Section
---@param endian Endian
---@return string
---@nodiscard
-function M.pack(sections, endian, class)
- local s = {}
- for i, section in ipairs(sections) do
- s[i] = string.pack(fmt.endian(endian) ..
- "I4I4" ..
- string.rep(fmt.word(class), 4) ..
- "I4I4" ..
- string.rep(fmt.word(class), 2),
- section.name,
- section.type,
- section.flags,
- section.addr,
- section.offset,
- section.size,
- section.link,
- section.info,
- section.addralign,
- section.entsize)
- end
- return table.concat(s)
+function M.pack(section, endian, class)
+ return string.pack(fmt.endian(endian) ..
+ "I4I4" ..
+ string.rep(fmt.word(class), 4) ..
+ "I4I4" ..
+ string.rep(fmt.word(class), 2),
+ section.name,
+ section.type,
+ section.flags,
+ section.addr,
+ section.offset,
+ section.size,
+ section.link,
+ section.info,
+ section.addralign,
+ section.entsize)
end
return M
diff --git a/elf/segment.lua b/elf/segment.lua
index 6b3c635..8af9146 100644
--- a/elf/segment.lua
+++ b/elf/segment.lua
@@ -12,29 +12,23 @@ local fmt = require("elf.fmt")
---@field align? integer
---@field first_section? string
---@field last_section? string
-
----@class Segments
local M = {}
-function M.normalize(phdrs)
- for _, phdr in ipairs(phdrs) do
- if type(phdr) ~= "table" then error("segment must be table", 3) end
- if not phdr.type then error("type is required in segment", 3) end
+function M.normalize(phdr)
+ if type(phdr) ~= "table" then error("segment must be table", 3) end
+ if not phdr.type then error("type is required in segment", 3) end
- phdr.flags = phdr.flags or 0
- phdr.offset = phdr.offset or 0
- phdr.vaddr = phdr.vaddr or 0
- phdr.paddr = phdr.paddr or 0
- phdr.filesz = phdr.filesz or 0
- phdr.memsz = phdr.memsz or 0
- phdr.align = phdr.align or 1
- end
+ phdr.flags = phdr.flags or 0
+ phdr.offset = phdr.offset or 0
+ phdr.vaddr = phdr.vaddr or 0
+ phdr.paddr = phdr.paddr or 0
+ phdr.filesz = phdr.filesz or 0
+ phdr.memsz = phdr.memsz or 0
+ phdr.align = phdr.align or 1
end
-function M.validate(phdrs)
- for _, phdr in ipairs(phdrs) do
- -- TODO validations
- end
+function M.validate(phdr)
+ -- TODO validations
end
---@param phdr Segment
@@ -71,19 +65,14 @@ local function pack64(phdr, endian)
phdr.align)
end
----@param phdrs Segment[]
+---@param phdr Segment
---@param endian Endian
---@param class Class
---@return string
---@nodiscard
-function M.pack(phdrs, endian, class)
- local p = {}
- for i, phdr in ipairs(phdrs) do
- p[i] = (class == ec.Class.bit32) and
- pack32(phdr, endian) or
- pack64(phdr, endian)
- end
- return table.concat(p)
+function M.pack(phdr, endian, class)
+ return (class == ec.Class.bit32) and
+ pack32(phdr, endian) or pack64(phdr, endian)
end
return M
diff --git a/mkelf.lua b/mkelf.lua
index 518b765..97530b4 100644
--- a/mkelf.lua
+++ b/mkelf.lua
@@ -11,12 +11,12 @@ local function usage()
end
---@param fn string|nil
----@return Header, Segments, Sections
+---@return Header, Segment[], Section[]
local function loadconfig(fn)
local header, segments, sections = {}, {}, {}
local header_mt = { __index = elf.header }
- local segments_mt = { __index = elf.segment }
- local sections_mt = { __index = elf.section }
+ local segment_mt = { __index = elf.segment }
+ local section_mt = { __index = elf.section }
local env = setmetatable({}, {
__index = {
-- Keep in sync with types/elf.d.lua
@@ -41,11 +41,22 @@ local function loadconfig(fn)
if type(t) ~= "table" then
error("segments must be a table", 2)
end
- segments = setmetatable(t, segments_mt)
- t:normalize()
- t:validate()
+ for _, segment in ipairs(t) do
+ segment = setmetatable(segment, segment_mt)
+ segment:normalize()
+ segment:validate()
+ end
+ end,
+ sections = function(t)
+ if type(t) ~= "table" then
+ error("sections must be a table", 2)
+ end
+ for _, section in ipairs(t) do
+ section = setmetatable(t, section_mt)
+ section:normalize()
+ section:validate()
+ end
end,
- sections = function(t) sections = setmetatable(t, sections_mt) end,
},
__newindex = function(_, name)
error(("may not define global %q"):format(name), 2)
@@ -80,8 +91,12 @@ local function main()
header.phentsize = (header.class == ec.Class.bit64) and 0x38 or 0x20
local out <close> = assert(io.open(outfn, "wb"))
out:write(header:pack())
- out:write(segments:pack(header.data, header.class))
- -- out:write(sections:pack(header.data, header.class))
+ for _, segment in ipairs(segments) do
+ out:write(segment:pack(header.data, header.class))
+ end
+ for _, section in ipairs(sections) do
+ out:write(section:pack(header.data, header.class))
+ end
end
main()