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
|
local ELFVERSION <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,
m32 = 0x01, -- AT&T WE 32100
sparc = 0x02, -- SPARC
i386 = 0x03, -- Intel 80386
m68k = 0x04, -- Motorola 68000 (M68k)
m88k = 0x05, -- Motorola 88000 (M88k)
mcu = 0x06, -- Intel MCU
i860 = 0x07, -- Intel 80860
mips = 0x08, -- MIPS
s370 = 0x09, -- IBM System/370
mipsrs3le = 0x0a, -- MIPS RS3000 Little Endian
parisc = 0x0f, -- Hewlett-Packard PA-RISC
vpp500 = 0x11, -- Fujitsu VPP500
sparc32 = 0x12, -- Enhanced instruction set SPARC
i960 = 0x13, -- Intel 80960
ppc = 0x14, -- PowerPC
ppc64 = 0x15, -- PowerPC (64-bit)
s390 = 0x16, -- IBM System/390
spu = 0x17, -- IBM SPC/SPU
v800 = 0x24, -- NEC V800
fr20 = 0x25, -- Fujitsu FR20
-- todo
}
---@alias ElfMagic [integer, integer, integer, integer]
---@class ElfHeader
---@field magic ElfMagic
---@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
---@param endian Endian
---@nodiscard
local function efmt(endian)
return (endian == Endian.big) and ">" or "<"
end
---@param class Class
---@nodiscard
local function szfmt(class)
return (class == Class.bit64) and "I8" or "I4"
end
---@param hdr ElfHeader
---@nodiscard
local function ElfHeader_pack(hdr)
return string.pack("BBBBBBBBBxxxxxxx" ..
efmt(hdr.data) ..
"I2I2I4" ..
string.rep(szfmt(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
---@class ProgramHeaders
---@field type integer
---@field flags integer
---@field offset integer
---@field vaddr integer
---@field paddr integer
---@field filesz integer
---@field memsz integer
---@field align integer
---@enum SegmentType
local SegmentType <const> = {
null = 0x00000000,
load = 0x00000001,
dynamic = 0x00000002,
interp = 0x00000003,
note = 0x00000004,
shlib = 0x00000005,
phdr = 0x00000006,
tls = 0x00000007,
loos = 0x60000000,
hios = 0x6FFFFFFF,
loproc = 0x70000000,
hiproc = 0x7FFFFFFF,
}
---@enum SegmentFlags
local SegmentFlags <const> = {
x = 0x1,
w = 0x2,
r = 0x3,
}
---@param hdr ProgramHeaders
---@param endian Endian
---@param class Class
---@nodiscard
local function ElfPHeader_pack(hdr, endian, class)
if class == Class.bit32 then
return string.pack(efmt(endian) .. string.rep(szfmt(class), 8),
hdr.type,
hdr.offset,
hdr.vaddr,
hdr.paddr,
hdr.filesz,
hdr.memsz,
hdr.flags,
hdr.align)
elseif class == Class.bit64 then
return string.pack(efmt(endian) .. "I4I4" .. string.rep(szfmt(class), 6),
hdr.type,
hdr.flags,
hdr.offset,
hdr.vaddr,
hdr.paddr,
hdr.filesz,
hdr.memsz,
hdr.align)
end
end
local function main()
---@type ElfHeader
local header <const> = {
magic = { 0x7f, 0x45, 0x4c, 0x46 },
class = Class.bit32,
data = Endian.big,
ident_version = ELFVERSION,
osabi = Osabi.netbsd,
abiversion = 0,
type = Type.exec,
machine = Machine.i386,
version = ELFVERSION,
entry = 0,
phoff = 0x34,
shoff = 0xff,
flags = 0,
ehsize = 52,
phentsize = 0x20,
phnum = 0,
shentsize = 0x28,
shnum = 0,
shstrndx = 0,
}
---@type ProgramHeaders
local pheader <const> = {
type = SegmentType.dynamic,
flags = SegmentFlags.x,
offset = 0xf,
vaddr = 0xff,
paddr = 0xfff,
filesz = 0xffff,
memsz = 0xfffff,
align = 1,
}
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(ElfHeader_pack(header))
f:write(ElfPHeader_pack(pheader, header.data, header.class))
f:close()
end
main()
|