Debian Codegraph @

btf/btf_types.go

raw symbols SCIP
1 package btf
2
3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "unsafe"
8 )
9
10 //go:generate go tool stringer -linecomment -output=btf_types_string.go -type=FuncLinkage,VarLinkage,btfKind
11
12 // btfKind describes a Type.
13 type btfKind uint8
14
15 // Equivalents of the BTF_KIND_* constants.
16 const (
17 kindUnknown btfKind = iota // Unknown
18 kindInt // Int
19 kindPointer // Pointer
20 kindArray // Array
21 kindStruct // Struct
22 kindUnion // Union
23 kindEnum // Enum
24 kindForward // Forward
25 kindTypedef // Typedef
26 kindVolatile // Volatile
27 kindConst // Const
28 kindRestrict // Restrict
29 // Added ~4.20
30 kindFunc // Func
31 kindFuncProto // FuncProto
32 // Added ~5.1
33 kindVar // Var
34 kindDatasec // Datasec
35 // Added ~5.13
36 kindFloat // Float
37 // Added 5.16
38 kindDeclTag // DeclTag
39 // Added 5.17
40 kindTypeTag // TypeTag
41 // Added 6.0
42 kindEnum64 // Enum64
43 )
44
45 // FuncLinkage describes BTF function linkage metadata.
46 type FuncLinkage int
47
48 // Equivalent of enum btf_func_linkage.
49 const (
50 StaticFunc FuncLinkage = iota // static
51 GlobalFunc // global
52 ExternFunc // extern
53 )
54
55 // VarLinkage describes BTF variable linkage metadata.
56 type VarLinkage int
57
58 const (
59 StaticVar VarLinkage = iota // static
60 GlobalVar // global
61 ExternVar // extern
62 )
63
64 const (
65 btfTypeKindShift = 24
66 btfTypeKindLen = 5
67 btfTypeVlenShift = 0
68 btfTypeVlenMask = 16
69 btfTypeKindFlagShift = 31
70 btfTypeKindFlagMask = 1
71 )
72
73 var btfHeaderLen = binary.Size(&btfHeader{})
74
75 type btfHeader struct {
76 Magic uint16
77 Version uint8
78 Flags uint8
79 HdrLen uint32
80
81 TypeOff uint32
82 TypeLen uint32
83 StringOff uint32
84 StringLen uint32
85 }
86
87 type btfLayout struct {
88 Off uint32
89 Len uint32
90 }
91
92 // parseBTFHeader parses the header of the .BTF section.
93 func parseBTFHeader(buf []byte) (*btfHeader, *btfLayout, binary.ByteOrder, error) {
94 var header btfHeader
95 var bo binary.ByteOrder
96 for _, order := range []binary.ByteOrder{binary.LittleEndian, binary.BigEndian} {
97 n, err := binary.Decode(buf, order, &header)
98 if err != nil {
99 return nil, nil, nil, fmt.Errorf("read header: %v", err)
100 }
101
102 if header.Magic != btfMagic {
103 continue
104 }
105
106 buf = buf[n:]
107 bo = order
108 break
109 }
110
111 if bo == nil {
112 return nil, nil, nil, fmt.Errorf("no valid BTF header")
113 }
114
115 if header.Version != 1 {
116 return nil, nil, nil, fmt.Errorf("unexpected version %v", header.Version)
117 }
118
119 if header.Flags != 0 {
120 return nil, nil, nil, fmt.Errorf("unsupported flags %v", header.Flags)
121 }
122
123 remainder := int64(header.HdrLen) - int64(binary.Size(&header))
124 if remainder < 0 {
125 return nil, nil, nil, errors.New("header length shorter than minimum BTF header size")
126 }
127
128 if len(buf) < int(remainder) {
129 return nil, nil, nil, errors.New("header length exceeds available data")
130 }
131
132 var layout btfLayout
133 if remainder >= int64(binary.Size(&btfLayout{})) {
134 n, err := binary.Decode(buf, bo, &layout)
135 if err != nil {
136 return nil, nil, nil, fmt.Errorf("read layout offset and length: %v", err)
137 }
138 buf = buf[n:]
139 remainder -= int64(n)
140 }
141
142 for _, b := range buf[:remainder] {
143 if b != 0 {
144 return nil, nil, nil, errors.New("header contains non-zero trailer")
145 }
146 }
147
148 return &header, &layout, bo, nil
149 }
150
151 // btfType is equivalent to struct btf_type in Documentation/bpf/btf.rst.
152 type btfType struct {
153 NameOff uint32
154 /* "info" bits arrangement
155 * bits 0-15: vlen (e.g. # of struct's members), linkage
156 * bits 16-23: unused
157 * bits 24-28: kind (e.g. int, ptr, array...etc)
158 * bits 29-30: unused
159 * bit 31: kind_flag, currently used by
160 * struct, union and fwd
161 */
162 Info uint32
163 /* "size" is used by INT, ENUM, STRUCT and UNION.
164 * "size" tells the size of the type it is describing.
165 *
166 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
167 * FUNC and FUNC_PROTO.
168 * "type" is a type_id referring to another type.
169 */
170 SizeType uint32
171 }
172
173 var btfTypeSize = int(unsafe.Sizeof(btfType{}))
174
175 func unmarshalBtfType(bt *btfType, b []byte, bo binary.ByteOrder) (int, error) {
176 if len(b) < btfTypeSize {
177 return 0, fmt.Errorf("not enough bytes to unmarshal btfType")
178 }
179
180 bt.NameOff = bo.Uint32(b[0:])
181 bt.Info = bo.Uint32(b[4:])
182 bt.SizeType = bo.Uint32(b[8:])
183 return btfTypeSize, nil
184 }
185
186 func mask(len uint32) uint32 {
187 return (1 << len) - 1
188 }
189
190 func readBits(value, len, shift uint32) uint32 {
191 return (value >> shift) & mask(len)
192 }
193
194 func writeBits(value, len, shift, new uint32) uint32 {
195 value &^= mask(len) << shift
196 value |= (new & mask(len)) << shift
197 return value
198 }
199
200 func (bt *btfType) info(len, shift uint32) uint32 {
201 return readBits(bt.Info, len, shift)
202 }
203
204 func (bt *btfType) setInfo(value, len, shift uint32) {
205 bt.Info = writeBits(bt.Info, len, shift, value)
206 }
207
208 func (bt *btfType) Kind() btfKind {
209 return btfKind(bt.info(btfTypeKindLen, btfTypeKindShift))
210 }
211
212 func (bt *btfType) SetKind(kind btfKind) {
213 bt.setInfo(uint32(kind), btfTypeKindLen, btfTypeKindShift)
214 }
215
216 func (bt *btfType) Vlen() int {
217 return int(bt.info(btfTypeVlenMask, btfTypeVlenShift))
218 }
219
220 func (bt *btfType) SetVlen(vlen int) {
221 bt.setInfo(uint32(vlen), btfTypeVlenMask, btfTypeVlenShift)
222 }
223
224 func (bt *btfType) kindFlagBool() bool {
225 return bt.info(btfTypeKindFlagMask, btfTypeKindFlagShift) == 1
226 }
227
228 func (bt *btfType) setKindFlagBool(set bool) {
229 var value uint32
230 if set {
231 value = 1
232 }
233 bt.setInfo(value, btfTypeKindFlagMask, btfTypeKindFlagShift)
234 }
235
236 // Bitfield returns true if the struct or union contain a bitfield.
237 func (bt *btfType) Bitfield() bool {
238 return bt.kindFlagBool()
239 }
240
241 func (bt *btfType) SetBitfield(isBitfield bool) {
242 bt.setKindFlagBool(isBitfield)
243 }
244
245 func (bt *btfType) FwdKind() FwdKind {
246 return FwdKind(bt.info(btfTypeKindFlagMask, btfTypeKindFlagShift))
247 }
248
249 func (bt *btfType) SetFwdKind(kind FwdKind) {
250 bt.setInfo(uint32(kind), btfTypeKindFlagMask, btfTypeKindFlagShift)
251 }
252
253 func (bt *btfType) Signed() bool {
254 return bt.kindFlagBool()
255 }
256
257 func (bt *btfType) SetSigned(signed bool) {
258 bt.setKindFlagBool(signed)
259 }
260
261 func (bt *btfType) Linkage() FuncLinkage {
262 return FuncLinkage(bt.info(btfTypeVlenMask, btfTypeVlenShift))
263 }
264
265 func (bt *btfType) SetLinkage(linkage FuncLinkage) {
266 bt.setInfo(uint32(linkage), btfTypeVlenMask, btfTypeVlenShift)
267 }
268
269 func (bt *btfType) Type() TypeID {
270 // TODO: Panic here if wrong kind?
271 return TypeID(bt.SizeType)
272 }
273
274 func (bt *btfType) SetType(id TypeID) {
275 bt.SizeType = uint32(id)
276 }
277
278 func (bt *btfType) Size() uint32 {
279 // TODO: Panic here if wrong kind?
280 return bt.SizeType
281 }
282
283 func (bt *btfType) SetSize(size uint32) {
284 bt.SizeType = size
285 }
286
287 func (bt *btfType) Encode(buf []byte, bo binary.ByteOrder) (int, error) {
288 if len(buf) < btfTypeSize {
289 return 0, fmt.Errorf("not enough bytes to marshal btfType")
290 }
291 bo.PutUint32(buf[0:], bt.NameOff)
292 bo.PutUint32(buf[4:], bt.Info)
293 bo.PutUint32(buf[8:], bt.SizeType)
294 return btfTypeSize, nil
295 }
296
297 // DataLen returns the length of additional type specific data in bytes.
298 func (bt *btfType) DataLen() (int, error) {
299 switch bt.Kind() {
300 case kindInt:
301 return int(unsafe.Sizeof(btfInt{})), nil
302 case kindPointer:
303 case kindArray:
304 return int(unsafe.Sizeof(btfArray{})), nil
305 case kindStruct:
306 fallthrough
307 case kindUnion:
308 return int(unsafe.Sizeof(btfMember{})) * bt.Vlen(), nil
309 case kindEnum:
310 return int(unsafe.Sizeof(btfEnum{})) * bt.Vlen(), nil
311 case kindForward:
312 case kindTypedef:
313 case kindVolatile:
314 case kindConst:
315 case kindRestrict:
316 case kindFunc:
317 case kindFuncProto:
318 return int(unsafe.Sizeof(btfParam{})) * bt.Vlen(), nil
319 case kindVar:
320 return int(unsafe.Sizeof(btfVariable{})), nil
321 case kindDatasec:
322 return int(unsafe.Sizeof(btfVarSecinfo{})) * bt.Vlen(), nil
323 case kindFloat:
324 case kindDeclTag:
325 return int(unsafe.Sizeof(btfDeclTag{})), nil
326 case kindTypeTag:
327 case kindEnum64:
328 return int(unsafe.Sizeof(btfEnum64{})) * bt.Vlen(), nil
329 default:
330 return 0, fmt.Errorf("unknown kind: %v", bt.Kind())
331 }
332
333 return 0, nil
334 }
335
336 // btfInt encodes additional data for integers.
337 //
338 // ? ? ? ? e e e e o o o o o o o o ? ? ? ? ? ? ? ? b b b b b b b b
339 // ? = undefined
340 // e = encoding
341 // o = offset (bitfields?)
342 // b = bits (bitfields)
343 type btfInt struct {
344 Raw uint32
345 }
346
347 const (
348 btfIntEncodingLen = 4
349 btfIntEncodingShift = 24
350 btfIntOffsetLen = 8
351 btfIntOffsetShift = 16
352 btfIntBitsLen = 8
353 btfIntBitsShift = 0
354 )
355
356 var btfIntLen = int(unsafe.Sizeof(btfInt{}))
357
358 func unmarshalBtfInt(bi *btfInt, b []byte, bo binary.ByteOrder) (int, error) {
359 if len(b) < btfIntLen {
360 return 0, fmt.Errorf("not enough bytes to unmarshal btfInt")
361 }
362
363 bi.Raw = bo.Uint32(b[0:])
364 return btfIntLen, nil
365 }
366
367 func (bi btfInt) Encoding() IntEncoding {
368 return IntEncoding(readBits(bi.Raw, btfIntEncodingLen, btfIntEncodingShift))
369 }
370
371 func (bi *btfInt) SetEncoding(e IntEncoding) {
372 bi.Raw = writeBits(uint32(bi.Raw), btfIntEncodingLen, btfIntEncodingShift, uint32(e))
373 }
374
375 func (bi btfInt) Offset() Bits {
376 return Bits(readBits(bi.Raw, btfIntOffsetLen, btfIntOffsetShift))
377 }
378
379 func (bi *btfInt) SetOffset(offset uint32) {
380 bi.Raw = writeBits(bi.Raw, btfIntOffsetLen, btfIntOffsetShift, offset)
381 }
382
383 func (bi btfInt) Bits() Bits {
384 return Bits(readBits(bi.Raw, btfIntBitsLen, btfIntBitsShift))
385 }
386
387 func (bi *btfInt) SetBits(bits byte) {
388 bi.Raw = writeBits(bi.Raw, btfIntBitsLen, btfIntBitsShift, uint32(bits))
389 }
390
391 type btfArray struct {
392 Type TypeID
393 IndexType TypeID
394 Nelems uint32
395 }
396
397 var btfArrayLen = int(unsafe.Sizeof(btfArray{}))
398
399 func unmarshalBtfArray(ba *btfArray, b []byte, bo binary.ByteOrder) (int, error) {
400 if len(b) < btfArrayLen {
401 return 0, fmt.Errorf("not enough bytes to unmarshal btfArray")
402 }
403
404 ba.Type = TypeID(bo.Uint32(b[0:]))
405 ba.IndexType = TypeID(bo.Uint32(b[4:]))
406 ba.Nelems = bo.Uint32(b[8:])
407 return btfArrayLen, nil
408 }
409
410 type btfMember struct {
411 NameOff uint32
412 Type TypeID
413 Offset uint32
414 }
415
416 var btfMemberLen = int(unsafe.Sizeof(btfMember{}))
417
418 func unmarshalBtfMember(bm *btfMember, b []byte, bo binary.ByteOrder) (int, error) {
419 if btfMemberLen > len(b) {
420 return 0, fmt.Errorf("not enough bytes to unmarshal btfMember")
421 }
422
423 bm.NameOff = bo.Uint32(b[0:])
424 bm.Type = TypeID(bo.Uint32(b[4:]))
425 bm.Offset = bo.Uint32(b[8:])
426 return btfMemberLen, nil
427 }
428
429 type btfVarSecinfo struct {
430 Type TypeID
431 Offset uint32
432 Size uint32
433 }
434
435 var btfVarSecinfoLen = int(unsafe.Sizeof(btfVarSecinfo{}))
436
437 func unmarshalBtfVarSecInfo(bvsi *btfVarSecinfo, b []byte, bo binary.ByteOrder) (int, error) {
438 if len(b) < btfVarSecinfoLen {
439 return 0, fmt.Errorf("not enough bytes to unmarshal btfVarSecinfo")
440 }
441
442 bvsi.Type = TypeID(bo.Uint32(b[0:]))
443 bvsi.Offset = bo.Uint32(b[4:])
444 bvsi.Size = bo.Uint32(b[8:])
445 return btfVarSecinfoLen, nil
446 }
447
448 type btfVariable struct {
449 Linkage uint32
450 }
451
452 var btfVariableLen = int(unsafe.Sizeof(btfVariable{}))
453
454 func unmarshalBtfVariable(bv *btfVariable, b []byte, bo binary.ByteOrder) (int, error) {
455 if len(b) < btfVariableLen {
456 return 0, fmt.Errorf("not enough bytes to unmarshal btfVariable")
457 }
458
459 bv.Linkage = bo.Uint32(b[0:])
460 return btfVariableLen, nil
461 }
462
463 type btfEnum struct {
464 NameOff uint32
465 Val uint32
466 }
467
468 var btfEnumLen = int(unsafe.Sizeof(btfEnum{}))
469
470 func unmarshalBtfEnum(be *btfEnum, b []byte, bo binary.ByteOrder) (int, error) {
471 if btfEnumLen > len(b) {
472 return 0, fmt.Errorf("not enough bytes to unmarshal btfEnum")
473 }
474
475 be.NameOff = bo.Uint32(b[0:])
476 be.Val = bo.Uint32(b[4:])
477 return btfEnumLen, nil
478 }
479
480 type btfEnum64 struct {
481 NameOff uint32
482 ValLo32 uint32
483 ValHi32 uint32
484 }
485
486 var btfEnum64Len = int(unsafe.Sizeof(btfEnum64{}))
487
488 func unmarshalBtfEnum64(enum *btfEnum64, b []byte, bo binary.ByteOrder) (int, error) {
489 if len(b) < btfEnum64Len {
490 return 0, fmt.Errorf("not enough bytes to unmarshal btfEnum64")
491 }
492
493 enum.NameOff = bo.Uint32(b[0:])
494 enum.ValLo32 = bo.Uint32(b[4:])
495 enum.ValHi32 = bo.Uint32(b[8:])
496
497 return btfEnum64Len, nil
498 }
499
500 type btfParam struct {
501 NameOff uint32
502 Type TypeID
503 }
504
505 var btfParamLen = int(unsafe.Sizeof(btfParam{}))
506
507 func unmarshalBtfParam(param *btfParam, b []byte, bo binary.ByteOrder) (int, error) {
508 if len(b) < btfParamLen {
509 return 0, fmt.Errorf("not enough bytes to unmarshal btfParam")
510 }
511
512 param.NameOff = bo.Uint32(b[0:])
513 param.Type = TypeID(bo.Uint32(b[4:]))
514
515 return btfParamLen, nil
516 }
517
518 type btfDeclTag struct {
519 ComponentIdx uint32
520 }
521
522 var btfDeclTagLen = int(unsafe.Sizeof(btfDeclTag{}))
523
524 func unmarshalBtfDeclTag(bdt *btfDeclTag, b []byte, bo binary.ByteOrder) (int, error) {
525 if len(b) < btfDeclTagLen {
526 return 0, fmt.Errorf("not enough bytes to unmarshal btfDeclTag")
527 }
528
529 bdt.ComponentIdx = bo.Uint32(b[0:])
530 return btfDeclTagLen, nil
531 }