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
|
local version <const> = 1
---@enum Osabi
local Osabi <const> = {
sysv = 0x00,
hpux = 0x01,
netbsd = 0x02,
linux = 0x03,
hurd = 0x04,
solaris = 0x06,
aix = 0x07,
irix = 0x08,
freebsd = 0x09,
tru64 = 0x0a,
modesto = 0x0b,
openbsd = 0x0c,
openvms = 0x0d,
nsk = 0x0e,
aros = 0x0f,
fenixos = 0x10,
cloudabi = 0x11,
opevos = 0x12,
}
---@enum Class
local Class <const> = {
bit32 = 0x1,
bit64 = 0x2,
}
---@enum Endian
local Endian <const> = {
little = 0x1,
big = 0x2,
}
---@enum Type
local Type <const> = {
none = 0x00,
rel = 0x01,
exec = 0x02,
dyn = 0x03,
core = 0x04,
loos = 0xfe00,
hios = 0xfeff,
loproc = 0xfe00,
hiproc = 0xfeff,
}
---@enum Machine
local Machine <const> = {
none = 0x00,
att = 0x01,
sparc = 0x02,
x86 = 0x03,
m68k = 0x04,
m88k = 0x05,
-- todo
}
---@alias ElfMagic [integer, integer, integer, integer]
---@class ElfHeader
---@field magic ElfMagic
---@field class Class
---@field data Endian
---@field version integer
---@field osabi Osabi
---@field abiversion integer
---@field type Type
---@field machine Machine
---@type ElfHeader
local header <const> = {
magic = { 0x7f, 0x45, 0x4c, 0x46 },
class = Class.bit32,
data = Endian.big,
version = version,
osabi = Osabi.netbsd,
abiversion = 0,
type = Type.exec,
machine = Machine.x86,
pack = function(self)
local ident = string.pack("BBBBBBBBBxxxxxxx",
self.magic[1], self.magic[2], self.magic[3], self.magic[4],
self.class,
self.data,
self.version,
self.osabi,
self.abiversion)
local e = (self.data == Endian.big) and ">" or "<"
local sz = (self.class == Class.bit64) and "I8" or "I4"
local hdr = string.pack(e .. "I2I2I4" ..
sz .. sz .. sz ..
"I4I2I2I2I2I2I2",
self.type,
self.machine,
self.version,
0,
0x34,
0xff,
0,
52,
0x20,
0,
0x28,
0,
0)
return ident .. hdr
end
}
local function main()
local output = "a.out"
for i in ipairs(arg) do
if arg[i] ~= "-o" then goto continue end
if not arg[i + 1] then
error("mkelf: missing filename after -o")
end
output = arg[i + 1]
::continue::
end
local f = assert(io.open(output, "w"))
f:write(header:pack())
f:close()
end
main()
|