Repository navigation
Expand file tree
/
Copy pathelf.lua
More file actions
178 lines (157 loc) · 6.32 KB
/
Copy pathelf.lua
File metadata and controls
178 lines (157 loc) · 6.32 KB
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
--
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
--
---
-- The relocatable ELF object libbpf loads: sections, a symbol table and the call relocations.
--
-- One string table serves both the section names and the symbol names. A symbol table lists its
-- local symbols first and says in `sh_info` where the global ones begin, so `symbol` refuses a
-- local added after a global rather than renumbering what a relocation already names.
-- @module luaebpf.elf
local class = require("lunatik.class")
local strtab = require("luaebpf.strtab")
local EHSIZE <const> = 64
local SHENTSIZE <const> = 64
local SYMSIZE <const> = 24
local RELSIZE <const> = 16
local EM_BPF <const> = 247
local ET_REL <const> = 1
local ELFDATA2LSB <const> = 1
local ELFDATA2MSB <const> = 2
local SHN_UNDEF <const> = 0
local elf = {}
--- Section types, as `SHT_*` names them.
-- @table luaebpf.elf.section
elf.section = {PROGBITS = 1, SYMTAB = 2, STRTAB = 3, REL = 9}
--- Section flags, as `SHF_*` names them.
-- @table luaebpf.elf.flags
elf.flags = {WRITE = 0x1, ALLOC = 0x2, EXEC = 0x4}
--- Symbol bindings, as `STB_*` names them.
-- @table luaebpf.elf.bind
elf.bind = {LOCAL = 0, GLOBAL = 1}
--- Symbol types, as `STT_*` names them.
-- @table luaebpf.elf.type
elf.type = {NOTYPE = 0, OBJECT = 1, FUNC = 2}
--- Relocation types: `IMM64` is `R_BPF_64_64`, which patches the immediate of an `ld_imm64`,
-- and `IMM32` is `R_BPF_64_32`, which patches a 32-bit one.
-- @table luaebpf.elf.relocation
elf.relocation = {IMM64 = 1, IMM32 = 10}
---
-- An object under construction.
-- @type luaebpf.elf.object
local object = class{}
---
-- A fresh object, with its null section and null symbol already in place.
-- @function luaebpf.elf.new
-- @tparam boolean bigendian whether the object is written big-endian
-- @treturn luaebpf.elf.object
function elf.new(bigendian)
local order = bigendian and ">" or "<"
return object:new{sections = {{name = "", data = ""}}, indexes = {}, nlocals = 1, order = order,
symbols = {string.pack(order .. "I4I1I1I2I8I8", 0, 0, 0, 0, 0, 0)}, names = strtab.new()}
end
---
-- Interns a string in the object's string table.
-- @function luaebpf.elf.object:string
-- @tparam string name
-- @treturn integer its offset in the table
function object:string(name)
return self.names:intern(name)
end
---
-- Appends a section.
-- @function luaebpf.elf.object:section
-- @tparam table section `{name, type, flags, data, link, info, align, entsize}`; `link` and a
-- string `info` name another section.
-- @treturn integer the section's index
function object:section(section)
self:string(section.name)
table.insert(self.sections, section)
self.indexes[section.name] = #self.sections - 1
return #self.sections - 1
end
---
-- Appends a symbol.
-- @function luaebpf.elf.object:symbol
-- @tparam table symbol `{name, section, value, size, bind, type}`, `type` one of `elf.type`; a
-- symbol with no `section` is undefined, for a relocation libbpf resolves outside the object
-- @treturn integer the symbol's index
-- @raise a local symbol follows a global one
function object:symbol(symbol)
if symbol.bind == elf.bind.LOCAL then
if #self.symbols > self.nlocals then
error("luaebpf.elf: a local symbol follows a global one", 0)
end
self.nlocals = self.nlocals + 1
end
local shndx = symbol.section == nil and SHN_UNDEF or self.indexes[symbol.section]
table.insert(self.symbols, string.pack(self.order .. "I4I1I1I2I8I8", self:string(symbol.name),
(symbol.bind << 4) | symbol.type, 0, shndx, symbol.value, symbol.size))
return #self.symbols - 1
end
---
-- A relocation section for `name`, each entry `{at, symbol, type}` with `at` a byte offset into
-- the relocated section and `type` one of `elf.relocation`.
-- @function luaebpf.elf.object:relocations
-- @tparam string name the relocated section
-- @tparam table entries
function object:relocations(name, entries)
local records = {}
for _, entry in ipairs(entries) do
table.insert(records, string.pack(self.order .. "I8I8", entry.at, (entry.symbol << 32) | entry.type))
end
self:section{name = ".rel" .. name, type = elf.section.REL, flags = 0, data = table.concat(records),
link = ".symtab", info = name, align = 8, entsize = RELSIZE}
end
local function place(self)
local offset = EHSIZE
for i = 2, #self.sections do
local section = self.sections[i]
local align = section.align or 1
offset = offset + (align - offset % align) % align
section.offset = offset
offset = offset + #section.data
end
return offset
end
local function headers(self)
local out = {string.pack(self.order .. "I4I4I8I8I8I8I4I4I8I8", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)}
for i = 2, #self.sections do
local section = self.sections[i]
local link = section.link ~= nil and self.indexes[section.link] or 0
local info = type(section.info) == "string" and self.indexes[section.info] or (section.info or 0)
table.insert(out, string.pack(self.order .. "I4I4I8I8I8I8I4I4I8I8", self:string(section.name), section.type,
section.flags, 0, section.offset, #section.data, link, info,
section.align or 1, section.entsize or 0))
end
return table.concat(out)
end
---
-- The object as bytes. The symbol and string tables are written here, last, so that every name
-- they carry is already interned.
-- @function luaebpf.elf.object:pack
-- @treturn string
function object:pack()
self:section{name = ".symtab", type = elf.section.SYMTAB, flags = 0, data = table.concat(self.symbols),
link = ".strtab", info = self.nlocals, align = 8, entsize = SYMSIZE}
local shstrndx = self:section{name = ".strtab", type = elf.section.STRTAB, flags = 0, align = 1}
self.sections[shstrndx + 1].data = self.names:pack()
local shoff = place(self)
shoff = shoff + (8 - shoff % 8) % 8
local data = self.order == ">" and ELFDATA2MSB or ELFDATA2LSB
local out = {"\x7fELF", string.pack("=I1I1I1I1I1", 2, data, 1, 0, 0), ("\0"):rep(7),
string.pack(self.order .. "I2I2I4I8I8I8I4I2I2I2I2I2I2", ET_REL, EM_BPF, 1, 0, 0, shoff, 0,
EHSIZE, 0, 0, SHENTSIZE, #self.sections, shstrndx)}
local at = EHSIZE
for i = 2, #self.sections do
local section = self.sections[i]
table.insert(out, ("\0"):rep(section.offset - at))
table.insert(out, section.data)
at = section.offset + #section.data
end
table.insert(out, ("\0"):rep(shoff - at))
table.insert(out, headers(self))
return table.concat(out)
end
return elf