zig/lib/std / os/linux/bpf/btf.zig

const std = @import("../../../std.zig");

magic

pub const magic = 0xeb9f;

version

pub const version = 1;

ext

btf_ext.zig
pub const ext = @import("btf_ext.zig");

Header

All offsets are in bytes relative to the end of this header

pub const Header = extern struct {
    magic: u16,
    version: u8,
    flags: u8,
    hdr_len: u32,

    type_off: u32,

    type_len: u32,

    str_off: u32,

    str_len: u32,
};

max_type

offset of type section length of type section offset of string section length of string section Max number of type identifiers

pub const max_type = 0xfffff;

max_name_offset

Max offset into string section

pub const max_name_offset = 0xffffff;

max_vlen

Max number of struct/union/enum member of func args

pub const max_vlen = 0xffff;

Type

pub const Type = extern struct {
    name_off: u32,
    info: packed struct(u32) {
        vlen: u16,

        unused_1: u8,
        kind: Kind,
        unused_2: u2,

        kind_flag: bool,
    },

    size_type: extern union { size: u32, typ: u32 },
};

Kind

number of struct's members used by Struct, Union, and Fwd size is used by Int, Enum, Struct, Union, and DataSec, it tells the size of the type it is describing

type is used by Ptr, Typedef, Volatile, Const, Restrict, Func, FuncProto, and Var. It is a type_id referring to another type For some kinds, Type is immediately followed by extra data

pub const Kind = enum(u5) {
    unknown,
    int,
    ptr,
    array,
    @"struct",
    @"union",
    @"enum",
    fwd,
    typedef,
    @"volatile",
    @"const",
    restrict,
    func,
    func_proto,
    @"var",
    datasec,
    float,
    decl_tag,
    type_tag,
    enum64,
};

IntInfo

int kind is followed by this struct

pub const IntInfo = packed struct(u32) {
    bits: u8,
    unused: u8,
    offset: u8,
    encoding: enum(u4) {
        signed = 1 << 0,
        char = 1 << 1,
        boolean = 1 << 2,
    },
};

Test:

IntInfo is 32 bits

test "IntInfo is 32 bits" {
    try std.testing.expectEqual(@bitSizeOf(IntInfo), 32);
}

Enum

enum kind is followed by this struct

pub const Enum = extern struct {
    name_off: u32,
    val: i32,
};

Enum64

enum64 kind is followed by this struct

pub const Enum64 = extern struct {
    name_off: u32,
    val_lo32: i32,
    val_hi32: i32,
};

Array

array kind is followed by this struct

pub const Array = extern struct {
    typ: u32,
    index_type: u32,
    nelems: u32,
};

Member

struct and union kinds are followed by multiple Member structs. The exact number is stored in vlen

pub const Member = extern struct {
    name_off: u32,
    typ: u32,

    offset: packed struct(u32) {
        bit: u24,
        bitfield_size: u8,
    },
};

Param

if the kind_flag is set, offset contains both member bitfield size and bit offset, the bitfield size is set for bitfield members. If the type info kind_flag is not set, the offset contains only bit offset func_proto is followed by multiple Params, the exact number is stored in vlen

pub const Param = extern struct {
    name_off: u32,
    typ: u32,
};

VarLinkage

pub const VarLinkage = enum {
    static,
    global_allocated,
    global_extern,
};

FuncLinkage

pub const FuncLinkage = enum {
    static,
    global,
    external,
};

Var

var kind is followed by a single Var struct to describe additional information related to the variable such as its linkage

pub const Var = extern struct {
    linkage: u32,
};

VarSecInfo

datasec kind is followed by multiple VarSecInfo to describe all Var kind types it contains along with it's in-section offset as well as size.

pub const VarSecInfo = extern struct {
    typ: u32,
    offset: u32,
    size: u32,
};

// decl_tag kind is followed by a single DeclTag struct to describe
// additional information related to the tag applied location.
// If component_idx == -1, the tag is applied to a struct, union,
// variable or function. Otherwise, it is applied to a struct/union
// member or a func argument, and component_idx indicates which member
// or argument (0 ... vlen-1).

DeclTag

pub const DeclTag = extern struct {
    component_idx: u32,
};