Repository navigation
Expand file tree
/
Copy pathbtf.lua
More file actions
243 lines (218 loc) · 7.88 KB
/
Copy pathbtf.lua
File metadata and controls
243 lines (218 loc) · 7.88 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
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
--
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
--
---
-- The `.BTF` and `.BTF.ext` sections: a `FUNC` and a `FUNC_PROTO` per compiled function and per
-- kfunc a call names, and the `func_info` and `line_info` records that let the verifier's log quote
-- the Lua source.
--
-- In the object an `insn_off` is a byte offset into its section; the kernel takes an index in
-- instructions, and libbpf converts on the way in.
-- @module luaebpf.btf
local class = require("lunatik.class")
local strtab = require("luaebpf.strtab")
local MAGIC <const> = 0xeb9f
local VERSION <const> = 1
local HDRLEN <const> = 24
local EXTLEN <const> = 32
local FUNCREC <const> = 8
local LINEREC <const> = 16
local KIND_INT <const> = 1
local KIND_PTR <const> = 2
local KIND_ARRAY <const> = 3
local KIND_STRUCT <const> = 4
local KIND_FUNC <const> = 12
local KIND_FUNC_PROTO <const> = 13
local KIND_VAR <const> = 14
local KIND_DATASEC <const> = 15
local SIGNED <const> = 1
local STATIC <const> = 0
local GLOBAL <const> = 1
local EXTERN <const> = 2
local ALLOCATED <const> = 1 -- BTF_VAR_GLOBAL_ALLOCATED
local BYTE <const> = 8
local WORD <const> = 8 -- a map attribute is a pointer, which is what libbpf reads
local btf = {}
--- Function linkage, as `BTF_FUNC_*` names it.
-- @table luaebpf.btf.linkage
btf.linkage = {STATIC = STATIC, GLOBAL = GLOBAL, EXTERN = EXTERN}
---
-- A BTF section under construction.
-- @type luaebpf.btf.types
local types = class{}
local function typeinfo(kind, vlen)
return (kind << 24) | vlen
end
---
-- A fresh BTF section carrying the two integer types every compiled function is written in.
-- @function luaebpf.btf.new
-- @tparam boolean bigendian whether the section is written big-endian
-- @treturn luaebpf.btf.types
function btf.new(bigendian)
local self = types:new{records = {}, n = 0, names = strtab.new(), order = bigendian and ">" or "<"}
self.int = self:integer("int", 4, 32)
self.long = self:integer("long", 8, 64)
return self
end
---
-- Interns a string in the BTF string section.
-- @function luaebpf.btf.types:string
-- @tparam string name
-- @treturn integer its offset
function types:string(name)
return self.names:intern(name)
end
---
-- Appends a raw type record.
-- @function luaebpf.btf.types:add
-- @tparam string record
-- @treturn integer its type id
function types:add(record)
table.insert(self.records, record)
self.n = self.n + 1
return self.n
end
---
-- A signed integer type.
-- @function luaebpf.btf.types:integer
-- @tparam string name
-- @tparam integer size in bytes
-- @tparam integer bits
-- @treturn integer its type id
function types:integer(name, size, bits)
return self:add(string.pack(self.order .. "I4I4I4I4", self:string(name), typeinfo(KIND_INT, 0), size,
(SIGNED << 24) | bits))
end
---
-- A function and the prototype under it. Parameters are named, as the kernel's BTF checker
-- expects of a prototype that names any of them.
-- @function luaebpf.btf.types:func
-- @tparam string name
-- @tparam integer linkage one of `btf.linkage`
-- @tparam table signature `{result, params}`, the result's type id and a type id per parameter
-- @treturn integer the `FUNC` type id
function types:func(name, linkage, signature)
local params = {}
for i, id in ipairs(signature.params) do
table.insert(params, string.pack(self.order .. "I4I4", self:string("a" .. i), id))
end
local proto = self:add(string.pack(self.order .. "I4I4I4", 0, typeinfo(KIND_FUNC_PROTO, #params), signature.result)
.. table.concat(params))
return self:add(string.pack(self.order .. "I4I4I4", self:string(name), typeinfo(KIND_FUNC, linkage), proto))
end
---
-- A pointer to `id`.
-- @function luaebpf.btf.types:pointer
-- @tparam integer id
-- @treturn integer its type id
function types:pointer(id)
return self:add(string.pack(self.order .. "I4I4I4", 0, typeinfo(KIND_PTR, 0), id))
end
---
-- An array of `n` elements of `id`, indexed by `self.int`. libbpf reads a BTF-defined map's
-- attributes out of the element count of such an array, so `n` is the number that matters.
-- @function luaebpf.btf.types:array
-- @tparam integer id
-- @tparam integer n
-- @treturn integer its type id
function types:array(id, n)
return self:add(string.pack(self.order .. "I4I4I4", 0, typeinfo(KIND_ARRAY, 0), 0)
.. string.pack(self.order .. "I4I4I4", id, self.int, n))
end
---
-- A struct of `members`, each `{name, type}`, laid out one word apart.
-- @function luaebpf.btf.types:struct
-- @tparam string name
-- @tparam table members
-- @treturn integer the `STRUCT` type id
-- @treturn integer its size in bytes
function types:struct(name, members)
local fields, size = {}, #members * WORD
for i, member in ipairs(members) do
table.insert(fields, string.pack(self.order .. "I4I4I4", self:string(member.name), member.type,
(i - 1) * WORD * BYTE))
end
return self:add(string.pack(self.order .. "I4I4I4", self:string(name), typeinfo(KIND_STRUCT, #members), size)
.. table.concat(fields)), size
end
---
-- A variable of `id` allocated in a section.
-- @function luaebpf.btf.types:var
-- @tparam string name
-- @tparam integer id
-- @treturn integer its type id
function types:var(name, id)
return self:add(string.pack(self.order .. "I4I4I4", self:string(name), typeinfo(KIND_VAR, 0), id)
.. string.pack(self.order .. "I4", ALLOCATED))
end
---
-- A data section holding `entries`, each `{type, offset, size}`.
-- @function luaebpf.btf.types:datasec
-- @tparam string name
-- @tparam table entries
-- @treturn integer its type id
function types:datasec(name, entries)
local records, size = {}, 0
for _, entry in ipairs(entries) do
table.insert(records, string.pack(self.order .. "I4I4I4", entry.type, entry.offset, entry.size))
size = size + entry.size
end
return self:add(string.pack(self.order .. "I4I4I4", self:string(name), typeinfo(KIND_DATASEC, #entries), size)
.. table.concat(records))
end
---
-- The `.BTF` section.
-- @function luaebpf.btf.types:pack
-- @treturn string
function types:pack()
local body, strings = table.concat(self.records), self.names:pack()
return string.pack(self.order .. "I2I1I1I4I4I4I4I4", MAGIC, VERSION, 0, HDRLEN, 0, #body, #body, #strings)
.. body .. strings
end
local function segment(self, sections, recsize, field)
local out = {string.pack(self.order .. "I4", recsize)}
for _, section in ipairs(sections) do
local records = section[field]
if #records > 0 then
table.insert(out, string.pack(self.order .. "I4I4", self:string(section.name), #records))
for _, record in ipairs(records) do
table.insert(out, record)
end
end
end
return table.concat(out)
end
---
-- One `func_info` record: the byte offset of the function's first instruction, and its `FUNC`.
-- @function luaebpf.btf.types:funcinfo
-- @tparam integer at
-- @tparam integer id
-- @treturn string
function types:funcinfo(at, id)
return string.pack(self.order .. "I4I4", at, id)
end
---
-- One `line_info` record. `line` and `column` are packed as the kernel reads them back.
-- @function luaebpf.btf.types:lineinfo
-- @tparam integer at byte offset of the instruction
-- @tparam integer file offset of the file name in the BTF strings
-- @tparam integer text offset of the source line in the BTF strings
-- @tparam integer line
-- @tparam integer column
-- @treturn string
function types:lineinfo(at, file, text, line, column)
return string.pack(self.order .. "I4I4I4I4", at, file, text, (line << 10) | column)
end
---
-- The `.BTF.ext` section over `sections`, each `{name, funcs, lines}`.
-- @function luaebpf.btf.types:ext
-- @tparam table sections
-- @treturn string
function types:ext(sections)
local funcs = segment(self, sections, FUNCREC, "funcs")
local lines = segment(self, sections, LINEREC, "lines")
return string.pack(self.order .. "I2I1I1I4I4I4I4I4I4I4", MAGIC, VERSION, 0, EXTLEN, 0, #funcs, #funcs, #lines,
#funcs + #lines, 0) .. funcs .. lines
end
return btf