xref: /linux-6.15/include/linux/btf_ids.h (revision c201324b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _LINUX_BTF_IDS_H
4 #define _LINUX_BTF_IDS_H
5 
6 #ifdef CONFIG_DEBUG_INFO_BTF
7 
8 #include <linux/compiler.h> /* for __PASTE */
9 
10 /*
11  * Following macros help to define lists of BTF IDs placed
12  * in .BTF_ids section. They are initially filled with zeros
13  * (during compilation) and resolved later during the
14  * linking phase by resolve_btfids tool.
15  *
16  * Any change in list layout must be reflected in resolve_btfids
17  * tool logic.
18  */
19 
20 #define BTF_IDS_SECTION ".BTF_ids"
21 
22 #define ____BTF_ID(symbol)				\
23 asm(							\
24 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
25 ".local " #symbol " ;                          \n"	\
26 ".type  " #symbol ", STT_OBJECT;               \n"	\
27 ".size  " #symbol ", 4;                        \n"	\
28 #symbol ":                                     \n"	\
29 ".zero 4                                       \n"	\
30 ".popsection;                                  \n");
31 
32 #define __BTF_ID(symbol) \
33 	____BTF_ID(symbol)
34 
35 #define __ID(prefix) \
36 	__PASTE(prefix, __COUNTER__)
37 
38 /*
39  * The BTF_ID defines unique symbol for each ID pointing
40  * to 4 zero bytes.
41  */
42 #define BTF_ID(prefix, name) \
43 	__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__))
44 
45 /*
46  * The BTF_ID_LIST macro defines pure (unsorted) list
47  * of BTF IDs, with following layout:
48  *
49  * BTF_ID_LIST(list1)
50  * BTF_ID(type1, name1)
51  * BTF_ID(type2, name2)
52  *
53  * list1:
54  * __BTF_ID__type1__name1__1:
55  * .zero 4
56  * __BTF_ID__type2__name2__2:
57  * .zero 4
58  *
59  */
60 #define __BTF_ID_LIST(name)				\
61 asm(							\
62 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
63 ".local " #name ";                             \n"	\
64 #name ":;                                      \n"	\
65 ".popsection;                                  \n");	\
66 
67 #define BTF_ID_LIST(name)				\
68 __BTF_ID_LIST(name)					\
69 extern u32 name[];
70 
71 /*
72  * The BTF_ID_UNUSED macro defines 4 zero bytes.
73  * It's used when we want to define 'unused' entry
74  * in BTF_ID_LIST, like:
75  *
76  *   BTF_ID_LIST(bpf_skb_output_btf_ids)
77  *   BTF_ID(struct, sk_buff)
78  *   BTF_ID_UNUSED
79  *   BTF_ID(struct, task_struct)
80  */
81 
82 #define BTF_ID_UNUSED					\
83 asm(							\
84 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
85 ".zero 4                                       \n"	\
86 ".popsection;                                  \n");
87 
88 #else
89 
90 #define BTF_ID_LIST(name) static u32 name[5];
91 #define BTF_ID(prefix, name)
92 #define BTF_ID_UNUSED
93 
94 #endif /* CONFIG_DEBUG_INFO_BTF */
95 
96 #endif
97