xref: /linux-6.15/include/uapi/linux/btf.h (revision cd65cd95)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /* Copyright (c) 2018 Facebook */
3 #ifndef _UAPI__LINUX_BTF_H__
4 #define _UAPI__LINUX_BTF_H__
5 
6 #include <linux/types.h>
7 
8 #define BTF_MAGIC	0xeB9F
9 #define BTF_VERSION	1
10 
11 struct btf_header {
12 	__u16	magic;
13 	__u8	version;
14 	__u8	flags;
15 
16 	__u32	parent_label;
17 	__u32	parent_name;
18 
19 	/* All offsets are in bytes relative to the end of this header */
20 	__u32	label_off;	/* offset of label section	*/
21 	__u32	object_off;	/* offset of data object section*/
22 	__u32	func_off;	/* offset of function section	*/
23 	__u32	type_off;	/* offset of type section	*/
24 	__u32	str_off;	/* offset of string section	*/
25 	__u32	str_len;	/* length of string section	*/
26 };
27 
28 /* Max # of type identifier */
29 #define BTF_MAX_TYPE	0x7fffffff
30 /* Max offset into the string section */
31 #define BTF_MAX_NAME_OFFSET	0x7fffffff
32 /* Max # of struct/union/enum members or func args */
33 #define BTF_MAX_VLEN	0xffff
34 
35 /* The type id is referring to a parent BTF */
36 #define BTF_TYPE_PARENT(id)	(((id) >> 31) & 0x1)
37 #define BTF_TYPE_ID(id)		((id) & BTF_MAX_TYPE)
38 
39 /* String is in the ELF string section */
40 #define BTF_STR_TBL_ELF_ID(ref)	(((ref) >> 31) & 0x1)
41 #define BTF_STR_OFFSET(ref)	((ref) & BTF_MAX_NAME_OFFSET)
42 
43 struct btf_type {
44 	__u32 name_off;
45 	/* "info" bits arrangement
46 	 * bits  0-15: vlen (e.g. # of struct's members)
47 	 * bits 16-23: unused
48 	 * bits 24-28: kind (e.g. int, ptr, array...etc)
49 	 * bits 29-30: unused
50 	 * bits    31: root
51 	 */
52 	__u32 info;
53 	/* "size" is used by INT, ENUM, STRUCT and UNION.
54 	 * "size" tells the size of the type it is describing.
55 	 *
56 	 * "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT.
57 	 * "type" is a type_id referring to another type.
58 	 */
59 	union {
60 		__u32 size;
61 		__u32 type;
62 	};
63 };
64 
65 #define BTF_INFO_KIND(info)	(((info) >> 24) & 0x1f)
66 #define BTF_INFO_ISROOT(info)	(!!(((info) >> 24) & 0x80))
67 #define BTF_INFO_VLEN(info)	((info) & 0xffff)
68 
69 #define BTF_KIND_UNKN		0	/* Unknown	*/
70 #define BTF_KIND_INT		1	/* Integer	*/
71 #define BTF_KIND_PTR		2	/* Pointer	*/
72 #define BTF_KIND_ARRAY		3	/* Array	*/
73 #define BTF_KIND_STRUCT		4	/* Struct	*/
74 #define BTF_KIND_UNION		5	/* Union	*/
75 #define BTF_KIND_ENUM		6	/* Enumeration	*/
76 #define BTF_KIND_FWD		7	/* Forward	*/
77 #define BTF_KIND_TYPEDEF	8	/* Typedef	*/
78 #define BTF_KIND_VOLATILE	9	/* Volatile	*/
79 #define BTF_KIND_CONST		10	/* Const	*/
80 #define BTF_KIND_RESTRICT	11	/* Restrict	*/
81 #define BTF_KIND_MAX		11
82 #define NR_BTF_KINDS		12
83 
84 /* For some specific BTF_KIND, "struct btf_type" is immediately
85  * followed by extra data.
86  */
87 
88 /* BTF_KIND_INT is followed by a u32 and the following
89  * is the 32 bits arrangement:
90  */
91 #define BTF_INT_ENCODING(VAL)	(((VAL) & 0xff000000) >> 24)
92 #define BTF_INT_OFFSET(VAL)	(((VAL  & 0x00ff0000)) >> 16)
93 #define BTF_INT_BITS(VAL)	((VAL)  & 0x0000ffff)
94 
95 /* Attributes stored in the BTF_INT_ENCODING */
96 #define BTF_INT_SIGNED	0x1
97 #define BTF_INT_CHAR	0x2
98 #define BTF_INT_BOOL	0x4
99 #define BTF_INT_VARARGS	0x8
100 
101 /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
102  * The exact number of btf_enum is stored in the vlen (of the
103  * info in "struct btf_type").
104  */
105 struct btf_enum {
106 	__u32	name_off;
107 	__s32	val;
108 };
109 
110 /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
111 struct btf_array {
112 	__u32	type;
113 	__u32	index_type;
114 	__u32	nelems;
115 };
116 
117 /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
118  * by multiple "struct btf_member".  The exact number
119  * of btf_member is stored in the vlen (of the info in
120  * "struct btf_type").
121  */
122 struct btf_member {
123 	__u32	name_off;
124 	__u32	type;
125 	__u32	offset;	/* offset in bits */
126 };
127 
128 #endif /* _UAPI__LINUX_BTF_H__ */
129