xref: /linux/tools/include/linux/btf_ids.h (revision 8ea636848aca35b9f97c5b5dee30225cf2dd0fe6)
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, scope)			\
61 asm(							\
62 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
63 "." #scope " " #name ";                        \n"	\
64 #name ":;                                      \n"	\
65 ".popsection;                                  \n");	\
66 
67 #define BTF_ID_LIST(name)				\
68 __BTF_ID_LIST(name, local)				\
69 extern u32 name[];
70 
71 #define BTF_ID_LIST_GLOBAL(name)			\
72 __BTF_ID_LIST(name, globl)
73 
74 /*
75  * The BTF_ID_UNUSED macro defines 4 zero bytes.
76  * It's used when we want to define 'unused' entry
77  * in BTF_ID_LIST, like:
78  *
79  *   BTF_ID_LIST(bpf_skb_output_btf_ids)
80  *   BTF_ID(struct, sk_buff)
81  *   BTF_ID_UNUSED
82  *   BTF_ID(struct, task_struct)
83  */
84 
85 #define BTF_ID_UNUSED					\
86 asm(							\
87 ".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
88 ".zero 4                                       \n"	\
89 ".popsection;                                  \n");
90 
91 #else
92 
93 #define BTF_ID_LIST(name) static u32 name[5];
94 #define BTF_ID(prefix, name)
95 #define BTF_ID_UNUSED
96 #define BTF_ID_LIST_GLOBAL(name) u32 name[1];
97 
98 #endif /* CONFIG_DEBUG_INFO_BTF */
99 
100 #ifdef CONFIG_NET
101 /* Define a list of socket types which can be the argument for
102  * skc_to_*_sock() helpers. All these sockets should have
103  * sock_common as the first argument in its memory layout.
104  */
105 #define BTF_SOCK_TYPE_xxx \
106 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET, inet_sock)			\
107 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_CONN, inet_connection_sock)	\
108 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_REQ, inet_request_sock)	\
109 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_INET_TW, inet_timewait_sock)	\
110 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_REQ, request_sock)			\
111 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK, sock)				\
112 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_SOCK_COMMON, sock_common)		\
113 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP, tcp_sock)			\
114 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_REQ, tcp_request_sock)		\
115 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP_TW, tcp_timewait_sock)		\
116 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_TCP6, tcp6_sock)			\
117 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP, udp_sock)			\
118 	BTF_SOCK_TYPE(BTF_SOCK_TYPE_UDP6, udp6_sock)
119 
120 enum {
121 #define BTF_SOCK_TYPE(name, str) name,
122 BTF_SOCK_TYPE_xxx
123 #undef BTF_SOCK_TYPE
124 MAX_BTF_SOCK_TYPE,
125 };
126 
127 extern u32 btf_sock_ids[];
128 #endif
129 
130 #endif
131