diff options
| author | Sunil Nimmagadda <sunil@nimmagadda.net> | 2026-10-24 14:14:33 +0530 |
|---|---|---|
| committer | Sunil Nimmagadda <sunil@nimmagadda.net> | 2026-10-24 14:14:33 +0530 |
| commit | fd07227afb68dfc5080427aac5714ba82f9e8b93 (patch) | |
| tree | 38f9489ac2ad58e061789b189f5372a70e12e043 | |
| parent | 0f9327478c0920aeddde2fbd787da844bad27308 (diff) | |
Refactor header configuration handling.
- Mark optional fields and normalize them on load.
- Validate (work in progress) required fields and their values.
- Make sure that errors point to useful location in the config file.
| -rw-r--r-- | elf/header.lua | 60 | ||||
| -rw-r--r-- | mkelf.lua | 6 | ||||
| -rw-r--r-- | sample.elf.lua | 13 |
3 files changed, 52 insertions, 27 deletions
diff --git a/elf/header.lua b/elf/header.lua index 6baf327..b289f13 100644 --- a/elf/header.lua +++ b/elf/header.lua @@ -1,29 +1,63 @@ local fmt = require("elf.fmt") +local ec = require("elf.constants") ---@alias Magic [integer, integer, integer, integer] ---@class Header ----@field magic Magic +---@field magic? Magic ---@field class Class ---@field data Endian ----@field ident_version integer ----@field osabi Osabi ----@field abiversion integer +---@field ident_version? integer +---@field osabi? Osabi +---@field abiversion? integer ---@field type Type ---@field machine Machine ----@field version integer ----@field entry integer ----@field phoff integer ----@field shoff integer ----@field flags integer ----@field ehsize integer +---@field version? integer +---@field entry? integer +---@field phoff? integer +---@field shoff? integer +---@field flags? integer +---@field ehsize? integer ---@field phentsize? integer ---@field phnum? integer ----@field shentsize integer ----@field shnum integer ----@field shstrndx integer +---@field shentsize? integer +---@field shnum? integer +---@field shstrndx? integer local M = {} +function M.normalize(hdr) + hdr.magic = hdr.magic or { 0x7f, 0x45, 0x4c, 0x46 } + hdr.ident_version = hdr.ident_version or ec.VERSION + hdr.osabi = hdr.osabi or ec.Osabi.sysv + hdr.abiversion = hdr.abiversion or 0 + hdr.version = hdr.version or ec.VERSION + hdr.entry = hdr.entry or 0 + hdr.phoff = hdr.phoff or 0 + hdr.shoff = hdr.shoff or 0 + hdr.flags = hdr.flags or 0 + hdr.ehsize = hdr.ehsize or 0 + hdr.phentsize = hdr.phentsize or 0 + hdr.phnum = hdr.phnum or 0 + hdr.shentsize = hdr.shentsize or 0 + hdr.shnum = hdr.shnum or 0 + hdr.shstrndx = hdr.shstrndx or 0 +end + +function M.validate(hdr) + if not hdr.class then error("class is required in header", 3) end + if not hdr.data then error("data is required in header", 3) end + if not hdr.type then error("type is required in header", 3) end + if not hdr.machine then error("machine is required in header", 3) end + + if hdr.class ~= ec.Class.bit32 and hdr.class ~= ec.Class.bit64 then + error("class must be Class.bit32 or Class.bit64 in header", 3) + end + if hdr.data ~= ec.Endian.little and hdr.data ~= ec.Endian.big then + error("data must be Endian.little or Endian.big in header", 3) + end + -- TODO additional validations +end + ---@param hdr Header ---@return string ---@nodiscard @@ -29,7 +29,11 @@ local function loadconfig(fn) SegmentFlags = elf.SegmentFlags, ELFVERSION = elf.VERSION, - header = function(t) header = setmetatable(t, header_mt) end, + header = function(t) + header = setmetatable(t, header_mt) + t:normalize() + t:validate() + end, segments = function(t) segments = setmetatable(t, segments_mt) end, sections = function(t) sections = setmetatable(t, sections_mt) end, }, diff --git a/sample.elf.lua b/sample.elf.lua index f6b48d5..d37a7c5 100644 --- a/sample.elf.lua +++ b/sample.elf.lua @@ -1,21 +1,8 @@ header { - magic = { 0x7f, 0x45, 0x4c, 0x46 }, class = Class.bit64, data = Endian.big, - ident_version = ELFVERSION, - osabi = Osabi.netbsd, - abiversion = 0, type = Type.exec, machine = Machine.i386, - version = ELFVERSION, - entry = 0, - phoff = 0x40, - shoff = 0xff, - flags = 0, - ehsize = 64, - shentsize = 0x40, - shnum = 0, - shstrndx = 0, } segments { |
