xref: /linux/tools/testing/selftests/bpf/test_btf.h (revision 058443934524590d5537a80f490267cc95a61c05)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 Facebook */
3 
4 #ifndef _TEST_BTF_H
5 #define _TEST_BTF_H
6 
7 #define BTF_END_RAW 0xdeadbeef
8 
9 #define BTF_INFO_ENC(kind, kind_flag, vlen)			\
10 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
11 
12 #define BTF_TYPE_ENC(name, info, size_or_type)	\
13 	(name), (info), (size_or_type)
14 
15 #define BTF_INT_ENC(encoding, bits_offset, nr_bits)	\
16 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
17 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz)	\
18 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz),	\
19 	BTF_INT_ENC(encoding, bits_offset, bits)
20 
21 #define BTF_FWD_ENC(name, kind_flag) \
22 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FWD, kind_flag, 0), 0)
23 
24 #define BTF_ARRAY_ENC(type, index_type, nr_elems)	\
25 	(type), (index_type), (nr_elems)
26 #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
27 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
28 	BTF_ARRAY_ENC(type, index_type, nr_elems)
29 
30 #define BTF_STRUCT_ENC(name, nr_elems, sz)	\
31 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, nr_elems), sz)
32 
33 #define BTF_UNION_ENC(name, nr_elems, sz)	\
34 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_UNION, 0, nr_elems), sz)
35 
36 #define BTF_VAR_ENC(name, type, linkage)	\
37 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), type), (linkage)
38 #define BTF_VAR_SECINFO_ENC(type, offset, size)	\
39 	(type), (offset), (size)
40 
41 #define BTF_MEMBER_ENC(name, type, bits_offset)	\
42 	(name), (type), (bits_offset)
43 #define BTF_ENUM_ENC(name, val) (name), (val)
44 #define BTF_ENUM64_ENC(name, val_lo32, val_hi32) (name), (val_lo32), (val_hi32)
45 #define BTF_MEMBER_OFFSET(bitfield_size, bits_offset) \
46 	((bitfield_size) << 24 | (bits_offset))
47 
48 #define BTF_TYPEDEF_ENC(name, type) \
49 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
50 
51 #define BTF_PTR_ENC(type) \
52 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
53 
54 #define BTF_CONST_ENC(type) \
55 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), type)
56 
57 #define BTF_VOLATILE_ENC(type) \
58 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), type)
59 
60 #define BTF_RESTRICT_ENC(type) \
61 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), type)
62 
63 #define BTF_FUNC_PROTO_ENC(ret_type, nargs) \
64 	BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, nargs), ret_type)
65 
66 #define BTF_FUNC_PROTO_ARG_ENC(name, type) \
67 	(name), (type)
68 
69 #define BTF_FUNC_ENC(name, func_proto) \
70 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
71 
72 #define BTF_TYPE_FLOAT_ENC(name, sz) \
73 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
74 
75 #define BTF_DECL_TAG_ENC(value, type, component_idx)	\
76 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
77 
78 #define BTF_TYPE_TAG_ENC(value, type)	\
79 	BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
80 
81 #endif /* _TEST_BTF_H */
82