From f45ff5c60b7464dc250671d58c969a6320aad9b6 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Wed, 4 Nov 2026 11:11:32 +0530 Subject: Refactor normalize() to prevent in-place mutation. We would need to distinguish user configuration options from normalization. Let normalize() return a new table instead of in-place update. Split annotations into xxxOptions and xxx for header, segments and sections, delineating optional vs normalized table. Leave header, segments and sections as just a data table for now and stop trying to make them objects. Leave data alone. --- elf/section.lua | 89 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 41 deletions(-) (limited to 'elf/section.lua') diff --git a/elf/section.lua b/elf/section.lua index 805a450..10f9652 100644 --- a/elf/section.lua +++ b/elf/section.lua @@ -1,34 +1,50 @@ local fmt = require("elf.fmt") ----@class Section ----@field name string ----@field type SectionType ----@field flags integer ----@field addr? integer ----@field offset? integer ----@field size? integer ----@field link? integer ----@field info? integer +---@class SectionOptions +---@field name string +---@field type SectionType +---@field flags integer +---@field addr? integer +---@field offset? integer +---@field size? integer +---@field link? integer +---@field info? integer ---@field addralign? integer ----@field entsize? integer +---@field entsize? integer + +---@class Section +---@field name string +---@field type SectionType +---@field flags integer +---@field addr integer +---@field offset integer +---@field size integer +---@field link integer +---@field info integer +---@field addralign integer +---@field entsize integer + local M = {} -function M.normalize(section) - if not section.name then error("name is required in section", 3) end - if not section.type then error("type is required in section", 3) end - if not section.flags then error("flags required in section", 3) end - - section.addr = section.addr or 0 - section.offset = section.offset or 0 - section.size = section.size or 0 - section.link = section.link or 0 - section.info = section.info or 0 - section.addralign = section.addralign or 0 - section.entsize = section.entsize or 0 -end +---@param secopts SectionOptions +---@return Section +function M.normalize(secopts) + if not secopts.name then error("name is required in section", 3) end + if not secopts.type then error("type is required in section", 3) end + if not secopts.flags then error("flags required in section", 3) end -function M.validate(section) - --TODO + return { + name = secopts.name, + type = secopts.type, + flags = secopts.flags, + addr = secopts.addr or 0, + offset = secopts.offset or 0, + size = secopts.size or 0, + link = secopts.link or 0, + info = secopts.info or 0, + addralign = secopts.addralign or 0, + entsize = secopts.entsize or 0 + } end local function section_index(name) @@ -38,25 +54,16 @@ local function section_index(name) end ---@param section Section ----@param endian Endian +---@param endian Endian +---@param class Class ---@return string ---@nodiscard 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_index(section.name), - section.type, - section.flags, - section.addr, - section.offset, - section.size, - section.link, - section.info, - section.addralign, - section.entsize) + return string.pack(fmt.endian(endian) .. "I4I4" + .. string.rep(fmt.word(class), 4) .. "I4I4" + .. string.rep(fmt.word(class), 2), section_index(section.name), + section.type, section.flags, section.addr, section.offset, section.size, + section.link, section.info, section.addralign, section.entsize) end return M -- cgit v1.3