| 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ |
| 2 | /* Copyright (c) 2018 Facebook */ |
| 3 | #ifndef __LINUX_BTF_H__ |
| 4 | #define __LINUX_BTF_H__ |
| 5 | |
| 6 | #include <linux/types.h> |
| 7 | |
| 8 | #define BTF_MAGIC	0xeB9F |
| 9 | #define BTF_VERSION	1 |
| 10 | |
| 11 | /* |
| 12 | * BTF layout section consists of a struct btf_layout for each known |
| 13 | * kind at BTF encoding time. |
| 14 | */ |
| 15 | struct btf_layout { |
| 16 | 	__u8 info_sz;		/* size of singular element after btf_type */ |
| 17 | 	__u8 elem_sz;		/* size of each of btf_vlen(t) elements */ |
| 18 | 	__u16 flags;		/* currently unused */ |
| 19 | }; |
| 20 | |
| 21 | struct btf_header { |
| 22 | 	__u16	magic; |
| 23 | 	__u8	version; |
| 24 | 	__u8	flags; |
| 25 | 	__u32	hdr_len; |
| 26 | |
| 27 | 	/* All offsets are in bytes relative to the end of this header */ |
| 28 | 	__u32	type_off;	/* offset of type section	*/ |
| 29 | 	__u32	type_len;	/* length of type section	*/ |
| 30 | 	__u32	str_off;	/* offset of string section	*/ |
| 31 | 	__u32	str_len;	/* length of string section	*/ |
| 32 | 	__u32	layout_off;	/* offset of layout section	*/ |
| 33 | 	__u32	layout_len;	/* length of layout section	*/ |
| 34 | }; |
| 35 | |
| 36 | enum btf_max { |
| 37 | 	/* Max possible kind */ |
| 38 | 	BTF_MAX_KIND =		0x0000007f, |
| 39 | 	/* Max # of type identifier */ |
| 40 | 	BTF_MAX_TYPE =		0x000fffff, |
| 41 | 	/* Max offset into the string section */ |
| 42 | 	BTF_MAX_NAME_OFFSET =	0x00ffffff, |
| 43 | 	/* Max # of struct/union/enum members or func args */ |
| 44 | 	BTF_MAX_VLEN =		0x00ffffff, |
| 45 | }; |
| 46 | |
| 47 | struct btf_type { |
| 48 | 	__u32 name_off; |
| 49 | 	/* "info" bits arrangement |
| 50 | 	 * bits 0-23: vlen (e.g. # of struct's members) |
| 51 | 	 * bits 24-30: kind (e.g. int, ptr, array...etc) |
| 52 | 	 * bit 31: kind_flag, currently used by |
| 53 | 	 * struct, union, enum, fwd, enum64, |
| 54 | 	 * decl_tag and type_tag |
| 55 | 	 */ |
| 56 | 	__u32 info; |
| 57 | 	/* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64. |
| 58 | 	 * "size" tells the size of the type it is describing. |
| 59 | 	 * |
| 60 | 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, |
| 61 | 	 * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG. |
| 62 | 	 * "type" is a type_id referring to another type. |
| 63 | 	 */ |
| 64 | 	union { |
| 65 | 		__u32 size; |
| 66 | 		__u32 type; |
| 67 | 	}; |
| 68 | }; |
| 69 | |
| 70 | #define BTF_INFO_KIND(info)	(((info) >> 24) & 0x7f) |
| 71 | #define BTF_INFO_VLEN(info)	((info) & 0xffffff) |
| 72 | #define BTF_INFO_KFLAG(info)	((info) >> 31) |
| 73 | |
| 74 | enum { |
| 75 | 	BTF_KIND_UNKN		= 0,	/* Unknown	*/ |
| 76 | 	BTF_KIND_INT		= 1,	/* Integer	*/ |
| 77 | 	BTF_KIND_PTR		= 2,	/* Pointer	*/ |
| 78 | 	BTF_KIND_ARRAY		= 3,	/* Array	*/ |
| 79 | 	BTF_KIND_STRUCT		= 4,	/* Struct	*/ |
| 80 | 	BTF_KIND_UNION		= 5,	/* Union	*/ |
| 81 | 	BTF_KIND_ENUM		= 6,	/* Enumeration up to 32-bit values */ |
| 82 | 	BTF_KIND_FWD		= 7,	/* Forward	*/ |
| 83 | 	BTF_KIND_TYPEDEF	= 8,	/* Typedef	*/ |
| 84 | 	BTF_KIND_VOLATILE	= 9,	/* Volatile	*/ |
| 85 | 	BTF_KIND_CONST		= 10,	/* Const	*/ |
| 86 | 	BTF_KIND_RESTRICT	= 11,	/* Restrict	*/ |
| 87 | 	BTF_KIND_FUNC		= 12,	/* Function	*/ |
| 88 | 	BTF_KIND_FUNC_PROTO	= 13,	/* Function Proto	*/ |
| 89 | 	BTF_KIND_VAR		= 14,	/* Variable	*/ |
| 90 | 	BTF_KIND_DATASEC	= 15,	/* Section	*/ |
| 91 | 	BTF_KIND_FLOAT		= 16,	/* Floating point	*/ |
| 92 | 	BTF_KIND_DECL_TAG	= 17,	/* Decl Tag */ |
| 93 | 	BTF_KIND_TYPE_TAG	= 18,	/* Type Tag */ |
| 94 | 	BTF_KIND_ENUM64		= 19,	/* Enumeration up to 64-bit values */ |
| 95 | |
| 96 | 	NR_BTF_KINDS, |
| 97 | 	BTF_KIND_MAX		= NR_BTF_KINDS - 1, |
| 98 | }; |
| 99 | |
| 100 | /* For some specific BTF_KIND, "struct btf_type" is immediately |
| 101 | * followed by extra data. |
| 102 | */ |
| 103 | |
| 104 | /* BTF_KIND_INT is followed by a u32 and the following |
| 105 | * is the 32 bits arrangement: |
| 106 | */ |
| 107 | #define BTF_INT_ENCODING(VAL)	(((VAL) & 0x0f000000) >> 24) |
| 108 | #define BTF_INT_OFFSET(VAL)	(((VAL) & 0x00ff0000) >> 16) |
| 109 | #define BTF_INT_BITS(VAL)	((VAL) & 0x000000ff) |
| 110 | |
| 111 | /* Attributes stored in the BTF_INT_ENCODING */ |
| 112 | #define BTF_INT_SIGNED	(1 << 0) |
| 113 | #define BTF_INT_CHAR	(1 << 1) |
| 114 | #define BTF_INT_BOOL	(1 << 2) |
| 115 | |
| 116 | /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". |
| 117 | * The exact number of btf_enum is stored in the vlen (of the |
| 118 | * info in "struct btf_type"). |
| 119 | */ |
| 120 | struct btf_enum { |
| 121 | 	__u32	name_off; |
| 122 | 	__s32	val; |
| 123 | }; |
| 124 | |
| 125 | /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ |
| 126 | struct btf_array { |
| 127 | 	__u32	type; |
| 128 | 	__u32	index_type; |
| 129 | 	__u32	nelems; |
| 130 | }; |
| 131 | |
| 132 | /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed |
| 133 | * by multiple "struct btf_member". The exact number |
| 134 | * of btf_member is stored in the vlen (of the info in |
| 135 | * "struct btf_type"). |
| 136 | */ |
| 137 | struct btf_member { |
| 138 | 	__u32	name_off; |
| 139 | 	__u32	type; |
| 140 | 	/* If the type info kind_flag is set, the btf_member offset |
| 141 | 	 * contains both member bitfield size and bit offset. The |
| 142 | 	 * bitfield size is set for bitfield members. If the type |
| 143 | 	 * info kind_flag is not set, the offset contains only bit |
| 144 | 	 * offset. |
| 145 | 	 */ |
| 146 | 	__u32	offset; |
| 147 | }; |
| 148 | |
| 149 | /* If the struct/union type info kind_flag is set, the |
| 150 | * following two macros are used to access bitfield_size |
| 151 | * and bit_offset from btf_member.offset. |
| 152 | */ |
| 153 | #define BTF_MEMBER_BITFIELD_SIZE(val)	((val) >> 24) |
| 154 | #define BTF_MEMBER_BIT_OFFSET(val)	((val) & 0xffffff) |
| 155 | |
| 156 | /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param". |
| 157 | * The exact number of btf_param is stored in the vlen (of the |
| 158 | * info in "struct btf_type"). |
| 159 | */ |
| 160 | struct btf_param { |
| 161 | 	__u32	name_off; |
| 162 | 	__u32	type; |
| 163 | }; |
| 164 | |
| 165 | enum { |
| 166 | 	BTF_VAR_STATIC = 0, |
| 167 | 	BTF_VAR_GLOBAL_ALLOCATED = 1, |
| 168 | 	BTF_VAR_GLOBAL_EXTERN = 2, |
| 169 | }; |
| 170 | |
| 171 | enum btf_func_linkage { |
| 172 | 	BTF_FUNC_STATIC = 0, |
| 173 | 	BTF_FUNC_GLOBAL = 1, |
| 174 | 	BTF_FUNC_EXTERN = 2, |
| 175 | }; |
| 176 | |
| 177 | /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe |
| 178 | * additional information related to the variable such as its linkage. |
| 179 | */ |
| 180 | struct btf_var { |
| 181 | 	__u32	linkage; |
| 182 | }; |
| 183 | |
| 184 | /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo" |
| 185 | * to describe all BTF_KIND_VAR types it contains along with it's |
| 186 | * in-section offset as well as size. |
| 187 | */ |
| 188 | struct btf_var_secinfo { |
| 189 | 	__u32	type; |
| 190 | 	__u32	offset; |
| 191 | 	__u32	size; |
| 192 | }; |
| 193 | |
| 194 | /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe |
| 195 | * additional information related to the tag applied location. |
| 196 | * If component_idx == -1, the tag is applied to a struct, union, |
| 197 | * variable or function. Otherwise, it is applied to a struct/union |
| 198 | * member or a func argument, and component_idx indicates which member |
| 199 | * or argument (0 ... vlen-1). |
| 200 | */ |
| 201 | struct btf_decl_tag { |
| 202 | __s32 component_idx; |
| 203 | }; |
| 204 | |
| 205 | /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64". |
| 206 | * The exact number of btf_enum64 is stored in the vlen (of the |
| 207 | * info in "struct btf_type"). |
| 208 | */ |
| 209 | struct btf_enum64 { |
| 210 | 	__u32	name_off; |
| 211 | 	__u32	val_lo32; |
| 212 | 	__u32	val_hi32; |
| 213 | }; |
| 214 | |
| 215 | #endif /* __LINUX_BTF_H__ */ |