| 1 |
/* Declarations and definitions relating to the BPF Type Format (BTF). |
| 2 |
Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 14 |
License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
/* This file is derived from the BTF specification described in the |
| 21 |
Linux kernel source tree (linux/Documentation/bpf/btf.rst). */ |
| 22 |
|
| 23 |
#ifndef _BTF_H_ |
| 24 |
#define _BTF_H_ |
| 25 |
|
| 26 |
#include <stdint.h> |
| 27 |
|
| 28 |
#ifdef __cplusplus |
| 29 |
extern "C" |
| 30 |
{ |
| 31 |
#endif |
| 32 |
|
| 33 |
/* BTF magic number to identify header, endianness. */ |
| 34 |
#define BTF_MAGIC 0xeb9f |
| 35 |
/* Data format version number. */ |
| 36 |
#define BTF_VERSION 1 |
| 37 |
|
| 38 |
struct btf_header |
| 39 |
{ |
| 40 |
uint16_t magic; /* Magic number (BTF_MAGIC). */ |
| 41 |
uint8_t version; /* Data format version (BTF_VERSION). */ |
| 42 |
uint8_t flags; /* Flags. Currently unused. */ |
| 43 |
uint32_t hdr_len; /* Length of this header (sizeof (struct btf_header)). */ |
| 44 |
|
| 45 |
/* Following offsets are relative to the end of this header. */ |
| 46 |
uint32_t type_off; /* Offset of type section, in bytes. */ |
| 47 |
uint32_t type_len; /* Length of type section, in bytes. */ |
| 48 |
uint32_t str_off; /* Offset of string section, in bytes. */ |
| 49 |
uint32_t str_len; /* Length of string section, in bytes. */ |
| 50 |
}; |
| 51 |
|
| 52 |
/* Maximum type identifier. */ |
| 53 |
#define BTF_MAX_TYPE 0x000fffff |
| 54 |
/* Maximum offset into the string section. */ |
| 55 |
#define BTF_MAX_NAME_OFFSET 0x00ffffff |
| 56 |
/* Maximum number of struct, union, enum members or func args. */ |
| 57 |
#define BTF_MAX_VLEN 0xffff |
| 58 |
|
| 59 |
/* Type ID 0 represents the void type. */ |
| 60 |
#define BTF_VOID_TYPEID 0 |
| 61 |
/* Initial type ID for regular types. */ |
| 62 |
#define BTF_INIT_TYPEID 1 |
| 63 |
|
| 64 |
struct btf_type |
| 65 |
{ |
| 66 |
uint32_t name_off; /* Offset in string section of type name. */ |
| 67 |
uint32_t info; /* Encoded kind, variant length, kind flag: |
| 68 |
- bits 0-15: vlen |
| 69 |
- bits 16-23: unused |
| 70 |
- bits 24-28: kind |
| 71 |
- bits 29-30: unused |
| 72 |
- bit 31: kind_flag |
| 73 |
See accessor macros below. */ |
| 74 |
|
| 75 |
/* SIZE is used by INT, ENUM, STRUCT, UNION, DATASEC kinds. |
| 76 |
TYPE is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, FUNC, |
| 77 |
FUNC_PROTO and VAR kinds. */ |
| 78 |
union |
| 79 |
{ |
| 80 |
uint32_t size; /* Size of the entire type, in bytes. */ |
| 81 |
uint32_t type; /* A type_id referring to another type. */ |
| 82 |
}; |
| 83 |
}; |
| 84 |
|
| 85 |
/* The following macros access the information encoded in btf_type.info. */ |
| 86 |
/* Type kind. See below. */ |
| 87 |
#define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f) |
| 88 |
/* Number of entries of variable length data following certain type kinds. |
| 89 |
For example, number of structure members, number of function parameters. */ |
| 90 |
#define BTF_INFO_VLEN(info) ((info) & 0xffff) |
| 91 |
/* For BTF_KIND_FWD, 1 if forward to union, 0 if forward to struct. |
| 92 |
For BTF_KIND_STRUCT and BTF_KIND_UNION, 1 if the struct/union contains |
| 93 |
a bitfield. */ |
| 94 |
#define BTF_INFO_KFLAG(info) ((info) >> 31) |
| 95 |
|
| 96 |
/* Encoding for struct btf_type.info. */ |
| 97 |
#define BTF_TYPE_INFO(kind, kflag, vlen) \ |
| 98 |
((((kflag) ? 1 : 0 ) << 31) | ((kind & 0x1f) << 24) | ((vlen) & 0xffff)) |
| 99 |
|
| 100 |
#define BTF_KIND_UNKN 0 /* Unknown or invalid. */ |
| 101 |
#define BTF_KIND_INT 1 /* Integer. */ |
| 102 |
#define BTF_KIND_PTR 2 /* Pointer. */ |
| 103 |
#define BTF_KIND_ARRAY 3 /* Array. */ |
| 104 |
#define BTF_KIND_STRUCT 4 /* Struct. */ |
| 105 |
#define BTF_KIND_UNION 5 /* Union. */ |
| 106 |
#define BTF_KIND_ENUM 6 /* Enumeration. */ |
| 107 |
#define BTF_KIND_FWD 7 /* Forward. */ |
| 108 |
#define BTF_KIND_TYPEDEF 8 /* Typedef. */ |
| 109 |
#define BTF_KIND_VOLATILE 9 /* Referenced type is volatile. */ |
| 110 |
#define BTF_KIND_CONST 10 /* Referenced type is const. */ |
| 111 |
#define BTF_KIND_RESTRICT 11 /* Restrict. */ |
| 112 |
#define BTF_KIND_FUNC 12 /* Subprogram. */ |
| 113 |
#define BTF_KIND_FUNC_PROTO 13 /* Function Prototype. */ |
| 114 |
#define BTF_KIND_VAR 14 /* Variable. */ |
| 115 |
#define BTF_KIND_DATASEC 15 /* Section such as .bss or .data. */ |
| 116 |
#define BTF_KIND_FLOAT 16 /* Floating point. */ |
| 117 |
#define BTF_KIND_ENUM64 19 /* Enumeration up to 64 bits. */ |
| 118 |
#define BTF_KIND_MAX BTF_KIND_ENUM64 |
| 119 |
#define NR_BTF_KINDS (BTF_KIND_MAX + 1) |
| 120 |
|
| 121 |
/* For some BTF_KINDs, struct btf_type is immediately followed by |
| 122 |
additional data describing the type. */ |
| 123 |
|
| 124 |
/* BTF_KIND_INT is followed by a 32-bit word, with the following |
| 125 |
bit arrangement. */ |
| 126 |
#define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) |
| 127 |
#define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16) |
| 128 |
#define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff) |
| 129 |
|
| 130 |
#define BTF_INT_DATA(encoding, offset, bits) \ |
| 131 |
((((encoding) & 0x0f) << 24) | (((offset) & 0xff) << 16) | ((bits) & 0xff)) |
| 132 |
|
| 133 |
/* BTF_INT_ENCODING holds the following attribute flags. */ |
| 134 |
#define BTF_INT_SIGNED (1 << 0) |
| 135 |
#define BTF_INT_CHAR (1 << 1) |
| 136 |
#define BTF_INT_BOOL (1 << 2) |
| 137 |
|
| 138 |
/* BTF_KIND_ENUM is followed by VLEN struct btf_enum entries, |
| 139 |
which describe the enumerators. */ |
| 140 |
struct btf_enum |
| 141 |
{ |
| 142 |
uint32_t name_off; /* Offset in string section of enumerator name. */ |
| 143 |
int32_t val; /* Enumerator value. */ |
| 144 |
}; |
| 145 |
|
| 146 |
/* BTF_KF_ENUM_ holds the flags for kflags in BTF_KIND_ENUM{,64}. */ |
| 147 |
#define BTF_KF_ENUM_UNSIGNED (0) |
| 148 |
#define BTF_KF_ENUM_SIGNED (1 << 0) |
| 149 |
|
| 150 |
/* BTF_KIND_ARRAY is followed by a single struct btf_array. */ |
| 151 |
struct btf_array |
| 152 |
{ |
| 153 |
uint32_t type; /* Type of array elements. */ |
| 154 |
uint32_t index_type; /* Type of array index. */ |
| 155 |
uint32_t nelems; /* Number of elements. 0 for unsized/variable length. */ |
| 156 |
}; |
| 157 |
|
| 158 |
/* BTF_KIND_STRUCT and BTF_KIND_UNION are followed by VLEN |
| 159 |
struct btf_member. */ |
| 160 |
struct btf_member |
| 161 |
{ |
| 162 |
uint32_t name_off; /* Offset in string section of member name. */ |
| 163 |
uint32_t type; /* Type of member. */ |
| 164 |
uint32_t offset; /* If the type info kind_flag is set, this contains |
| 165 |
both the member bitfield size and bit offset, |
| 166 |
according to the macros below. If kind_flag is not |
| 167 |
set, offset contains only the bit offset (from the |
| 168 |
beginning of the struct). */ |
| 169 |
}; |
| 170 |
|
| 171 |
/* If struct or union type info kind_flag is set, used to access member |
| 172 |
bitfield size from btf_member.offset. */ |
| 173 |
#define BTF_MEMBER_BITFIELD_SIZE (val) ((val) >> 24) |
| 174 |
/* If struct or union type info kind_flag is set, used to access member |
| 175 |
bit offset from btf_member.offset. */ |
| 176 |
#define BTF_MEMBER_BIT_OFFSET (val) ((val) & 0x00ffffff) |
| 177 |
|
| 178 |
/* BTF_KIND_FUNC_PROTO is followed by VLEN struct btf_param entries, which |
| 179 |
describe the types of the function parameters. */ |
| 180 |
struct btf_param |
| 181 |
{ |
| 182 |
uint32_t name_off; /* Offset in string section of parameter name. */ |
| 183 |
uint32_t type; /* Type of parameter. */ |
| 184 |
}; |
| 185 |
|
| 186 |
/* BTF_KIND_FUNC records encode linkage information in the VLEN bits |
| 187 |
of the type record. These are the supported values. */ |
| 188 |
enum btf_func_linkage |
| 189 |
{ |
| 190 |
BTF_FUNC_STATIC = 0, |
| 191 |
BTF_FUNC_GLOBAL = 1, |
| 192 |
BTF_FUNC_EXTERN = 2, |
| 193 |
}; |
| 194 |
|
| 195 |
/* BTF_KIND_VAR records encode linkage information in a single |
| 196 |
trailing struct btf_var. These are the supported values. */ |
| 197 |
enum btf_var_linkage |
| 198 |
{ |
| 199 |
BTF_VAR_STATIC = 0, |
| 200 |
BTF_VAR_GLOBAL_ALLOCATED = 1, |
| 201 |
BTF_VAR_GLOBAL_EXTERN = 2, |
| 202 |
}; |
| 203 |
|
| 204 |
/* BTF_KIND_VAR is followed by a single struct btf_var, which describes |
| 205 |
information about the variable. */ |
| 206 |
struct btf_var |
| 207 |
{ |
| 208 |
uint32_t linkage; /* 0=static, 1=global, 2=extern. */ |
| 209 |
}; |
| 210 |
|
| 211 |
/* BTF_KIND_DATASEC is followed by VLEN struct btf_var_secinfo entries, |
| 212 |
which describe all BTF_KIND_VAR or extern BTF_KIND_FUNC types contained |
| 213 |
in the section. */ |
| 214 |
struct btf_var_secinfo |
| 215 |
{ |
| 216 |
uint32_t type; /* Type of BTF_KIND_VAR or BTF_KIND_FUNC item. */ |
| 217 |
uint32_t offset; /* In-section offset (in bytes) of item. */ |
| 218 |
uint32_t size; /* Size (in bytes) of item. */ |
| 219 |
}; |
| 220 |
|
| 221 |
/* BTF_KIND_ENUM64 is followed by VLEN struct btf_enum64 entries, |
| 222 |
which describe the 64 bits enumerators. */ |
| 223 |
struct btf_enum64 |
| 224 |
{ |
| 225 |
uint32_t name_off; /* Offset in string section of enumerator name. */ |
| 226 |
uint32_t val_lo32; /* lower 32-bit value for a 64-bit value Enumerator */ |
| 227 |
uint32_t val_hi32; /* high 32-bit value for a 64-bit value Enumerator */ |
| 228 |
}; |
| 229 |
|
| 230 |
#ifdef __cplusplus |
| 231 |
} |
| 232 |
#endif |
| 233 |
|
| 234 |
#endif /* _BTF_H_ */ |