aboutsummaryrefslogtreecommitdiff
path: root/elf/header.lua
blob: de939d912e31bdf45dfe46263b5e773b56c545a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
local fmt = require("elf.fmt")
local ec = require("elf.constants")

---@alias Magic [integer, integer, integer, integer]

---@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

---@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

local M = {}

---@param hdropts HeaderOptions
---@return Header
function M.normalize(hdropts)
    -- TODO drop these error() and return, let callers report.
    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

    -- 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 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

    local phoff, ehsize, phentsize, shentsize
    if hdropts.class == ec.Class.bit32 then
        phoff = hdropts.phoff or 0x34
        ehsize = hdropts.ehsize or 0x34
        phentsize = hdropts.phentsize or 0x20
        shentsize = hdropts.shentsize or 0x28
    else
        phoff = hdropts.phoff or 0x40
        ehsize = hdropts.ehsize or 0x40
        phentsize = hdropts.phentsize or 0x38
        shentsize = hdropts.shentsize or 0x40
    end

    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 = phoff,
        ehsize = ehsize,
        phentsize = phentsize,
        shentsize = shentsize,
    }
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
    )
end

return M