summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2026-10-24 11:21:21 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2026-10-24 11:21:21 +0530
commit3e36380874aa40932fa629fc4f53bdd50c4f2311 (patch)
treeca18272e9d5f4143937d8dbb869290e22dea4560
parent4676bb3a05d89bb3c282da2ed8b2072e163093ff (diff)
Attach the respective metatables to loaded the config.
This simplifies calling pack() on respective tables.
-rw-r--r--elf/header.lua3
-rw-r--r--elf/section.lua9
-rw-r--r--elf/segment.lua5
-rw-r--r--mkelf.lua17
4 files changed, 19 insertions, 15 deletions
diff --git a/elf/header.lua b/elf/header.lua
index 2a9ec68..6baf327 100644
--- a/elf/header.lua
+++ b/elf/header.lua
@@ -1,7 +1,5 @@
local fmt = require("elf.fmt")
-local M = {}
-
---@alias Magic [integer, integer, integer, integer]
---@class Header
@@ -24,6 +22,7 @@ local M = {}
---@field shentsize integer
---@field shnum integer
---@field shstrndx integer
+local M = {}
---@param hdr Header
---@return string
diff --git a/elf/section.lua b/elf/section.lua
index 64a91b2..5e185fd 100644
--- a/elf/section.lua
+++ b/elf/section.lua
@@ -1,14 +1,15 @@
local fmt = require("elf.fmt")
-local M = {}
-
---@class Section
----@param section Section
+---@class Sections
+local M = {}
+
+---@param sections Sections
---@param endian Endian
---@return string
---@nodiscard
-function M.pack(section, endian)
+function M.pack(sections, endian)
-- TODO
return string.pack("B", 0x1)
end
diff --git a/elf/segment.lua b/elf/segment.lua
index da8e9c6..662f42e 100644
--- a/elf/segment.lua
+++ b/elf/segment.lua
@@ -1,8 +1,6 @@
local ec = require("elf.constants")
local fmt = require("elf.fmt")
-local M = {}
-
---@class Segment
---@field type SegmentType
---@field flags integer
@@ -13,6 +11,9 @@ local M = {}
---@field memsz integer
---@field align integer
+---@class Segments
+local M = {}
+
---@param phdr Segment
---@param endian Endian
---@return string
diff --git a/mkelf.lua b/mkelf.lua
index 6edeebb..7dd32c6 100644
--- a/mkelf.lua
+++ b/mkelf.lua
@@ -11,9 +11,12 @@ local function usage()
end
---@param fn string|nil
----@return Header, Segment[], Section[]
+---@return Header, Segments, Sections
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 env = {
-- Keep in sync with types/elf.d.lua
Class = elf.Class,
@@ -25,9 +28,9 @@ local function loadconfig(fn)
SegmentFlags = elf.SegmentFlags,
ELFVERSION = elf.VERSION,
- header = function(t) header = t end,
- segments = function(t) segments = t end,
- sections = function(t) sections = t end,
+ header = function(t) header = setmetatable(t, header_mt) end,
+ segments = function(t) segments = setmetatable(t, segments_mt) end,
+ sections = function(t) sections = setmetatable(t, sections_mt) end,
}
-- TODO error check for multiple headers
local chunk, err = loadfile(fn, "t", env)
@@ -56,10 +59,10 @@ local function main()
header.phentsize = (header.class == ec.Class.bit64) and 0x38 or 0x20
-- TODO write to a temp file and atomic rename to outfn.
local out <close> = assert(io.open(outfn, "wb"))
- out:write(elf.header.pack(header))
- out:write(elf.segment.pack(segments, header.data, header.class))
+ out:write(header:pack())
+ out:write(segments:pack(header.data, header.class))
-- TODO finish section.pack
- -- out:write(elf.section.pack(c.sections, c.header.data))
+ out:write(sections:pack(header.data))
end
main()