/* btf.pk - BTF implementation for GNU poke. */ /* Copyright (C) 2019, 2020, 2021 Jose E. Marchesi. */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* This file contains a description of the Linux BPF Type Format, as documented in https://www.kernel.org/doc/html/latest/bpf/btf.html (fetched 2019-10-25) */ var BTF_KIND_UNKNOWN = 0, BTF_KIND_INT = 1, BTF_KIND_PTR = 2, BTF_KIND_ARRAY = 3, BTF_KIND_STRUCT = 4, BTF_KIND_UNION = 5, BTF_KIND_ENUM = 6, BTF_KIND_FWD = 7, BTF_KIND_TYPEDEF = 8, BTF_KIND_VOLATILE = 9, BTF_KIND_CONST = 10, BTF_KIND_RESTRICT = 11, BTF_KIND_FUNC = 12, BTF_KIND_FUNC_PROTO = 13, BTF_KIND_VAR = 14, BTF_KIND_DATASEC = 15; var btf_kind_names = ["unkn", "int", "ptr", "array", "struct", "union", "enum", "fwd", "typedef", "volatile", "const", "restrict", "func", "func_proto", "var", "datasec"]; type BTF_Type_Id = uint<32>; type BTF_Int = struct uint<32> { uint<4>; uint<1>; uint<1> bool_p; uint<1> char_p; uint<1> signed_p; offset,b> offset; uint<8>; offset,b> bits; /* XXX turn `encoding' into a nested integral struct once we support it in the compiler. */ method encoding = uint<4>: { return bool_p:::char_p:::signed_p; } }; type BTF_Array = struct { BTF_Type_Id elem_type; uint<32> index_type; uint<32> nelems; }; type BTF_Enum = struct { offset,B> name; int<32> val; }; type BTF_Param = struct { offset,B> name; BTF_Type_Id param_type; }; type BTF_Variable = struct { uint<32> linkage; }; type BTF_Var_SecInfo = struct { BTF_Type_Id var_type; offset,B> offset; offset,B> size; }; type BTF_Type = struct { offset,B> name; struct uint<32> { uint<1> kind_flag; uint<3>; uint<4> kind; uint<8>; uint<16> vlen; method _print = void: { printf ("#<%s,kind_flag:%u32d,vlen:%v>", btf_kind_names[kind], kind_flag, vlen); } } info; union { offset,B> size : (info.kind in [BTF_KIND_INT, BTF_KIND_ENUM, BTF_KIND_STRUCT, BTF_KIND_UNION]); BTF_Type_Id type_id; } attrs; /* XXX move these types to the top-level once we get support to pass arguments to struct types. */ type BTF_Member = struct { offset,B> name; BTF_Type_Id type_id; union { offset,b> member_offset : !info.kind_flag; struct uint<32> { offset,b> bitfield_size; offset,b> bit_offset; } bitfield; } offset; }; type BTF_Func_Proto = struct { BTF_Param[info.vlen] params; }; union { BTF_Int integer : info.kind == BTF_KIND_INT; BTF_Array array : info.kind == BTF_KIND_ARRAY; BTF_Enum[info.vlen] enum : info.kind == BTF_KIND_ENUM; BTF_Func_Proto func_proto : info.kind == BTF_KIND_FUNC_PROTO; BTF_Variable variable : info.kind == BTF_KIND_VAR; BTF_Member[info.vlen] members : (info.kind == BTF_KIND_UNION || info.kind == BTF_KIND_STRUCT); BTF_Var_SecInfo[info.vlen] datasec : info.kind == BTF_KIND_DATASEC; struct {} nothing; } data; method vararg_p = int: { var last_param = data.func_proto.params[info.vlen - 1]; return (last_param.name == 0#B && last_param.param_type == 0); } method get_kind_name = string: { return btf_kind_names[info.kind]; } }; type BTF_Header = struct { uint<16> magic : ((magic == 0x9feb && set_endian (ENDIAN_BIG) || (magic == 0xeb9f && set_endian (ENDIAN_LITTLE)))) = (get_endian == ENDIAN_BIG ? 0x9feb : 0xeb9f); uint<8> version; uint<8> flags; offset,B> hdr_len; /* Size of this header. */ /* The offsets below are relative to the end of this header. */ offset,B> type_off; offset,B> type_len; offset,B> str_off; offset,B> str_len; }; type BTF_Section = struct { BTF_Header header : header.hdr_len == header'size; BTF_Type[header.type_len] types @ header.hdr_len + header.type_off; string[header.str_len] strings @ header.hdr_len + header.str_off; /* Given an offset into the BTF strings section, return the string. */ method get_string = (offset,B> off) string: { return string @ strings'offset + off; } };