xref: /openbmc/linux/tools/lib/bpf/btf.h (revision 911b8eac)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2018 Facebook */
3 
4 #ifndef __LIBBPF_BTF_H
5 #define __LIBBPF_BTF_H
6 
7 #include <stdarg.h>
8 #include <linux/btf.h>
9 #include <linux/types.h>
10 
11 #include "libbpf_common.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define BTF_ELF_SEC ".BTF"
18 #define BTF_EXT_ELF_SEC ".BTF.ext"
19 #define MAPS_ELF_SEC ".maps"
20 
21 struct btf;
22 struct btf_ext;
23 struct btf_type;
24 
25 struct bpf_object;
26 
27 LIBBPF_API void btf__free(struct btf *btf);
28 LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
29 LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
30 LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
31 LIBBPF_API struct btf *btf__parse_raw(const char *path);
32 LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
33 LIBBPF_API int btf__load(struct btf *btf);
34 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
35 				   const char *type_name);
36 LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
37 					const char *type_name, __u32 kind);
38 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
39 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
40 						  __u32 id);
41 LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
42 LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
43 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
44 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
45 LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
46 LIBBPF_API int btf__fd(const struct btf *btf);
47 LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
48 LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
49 LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
50 LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
51 LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
52 				    __u32 expected_key_size,
53 				    __u32 expected_value_size,
54 				    __u32 *key_type_id, __u32 *value_type_id);
55 
56 LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
57 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
58 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
59 					     __u32 *size);
60 LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_func_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
61 int btf_ext__reloc_func_info(const struct btf *btf,
62 			     const struct btf_ext *btf_ext,
63 			     const char *sec_name, __u32 insns_cnt,
64 			     void **func_info, __u32 *cnt);
65 LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_line_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
66 int btf_ext__reloc_line_info(const struct btf *btf,
67 			     const struct btf_ext *btf_ext,
68 			     const char *sec_name, __u32 insns_cnt,
69 			     void **line_info, __u32 *cnt);
70 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
71 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
72 
73 LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
74 
75 struct btf_dedup_opts {
76 	unsigned int dedup_table_size;
77 	bool dont_resolve_fwds;
78 };
79 
80 LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
81 			  const struct btf_dedup_opts *opts);
82 
83 struct btf_dump;
84 
85 struct btf_dump_opts {
86 	void *ctx;
87 };
88 
89 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
90 
91 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
92 					  const struct btf_ext *btf_ext,
93 					  const struct btf_dump_opts *opts,
94 					  btf_dump_printf_fn_t printf_fn);
95 LIBBPF_API void btf_dump__free(struct btf_dump *d);
96 
97 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
98 
99 struct btf_dump_emit_type_decl_opts {
100 	/* size of this struct, for forward/backward compatiblity */
101 	size_t sz;
102 	/* optional field name for type declaration, e.g.:
103 	 * - struct my_struct <FNAME>
104 	 * - void (*<FNAME>)(int)
105 	 * - char (*<FNAME>)[123]
106 	 */
107 	const char *field_name;
108 	/* extra indentation level (in number of tabs) to emit for multi-line
109 	 * type declarations (e.g., anonymous struct); applies for lines
110 	 * starting from the second one (first line is assumed to have
111 	 * necessary indentation already
112 	 */
113 	int indent_level;
114 	/* strip all the const/volatile/restrict mods */
115 	bool strip_mods;
116 };
117 #define btf_dump_emit_type_decl_opts__last_field strip_mods
118 
119 LIBBPF_API int
120 btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
121 			 const struct btf_dump_emit_type_decl_opts *opts);
122 
123 /*
124  * A set of helpers for easier BTF types handling
125  */
126 static inline __u16 btf_kind(const struct btf_type *t)
127 {
128 	return BTF_INFO_KIND(t->info);
129 }
130 
131 static inline __u16 btf_vlen(const struct btf_type *t)
132 {
133 	return BTF_INFO_VLEN(t->info);
134 }
135 
136 static inline bool btf_kflag(const struct btf_type *t)
137 {
138 	return BTF_INFO_KFLAG(t->info);
139 }
140 
141 static inline bool btf_is_void(const struct btf_type *t)
142 {
143 	return btf_kind(t) == BTF_KIND_UNKN;
144 }
145 
146 static inline bool btf_is_int(const struct btf_type *t)
147 {
148 	return btf_kind(t) == BTF_KIND_INT;
149 }
150 
151 static inline bool btf_is_ptr(const struct btf_type *t)
152 {
153 	return btf_kind(t) == BTF_KIND_PTR;
154 }
155 
156 static inline bool btf_is_array(const struct btf_type *t)
157 {
158 	return btf_kind(t) == BTF_KIND_ARRAY;
159 }
160 
161 static inline bool btf_is_struct(const struct btf_type *t)
162 {
163 	return btf_kind(t) == BTF_KIND_STRUCT;
164 }
165 
166 static inline bool btf_is_union(const struct btf_type *t)
167 {
168 	return btf_kind(t) == BTF_KIND_UNION;
169 }
170 
171 static inline bool btf_is_composite(const struct btf_type *t)
172 {
173 	__u16 kind = btf_kind(t);
174 
175 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
176 }
177 
178 static inline bool btf_is_enum(const struct btf_type *t)
179 {
180 	return btf_kind(t) == BTF_KIND_ENUM;
181 }
182 
183 static inline bool btf_is_fwd(const struct btf_type *t)
184 {
185 	return btf_kind(t) == BTF_KIND_FWD;
186 }
187 
188 static inline bool btf_is_typedef(const struct btf_type *t)
189 {
190 	return btf_kind(t) == BTF_KIND_TYPEDEF;
191 }
192 
193 static inline bool btf_is_volatile(const struct btf_type *t)
194 {
195 	return btf_kind(t) == BTF_KIND_VOLATILE;
196 }
197 
198 static inline bool btf_is_const(const struct btf_type *t)
199 {
200 	return btf_kind(t) == BTF_KIND_CONST;
201 }
202 
203 static inline bool btf_is_restrict(const struct btf_type *t)
204 {
205 	return btf_kind(t) == BTF_KIND_RESTRICT;
206 }
207 
208 static inline bool btf_is_mod(const struct btf_type *t)
209 {
210 	__u16 kind = btf_kind(t);
211 
212 	return kind == BTF_KIND_VOLATILE ||
213 	       kind == BTF_KIND_CONST ||
214 	       kind == BTF_KIND_RESTRICT;
215 }
216 
217 static inline bool btf_is_func(const struct btf_type *t)
218 {
219 	return btf_kind(t) == BTF_KIND_FUNC;
220 }
221 
222 static inline bool btf_is_func_proto(const struct btf_type *t)
223 {
224 	return btf_kind(t) == BTF_KIND_FUNC_PROTO;
225 }
226 
227 static inline bool btf_is_var(const struct btf_type *t)
228 {
229 	return btf_kind(t) == BTF_KIND_VAR;
230 }
231 
232 static inline bool btf_is_datasec(const struct btf_type *t)
233 {
234 	return btf_kind(t) == BTF_KIND_DATASEC;
235 }
236 
237 static inline __u8 btf_int_encoding(const struct btf_type *t)
238 {
239 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
240 }
241 
242 static inline __u8 btf_int_offset(const struct btf_type *t)
243 {
244 	return BTF_INT_OFFSET(*(__u32 *)(t + 1));
245 }
246 
247 static inline __u8 btf_int_bits(const struct btf_type *t)
248 {
249 	return BTF_INT_BITS(*(__u32 *)(t + 1));
250 }
251 
252 static inline struct btf_array *btf_array(const struct btf_type *t)
253 {
254 	return (struct btf_array *)(t + 1);
255 }
256 
257 static inline struct btf_enum *btf_enum(const struct btf_type *t)
258 {
259 	return (struct btf_enum *)(t + 1);
260 }
261 
262 static inline struct btf_member *btf_members(const struct btf_type *t)
263 {
264 	return (struct btf_member *)(t + 1);
265 }
266 
267 /* Get bit offset of a member with specified index. */
268 static inline __u32 btf_member_bit_offset(const struct btf_type *t,
269 					  __u32 member_idx)
270 {
271 	const struct btf_member *m = btf_members(t) + member_idx;
272 	bool kflag = btf_kflag(t);
273 
274 	return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
275 }
276 /*
277  * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
278  * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
279  */
280 static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
281 					     __u32 member_idx)
282 {
283 	const struct btf_member *m = btf_members(t) + member_idx;
284 	bool kflag = btf_kflag(t);
285 
286 	return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
287 }
288 
289 static inline struct btf_param *btf_params(const struct btf_type *t)
290 {
291 	return (struct btf_param *)(t + 1);
292 }
293 
294 static inline struct btf_var *btf_var(const struct btf_type *t)
295 {
296 	return (struct btf_var *)(t + 1);
297 }
298 
299 static inline struct btf_var_secinfo *
300 btf_var_secinfos(const struct btf_type *t)
301 {
302 	return (struct btf_var_secinfo *)(t + 1);
303 }
304 
305 #ifdef __cplusplus
306 } /* extern "C" */
307 #endif
308 
309 #endif /* __LIBBPF_BTF_H */
310