aboutsummaryrefslogtreecommitdiff
path: root/elf/test.lua
blob: 9da65e93dd79dfbf28e9c69c5a2e5c30f75cbd9d (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local fmt = require("elf.fmt")
local elf_header = require("elf.header")
local ec = require("elf.constants")

local nfail = 0
local function test(name, fn)
    local ok, err = pcall(fn)
    if not ok then
        nfail = nfail + 1
        print("fail: " .. name .. ": " .. err)
    end
end

-- Test fmt.lua
test("fmt.endian big", function ()
    assert(fmt.endian(ec.Endian.big) == ">", "invalid format string")
end)

test("fmt.endian little", function ()
    assert(fmt.endian(ec.Endian.little) == "<", "invalid format string")
end)

test("fmt.endian", function ()
    ---@diagnostic disable-next-line: param-type-mismatch
    local ok = pcall(function () fmt.endian(42) end)
    assert(not ok, "invalid input accepted")
end)

test("fmt.word 64", function ()
    assert(fmt.word(ec.Class.bit64) == "I8", "invalid format string")
end)

test("fmt.word 32", function ()
    assert(fmt.word(ec.Class.bit32) == "I4", "invalid format string")
end)

test("fmt.word", function ()
    ---@diagnostic disable-next-line: param-type-mismatch
    local ok = pcall(function () fmt.word(42) end)
    assert(not ok, "invalid input accepted")
end)

-- Test header.lua
test("header.normalize ELF64 defaults", function ()
    local h = elf_header.normalize({
        class = ec.Class.bit64,
        data = ec.Endian.little,
        type = ec.Type.exec,
        machine = ec.Machine.x86_64,
    })

    assert(h.magic[1] == 0x7f, "magic[1] mismatch")
    assert(h.magic[2] == 0x45, "magic[2] mismatch")
    assert(h.magic[3] == 0x4c, "magic[3] mismatch")
    assert(h.magic[4] == 0x46, "magic[4] mismatch")
    assert(h.class == ec.Class.bit64, "class mismatch")
    assert(h.data == ec.Endian.little, "data mismatch")
    assert(h.type == ec.Type.exec, "type mismatch")
    assert(h.machine == ec.Machine.x86_64, "machine mismatch")
    assert(h.ident_version == ec.VERSION, "ident_version mismatch")
    assert(h.osabi == ec.Osabi.sysv, "osabi mismatch")
    assert(h.abiversion == 0, "abiversion mismatch")
    assert(h.version == ec.VERSION, "version mismatch")
    assert(h.entry == 0, "entry mismatch")
    assert(h.phoff == 0x40, "phoff mismatch")
    assert(h.shoff == 0, "shoff mismatch")
    assert(h.flags == 0, "flags mismatch")
    assert(h.ehsize == 0x40, "ehsize mismatch")
    assert(h.phentsize == 0x38, "phentsize mismatch")
    assert(h.phnum == 0, "phnum mismatch")
    assert(h.shentsize == 0x40, "shentsize mismatch")
    assert(h.shnum == 0, "shnum mismatch")
    assert(h.shstrndx == 0, "shstrndx mismatch")
end)

test("header.normalize preserve specified values", function ()
    local h = elf_header.normalize({
        class = ec.Class.bit64,
        data = ec.Endian.little,
        type = ec.Type.exec,
        machine = ec.Machine.x86_64,
        entry = 0x401000,
        flags = 3,
        phnum = 2,
        shnum = 4,
        shstrndx = 3,
    })
    assert(h.entry == 0x401000, "entry mismatch")
    assert(h.flags == 3, "flags mismatch")
    assert(h.phnum == 2, "phnum mismatch")
    assert(h.shnum == 4, "shnum mismatch")
    assert(h.shstrndx == 3, "shstrndx mismatch")
    -- Layout defaults remain ELF64 values.
    assert(h.phoff == 0x40, "phoff mismatch")
    assert(h.ehsize == 0x40, "ehsize mismatch")
    assert(h.phentsize == 0x38, "phentsize mismatch")
    assert(h.shentsize == 0x40, "shentsize mismatch")
end)

test("header.normalize ELF32 layout", function ()
    local h = elf_header.normalize({
        class = ec.Class.bit32,
        data = ec.Endian.little,
        type = ec.Type.exec,
        machine = ec.Machine.i386,
    })
    assert(h.phoff == 0x34, "phoff mismatch")
    assert(h.ehsize == 0x34, "ehsize mismatch")
    assert(h.phentsize == 0x20, "phentsize mismatch")
    assert(h.shentsize == 0x28, "shentsize mismatch")
end)

local function hex_to_bytes(hex)
    return (hex:gsub("..", function (pair)
        local byte = assert(tonumber(pair, 16), "Invalid hex")
        return string.char(byte)
    end))
end

test("header.pack 64_le_x86-64", function ()
    local h = {
        class = ec.Class.bit64,
        data = ec.Endian.little,
        type = ec.Type.exec,
        machine = ec.Machine.x86_64,
    }
    local expected = hex_to_bytes(table.concat({
            -- "BBBBBBBBBxxxxxxx"
            "7f454c46020101000000000000000000",
            -- "I2I2I4"
            "02003e0001000000",
            -- string.rep(fmt.word(hdr.class), 3), where I8 is selected
            "0000000000000000", -- entry
            "4000000000000000", -- phoff
            "0000000000000000", -- shoff
            -- "I4I2I2I2I2I2I2"
            "00000000",         -- flags
            "4000",             -- ehsize
            "3800",             -- phentsize
            "0000",             -- phnum
            "4000",             -- shentsize
            "0000",             -- shnum
            "0000"             -- shstrndx
        }))
    local actual = elf_header.pack(elf_header.normalize(h))
    assert(#actual == 64, "invalid ELF64 header size")
    assert(actual == expected, "mismatch")
end)

test("header.pack 32_le_i386", function ()
    local header = {
        class = ec.Class.bit32,
        data = ec.Endian.little,
        type = ec.Type.exec,
        machine = ec.Machine.i386,
        entry = 0x01020304,
        phoff = 0x11121314,
        shoff = 0x21222324,
        flags = 0x31323334,
        ehsize = 0x3435,
        phentsize = 0x3637,
        phnum = 0x3839,
        shentsize = 0x3a3b,
        shnum = 0x3c3d,
        shstrndx = 0x3e3f,
    }
    local expected = hex_to_bytes(table.concat({
            -- "BBBBBBBBBxxxxxxx"
            "7f454c46010101000000000000000000",
            -- "I2I2I4"
            "0200030001000000",
            -- string.rep(fmt.word(hdr.class), 3), where I4 is selected
            "04030201", -- entry
            "14131211", -- phoff
            "24232221", -- shoff
            -- "I4I2I2I2I2I2I2"
            "34333231", -- flags
            "3534",     -- ehsize
            "3736",     -- phentsize
            "3938",     -- phnum
            "3b3a",     -- shentsize
            "3d3c",     -- shnum
            "3f3e"     -- shstrndx
        }))
    local actual = elf_header.pack(elf_header.normalize(header))
    assert(#actual == 52, "invalid ELF32 header size")
    assert(actual == expected, "mismatch")
end)

test("header.pack 32_be_i386", function ()
    local header = {
        class = ec.Class.bit32,
        data = ec.Endian.big,
        type = ec.Type.exec,
        machine = ec.Machine.i386,
        entry = 0x01020304,
        phoff = 0x11121314,
        shoff = 0x21222324,
        flags = 0x31323334,
        ehsize = 0x3435,
        phentsize = 0x3637,
        phnum = 0x3839,
        shentsize = 0x3a3b,
        shnum = 0x3c3d,
        shstrndx = 0x3e3f,
    }
    local expected = hex_to_bytes(table.concat({
            -- "BBBBBBBBBxxxxxxx"
            "7f454c46010201000000000000000000",
            -- "I2I2I4"
            "0002000300000001",
            -- string.rep(fmt.word(hdr.class), 3), where I4 is selected
            "01020304", -- entry
            "11121314", -- phoff
            "21222324", -- shoff
            -- "I4I2I2I2I2I2I2"
            "31323334", -- flags
            "3435",     -- ehsize
            "3637",     -- phentsize
            "3839",     -- phnum
            "3a3b",     -- shentsize
            "3c3d",     -- shnum
            "3e3f"     -- shstrndx
        }))
    local actual = elf_header.pack(elf_header.normalize(header))
    assert(#actual == 52, "invalid ELF32 header size")
    assert(actual == expected, "mismatch")
end)

test("header.pack 64_be_x86-64", function ()
    local header = {
        class = ec.Class.bit64,
        data = ec.Endian.big,
        type = ec.Type.exec,
        machine = ec.Machine.x86_64,
        entry = 0x0102030405060708,
        phoff = 0x1112131415161718,
        shoff = 0x2122232425262728,
        flags = 0x31323334,
        ehsize = 0x3435,
        phentsize = 0x3637,
        phnum = 0x3839,
        shentsize = 0x3a3b,
        shnum = 0x3c3d,
        shstrndx = 0x3e3f,
    }
    local expected = hex_to_bytes(table.concat({
            -- "BBBBBBBBBxxxxxxx"
            "7f454c46020201000000000000000000",
            -- "I2I2I4"
            "0002003e00000001",
            -- string.rep(fmt.word(hdr.class), 3), where I8 is selected
            "0102030405060708",
            "1112131415161718",
            "2122232425262728",
            -- "I4I2I2I2I2I2I2"
            "31323334", -- flags
            "3435",     -- ehsize
            "3637",     -- phentsize
            "3839",     -- phnum
            "3a3b",     -- shentsize
            "3c3d",     -- shnum
            "3e3f"     -- shstrndx
        }))
    local actual = elf_header.pack(elf_header.normalize(header))
    assert(#actual == 64, "invalid ELF64 header size")
    assert(actual == expected, "mismatch")
end)

-- Test segment.lua
-- Test section.lua
-- Test layout.lua

if nfail > 0 then
    print(("%d %s failed"):format(nfail, (nfail == 1) and "test" or "tests"))
    os.exit(1)
end