summaryrefslogtreecommitdiff
path: root/mkelf.lua
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2026-10-15 21:58:04 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2026-10-15 21:58:04 +0530
commit9b629a1bcac12aad562864270818c778a1bef285 (patch)
treee2c093d3ba816b037d9c5687f888da1e04e7c183 /mkelf.lua
parentbda75b0708a11a2fdb47eaa45f0c0b23195127f5 (diff)
Make it compact, just pack once.
Diffstat (limited to 'mkelf.lua')
-rw-r--r--mkelf.lua17
1 files 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