diff options
| -rw-r--r-- | elf/header.lua | 3 | ||||
| -rw-r--r-- | elf/section.lua | 9 | ||||
| -rw-r--r-- | elf/segment.lua | 5 | ||||
| -rw-r--r-- | mkelf.lua | 17 |
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 @@ -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() |
