aboutsummaryrefslogtreecommitdiff
path: root/elf/section.lua
blob: 7849d4806527b7f79dcde2ffee02cba2d9fb8dae (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
local fmt = require("elf.fmt")

---@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 content?   string

---@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
---@field content   string

local M = {}

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

    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,
        content = secopts.content or "",
    }
end

local function section_index(name)
    -- TODO

    return 42
end

---@param section Section
---@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)
end

return M