aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--elf/header.lua153
-rw-r--r--elf/section.lua89
-rw-r--r--elf/segment.lua98
-rw-r--r--mkelf.lua48
-rw-r--r--types/elf.d.lua14
5 files changed, 210 insertions, 192 deletions
diff --git a/elf/header.lua b/elf/header.lua
index a5fc78a..7251d99 100644
--- a/elf/header.lua
+++ b/elf/header.lua
@@ -3,91 +3,102 @@ local ec = require("elf.constants")
---@alias Magic [integer, integer, integer, integer]
----@class Header
----@field magic? Magic
----@field class Class
----@field data Endian
+---@class HeaderOptions
+---@field magic? Magic
+---@field class Class
+---@field data Endian
---@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 phentsize? integer
----@field phnum? integer
----@field shentsize? integer
----@field shnum? integer
----@field shstrndx? integer
-local M = {}
+---@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 phentsize? integer
+---@field phnum? integer
+---@field shentsize? integer
+---@field shnum? integer
+---@field shstrndx? integer
-function M.normalize(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
+---@class Header
+---@field magic Magic
+---@field class Class
+---@field data Endian
+---@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 phentsize integer
+---@field phnum integer
+---@field shentsize integer
+---@field shnum integer
+---@field shstrndx integer
- 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 ((hdr.class == ec.Class.bit32) and 0x34 or 0x40)
- hdr.shoff = hdr.shoff or 0
- hdr.flags = hdr.flags or 0
- hdr.ehsize = hdr.ehsize or ((hdr.class == ec.Class.bit32) and 0x34 or 0x40)
- hdr.phentsize = hdr.phentsize or
- ((hdr.class == ec.Class.bit32) and 0x20 or 0x38)
- hdr.phnum = hdr.phnum or 0
- hdr.shentsize = hdr.shentsize or
- ((hdr.class == ec.Class.bit32) and 0x28 or 0x40)
- hdr.shnum = hdr.shnum or 0
- hdr.shstrndx = hdr.shstrndx or 0
-end
+local M = {}
+
+---@param hdropts HeaderOptions
+---@return Header
+function M.normalize(hdropts)
+ if not hdropts.class then error("class is required in header", 3) end
+ if not hdropts.data then error("data is required in header", 3) end
+ if not hdropts.type then error("type is required in header", 3) end
+ if not hdropts.machine then error("machine is required in header", 3) end
-function M.validate(hdr)
- if hdr.class ~= ec.Class.bit32 and hdr.class ~= ec.Class.bit64 then
+ -- Can't pack without these values being valid.
+ if hdropts.class ~= ec.Class.bit32 and hdropts.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
+ if hdropts.data ~= ec.Endian.little and hdropts.data ~= ec.Endian.big then
error("data must be Endian.little or Endian.big in header", 3)
end
- -- TODO additional validations
+
+ return {
+ magic = hdropts.magic or { 0x7f, 0x45, 0x4c, 0x46 },
+ class = hdropts.class,
+ data = hdropts.data,
+ ident_version = hdropts.ident_version or ec.VERSION,
+ osabi = hdropts.osabi or ec.Osabi.sysv,
+ abiversion = hdropts.abiversion or 0,
+ type = hdropts.type,
+ machine = hdropts.machine,
+ version = hdropts.version or ec.VERSION,
+ entry = hdropts.entry or 0,
+ shoff = hdropts.shoff or 0,
+ flags = hdropts.flags or 0,
+ phnum = hdropts.phnum or 0,
+ shnum = hdropts.shnum or 0,
+ shstrndx = hdropts.shstrndx or 0,
+ phoff = (hdropts.class == ec.Class.bit32) and 0x34 or 0x40,
+ ehsize = (hdropts.class == ec.Class.bit32) and 0x34 or 0x40,
+ phentsize = (hdropts.class == ec.Class.bit32) and 0x20 or 0x38,
+ shentsize = (hdropts.class == ec.Class.bit32) and 0x28 or 0x40
+ }
end
---@param hdr Header
---@return string
---@nodiscard
function M.pack(hdr)
- return string.pack("BBBBBBBBBxxxxxxx" ..
- fmt.endian(hdr.data) ..
- "I2I2I4" ..
- string.rep(fmt.word(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,
- hdr.type,
- hdr.machine,
- hdr.version,
- hdr.entry,
- hdr.phoff,
- hdr.shoff,
- hdr.flags,
- hdr.ehsize,
- hdr.phentsize,
- hdr.phnum,
- hdr.shentsize,
- hdr.shnum,
- hdr.shstrndx)
+ return string.pack(
+ "BBBBBBBBBxxxxxxx" .. fmt.endian(hdr.data) .. "I2I2I4"
+ .. string.rep(fmt.word(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, hdr.type,
+ hdr.machine, hdr.version, hdr.entry, hdr.phoff, hdr.shoff, hdr.flags,
+ hdr.ehsize, hdr.phentsize, hdr.phnum, hdr.shentsize, hdr.shnum,
+ hdr.shstrndx
+ )
end
return M
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
-local M = {}
+---@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
-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
+local M = {}
- 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
diff --git a/elf/segment.lua b/elf/segment.lua
index 59863b5..dd3d01f 100644
--- a/elf/segment.lua
+++ b/elf/segment.lua
@@ -1,77 +1,83 @@
local ec = require("elf.constants")
local fmt = require("elf.fmt")
----@class Segment
----@field type SegmentType
----@field flags? integer
----@field offset? integer
----@field vaddr? integer
----@field paddr? integer
----@field filesz? integer
----@field memsz? integer
----@field align? integer
+---@class SegmentOptions
+---@field type SegmentType
+---@field flags? integer
+---@field offset? integer
+---@field vaddr? integer
+---@field paddr? integer
+---@field filesz? integer
+---@field memsz? integer
+---@field align? integer
---@field first_section? string
----@field last_section? string
-local M = {}
+---@field last_section? string
+
+---@class Segment
+---@field type SegmentType
+---@field flags integer
+---@field offset integer
+---@field vaddr integer
+---@field paddr integer
+---@field filesz integer
+---@field memsz integer
+---@field align integer
+---@field first_section string
+---@field last_section string
-function M.normalize(phdr)
- if not phdr.type then error("type is required in segment", 3) end
+local M = {}
- phdr.flags = phdr.flags or 0
- phdr.offset = phdr.offset or 0
- phdr.vaddr = phdr.vaddr or 0
- phdr.paddr = phdr.paddr or 0
- phdr.filesz = phdr.filesz or 0
- phdr.memsz = phdr.memsz or 0
- phdr.align = phdr.align or 1
-end
+---@param segopts SegmentOptions
+---@return Segment
+function M.normalize(segopts)
+ if not segopts.type then error("type is required in segment", 3) end
-function M.validate(phdr)
- -- TODO validations
+ return {
+ type = segopts.type,
+ flags = segopts.flags or 0,
+ offset = segopts.offset or 0,
+ vaddr = segopts.vaddr or 0,
+ paddr = segopts.paddr or 0,
+ filesz = segopts.filesz or 0,
+ memsz = segopts.memsz or 0,
+ align = segopts.align or 1,
+ first_section = "",
+ last_section = ""
+ }
end
----@param phdr Segment
+---@param phdr Segment
---@param endian Endian
---@return string
---@nodiscard
local function pack32(phdr, endian)
return string.pack(
- fmt.endian(endian) .. string.rep(fmt.word(ec.Class.bit32), 8),
- phdr.type,
- phdr.offset,
- phdr.vaddr,
- phdr.paddr,
- phdr.filesz,
- phdr.memsz,
- phdr.flags,
- phdr.align)
+ fmt.endian(endian) .. string.rep(fmt.word(ec.Class.bit32), 8), phdr.type,
+ phdr.offset, phdr.vaddr, phdr.paddr, phdr.filesz, phdr.memsz, phdr.flags,
+ phdr.align
+ )
end
----@param phdr Segment
+---@param phdr Segment
---@param endian Endian
---@return string
---@nodiscard
local function pack64(phdr, endian)
return string.pack(
fmt.endian(endian) .. "I4I4" .. string.rep(fmt.word(ec.Class.bit64), 6),
- phdr.type,
- phdr.flags,
- phdr.offset,
- phdr.vaddr,
- phdr.paddr,
- phdr.filesz,
- phdr.memsz,
- phdr.align)
+ phdr.type, phdr.flags, phdr.offset, phdr.vaddr, phdr.paddr, phdr.filesz,
+ phdr.memsz, phdr.align
+ )
end
----@param phdr Segment
+---@param phdr Segment
---@param endian Endian
----@param class Class
+---@param class Class
---@return string
---@nodiscard
function M.pack(phdr, endian, class)
- return (class == ec.Class.bit32) and
- pack32(phdr, endian) or pack64(phdr, endian)
+ return (class == ec.Class.bit32) and pack32(phdr, endian)
+ or pack64(phdr, endian)
end
return M
diff --git a/mkelf.lua b/mkelf.lua
index cd02626..d77ac0f 100644
--- a/mkelf.lua
+++ b/mkelf.lua
@@ -10,13 +10,15 @@ local function usage()
die("[-o output] [input]")
end
----@param fn string|nil
+---@param fn string | nil
---@return Header, Segment[], Section[]
local function loadconfig(fn)
- local header, segments, sections = {}, {}, {}
- local header_mt = { __index = elf.header }
- local segment_mt = { __index = elf.segment }
- local section_mt = { __index = elf.section }
+ ---@type Header
+ local header
+ ---@type Segment[]
+ local segments = {}
+ ---@type Section[]
+ local sections = {}
local env = setmetatable({}, {
__index = {
Class = ec.Class,
@@ -30,44 +32,36 @@ local function loadconfig(fn)
SectionFlags = ec.SectionFlags,
ELFVERSION = ec.VERSION,
- header = function(t)
+ header = function (t)
if type(t) ~= "table" then
error("header must be a table", 2)
end
- header = setmetatable(t, header_mt)
- t:normalize()
- t:validate()
+ header = elf.header.normalize(t)
end,
- segments = function(t)
+ segments = function (t)
if type(t) ~= "table" then
error("segments must be a table", 2)
end
- for i, segment in ipairs(t) do
- if type(segment) ~= "table" then
+ for i, s in ipairs(t) do
+ if type(s) ~= "table" then
error(("segment #%d must be a table"):format(i), 2)
end
- segment = setmetatable(segment, segment_mt)
- segment:normalize()
- segment:validate()
+ table.insert(segments, elf.segment.normalize(s))
end
- segments = t
end,
- sections = function(t)
+ sections = function (t)
if type(t) ~= "table" then
error("sections must be a table", 2)
end
- for i, section in ipairs(t) do
- if type(section) ~= "table" then
+ for i, s in ipairs(t) do
+ if type(s) ~= "table" then
error(("section #%d must be a table"):format(i), 2)
end
- section = setmetatable(section, section_mt)
- section:normalize()
- section:validate()
+ table.insert(sections, elf.section.normalize(s))
end
- sections = t
end,
},
- __newindex = function(_, name)
+ __newindex = function (_, name)
error(("may not define global %q"):format(name), 2)
end,
__metatable = false,
@@ -101,12 +95,12 @@ local function main()
if header.phnum == 0 then header.phnum = #segments end
if header.shnum == 0 then header.shnum = #sections end
local out <close> = assert(io.open(outfn, "wb"))
- out:write(header:pack())
+ out:write(elf.header.pack(header))
for _, segment in ipairs(segments) do
- out:write(segment:pack(header.data, header.class))
+ out:write(elf.segment.pack(segment, header.data, header.class))
end
for _, section in ipairs(sections) do
- out:write(section:pack(header.data, header.class))
+ out:write(elf.section.pack(section, header.data, header.class))
end
end
diff --git a/types/elf.d.lua b/types/elf.d.lua
index 4d38707..40f88df 100644
--- a/types/elf.d.lua
+++ b/types/elf.d.lua
@@ -13,24 +13,24 @@ SectionType = ec.SectionType
SectionFlags = ec.SectionFlags
ELFVERSION = ec.VERSION
---[[
-ELF header.
+--[[ ELF header.
+
There can be only one header per configuration file.
class, data, type and machine are mandatory fields while
the remaining fields are assigned defaults or computed after layout.
]]
----@param hdr Header
+---@param hdr HeaderOptions
function header(hdr) end
---[[
-ELF segments.
+--[[ ELF segments.
+
The program headers are derived from these segment entries.
type field is mandatory and rest of fields are assigned defaults
or computed after layout.A segment describes either a range of sections
or a raw mapping. Do not mix the two forms.
]]
----@param xs Segment[]
+---@param xs SegmentOptions[]
function segments(xs) end
----@param xs Section[]
+---@param xs SectionOptions[]
function sections(xs) end