From 9b629a1bcac12aad562864270818c778a1bef285 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Thu, 15 Oct 2026 21:58:04 +0530 Subject: Make it compact, just pack once. --- mkelf.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mkelf.lua b/mkelf.lua index eb76d16..b6535d6 100644 --- a/mkelf.lua +++ b/mkelf.lua @@ -99,20 +99,24 @@ local function efmt(endian) return (endian == Endian.big) and ">" or "<" end +local function szfmt(class) + return (class == Class.bit64) and "I8" or "I4" +end + ---@param hdr ElfHeader ---@nodiscard local function ElfHeader_pack(hdr) - local ident = string.pack("BBBBBBBBBxxxxxxx", + return string.pack("BBBBBBBBBxxxxxxx" .. + efmt(hdr.data) .. + "I2I2I4" .. + string.rep(szfmt(hdr.class), 3) .. + "I4I2I2I2I2I2I2", hdr.magic[1], hdr.magic[2], hdr.magic[3], hdr.magic[4], hdr.class, hdr.data, hdr.ident_version, hdr.osabi, - hdr.abiversion) - local sz = (hdr.class == Class.bit64) and "I8" or "I4" - local h = string.pack(efmt(hdr.data) .. "I2I2I4" .. - sz .. sz .. sz .. - "I4I2I2I2I2I2I2", + hdr.abiversion, hdr.type, hdr.machine, hdr.version, @@ -126,7 +130,6 @@ local function ElfHeader_pack(hdr) hdr.shentsize, hdr.shnum, hdr.shstrndx) - return ident .. h end ---@type ElfHeader -- cgit v1.3