xref: /openbmc/linux/tools/lib/bpf/btf.h (revision 505b0877)
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 <stdbool.h>
9 #include <linux/btf.h>
10 #include <linux/types.h>
11 
12 #include "libbpf_common.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define BTF_ELF_SEC ".BTF"
19 #define BTF_EXT_ELF_SEC ".BTF.ext"
20 #define MAPS_ELF_SEC ".maps"
21 
22 struct btf;
23 struct btf_ext;
24 struct btf_type;
25 
26 struct bpf_object;
27 
28 enum btf_endianness {
29 	BTF_LITTLE_ENDIAN = 0,
30 	BTF_BIG_ENDIAN = 1,
31 };
32 
33 LIBBPF_API void btf__free(struct btf *btf);
34 LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
35 LIBBPF_API struct btf *btf__new_empty(void);
36 LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
37 LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
38 LIBBPF_API struct btf *btf__parse_raw(const char *path);
39 LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
40 LIBBPF_API int btf__load(struct btf *btf);
41 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
42 				   const char *type_name);
43 LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
44 					const char *type_name, __u32 kind);
45 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
46 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
47 						  __u32 id);
48 LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
49 LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
50 LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
51 LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
52 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
53 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
54 LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
55 LIBBPF_API int btf__fd(const struct btf *btf);
56 LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
57 LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
58 LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
59 LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
60 LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
61 LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
62 				    __u32 expected_key_size,
63 				    __u32 expected_value_size,
64 				    __u32 *key_type_id, __u32 *value_type_id);
65 
66 LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
67 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
68 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
69 					     __u32 *size);
70 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")
71 int btf_ext__reloc_func_info(const struct btf *btf,
72 			     const struct btf_ext *btf_ext,
73 			     const char *sec_name, __u32 insns_cnt,
74 			     void **func_info, __u32 *cnt);
75 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")
76 int btf_ext__reloc_line_info(const struct btf *btf,
77 			     const struct btf_ext *btf_ext,
78 			     const char *sec_name, __u32 insns_cnt,
79 			     void **line_info, __u32 *cnt);
80 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
81 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
82 
83 LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
84 
85 LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
86 LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
87 
88 LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
89 LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
90 LIBBPF_API int btf__add_array(struct btf *btf,
91 			      int index_type_id, int elem_type_id, __u32 nr_elems);
92 /* struct/union construction APIs */
93 LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
94 LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
95 LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
96 			      __u32 bit_offset, __u32 bit_size);
97 
98 /* enum construction APIs */
99 LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
100 LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
101 
102 enum btf_fwd_kind {
103 	BTF_FWD_STRUCT = 0,
104 	BTF_FWD_UNION = 1,
105 	BTF_FWD_ENUM = 2,
106 };
107 
108 LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
109 LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
110 LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
111 LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
112 LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
113 
114 /* func and func_proto construction APIs */
115 LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
116 			     enum btf_func_linkage linkage, int proto_type_id);
117 LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
118 LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
119 
120 /* var & datasec construction APIs */
121 LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
122 LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
123 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
124 					 __u32 offset, __u32 byte_sz);
125 
126 struct btf_dedup_opts {
127 	unsigned int dedup_table_size;
128 	bool dont_resolve_fwds;
129 };
130 
131 LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
132 			  const struct btf_dedup_opts *opts);
133 
134 struct btf_dump;
135 
136 struct btf_dump_opts {
137 	void *ctx;
138 };
139 
140 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
141 
142 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
143 					  const struct btf_ext *btf_ext,
144 					  const struct btf_dump_opts *opts,
145 					  btf_dump_printf_fn_t printf_fn);
146 LIBBPF_API void btf_dump__free(struct btf_dump *d);
147 
148 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
149 
150 struct btf_dump_emit_type_decl_opts {
151 	/* size of this struct, for forward/backward compatiblity */
152 	size_t sz;
153 	/* optional field name for type declaration, e.g.:
154 	 * - struct my_struct <FNAME>
155 	 * - void (*<FNAME>)(int)
156 	 * - char (*<FNAME>)[123]
157 	 */
158 	const char *field_name;
159 	/* extra indentation level (in number of tabs) to emit for multi-line
160 	 * type declarations (e.g., anonymous struct); applies for lines
161 	 * starting from the second one (first line is assumed to have
162 	 * necessary indentation already
163 	 */
164 	int indent_level;
165 	/* strip all the const/volatile/restrict mods */
166 	bool strip_mods;
167 };
168 #define btf_dump_emit_type_decl_opts__last_field strip_mods
169 
170 LIBBPF_API int
171 btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
172 			 const struct btf_dump_emit_type_decl_opts *opts);
173 
174 /*
175  * A set of helpers for easier BTF types handling
176  */
177 static inline __u16 btf_kind(const struct btf_type *t)
178 {
179 	return BTF_INFO_KIND(t->info);
180 }
181 
182 static inline __u16 btf_vlen(const struct btf_type *t)
183 {
184 	return BTF_INFO_VLEN(t->info);
185 }
186 
187 static inline bool btf_kflag(const struct btf_type *t)
188 {
189 	return BTF_INFO_KFLAG(t->info);
190 }
191 
192 static inline bool btf_is_void(const struct btf_type *t)
193 {
194 	return btf_kind(t) == BTF_KIND_UNKN;
195 }
196 
197 static inline bool btf_is_int(const struct btf_type *t)
198 {
199 	return btf_kind(t) == BTF_KIND_INT;
200 }
201 
202 static inline bool btf_is_ptr(const struct btf_type *t)
203 {
204 	return btf_kind(t) == BTF_KIND_PTR;
205 }
206 
207 static inline bool btf_is_array(const struct btf_type *t)
208 {
209 	return btf_kind(t) == BTF_KIND_ARRAY;
210 }
211 
212 static inline bool btf_is_struct(const struct btf_type *t)
213 {
214 	return btf_kind(t) == BTF_KIND_STRUCT;
215 }
216 
217 static inline bool btf_is_union(const struct btf_type *t)
218 {
219 	return btf_kind(t) == BTF_KIND_UNION;
220 }
221 
222 static inline bool btf_is_composite(const struct btf_type *t)
223 {
224 	__u16 kind = btf_kind(t);
225 
226 	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
227 }
228 
229 static inline bool btf_is_enum(const struct btf_type *t)
230 {
231 	return btf_kind(t) == BTF_KIND_ENUM;
232 }
233 
234 static inline bool btf_is_fwd(const struct btf_type *t)
235 {
236 	return btf_kind(t) == BTF_KIND_FWD;
237 }
238 
239 static inline bool btf_is_typedef(const struct btf_type *t)
240 {
241 	return btf_kind(t) == BTF_KIND_TYPEDEF;
242 }
243 
244 static inline bool btf_is_volatile(const struct btf_type *t)
245 {
246 	return btf_kind(t) == BTF_KIND_VOLATILE;
247 }
248 
249 static inline bool btf_is_const(const struct btf_type *t)
250 {
251 	return btf_kind(t) == BTF_KIND_CONST;
252 }
253 
254 static inline bool btf_is_restrict(const struct btf_type *t)
255 {
256 	return btf_kind(t) == BTF_KIND_RESTRICT;
257 }
258 
259 static inline bool btf_is_mod(const struct btf_type *t)
260 {
261 	__u16 kind = btf_kind(t);
262 
263 	return kind == BTF_KIND_VOLATILE ||
264 	       kind == BTF_KIND_CONST ||
265 	       kind == BTF_KIND_RESTRICT;
266 }
267 
268 static inline bool btf_is_func(const struct btf_type *t)
269 {
270 	return btf_kind(t) == BTF_KIND_FUNC;
271 }
272 
273 static inline bool btf_is_func_proto(const struct btf_type *t)
274 {
275 	return btf_kind(t) == BTF_KIND_FUNC_PROTO;
276 }
277 
278 static inline bool btf_is_var(const struct btf_type *t)
279 {
280 	return btf_kind(t) == BTF_KIND_VAR;
281 }
282 
283 static inline bool btf_is_datasec(const struct btf_type *t)
284 {
285 	return btf_kind(t) == BTF_KIND_DATASEC;
286 }
287 
288 static inline __u8 btf_int_encoding(const struct btf_type *t)
289 {
290 	return BTF_INT_ENCODING(*(__u32 *)(t + 1));
291 }
292 
293 static inline __u8 btf_int_offset(const struct btf_type *t)
294 {
295 	return BTF_INT_OFFSET(*(__u32 *)(t + 1));
296 }
297 
298 static inline __u8 btf_int_bits(const struct btf_type *t)
299 {
300 	return BTF_INT_BITS(*(__u32 *)(t + 1));
301 }
302 
303 static inline struct btf_array *btf_array(const struct btf_type *t)
304 {
305 	return (struct btf_array *)(t + 1);
306 }
307 
308 static inline struct btf_enum *btf_enum(const struct btf_type *t)
309 {
310 	return (struct btf_enum *)(t + 1);
311 }
312 
313 static inline struct btf_member *btf_members(const struct btf_type *t)
314 {
315 	return (struct btf_member *)(t + 1);
316 }
317 
318 /* Get bit offset of a member with specified index. */
319 static inline __u32 btf_member_bit_offset(const struct btf_type *t,
320 					  __u32 member_idx)
321 {
322 	const struct btf_member *m = btf_members(t) + member_idx;
323 	bool kflag = btf_kflag(t);
324 
325 	return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
326 }
327 /*
328  * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
329  * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
330  */
331 static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
332 					     __u32 member_idx)
333 {
334 	const struct btf_member *m = btf_members(t) + member_idx;
335 	bool kflag = btf_kflag(t);
336 
337 	return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
338 }
339 
340 static inline struct btf_param *btf_params(const struct btf_type *t)
341 {
342 	return (struct btf_param *)(t + 1);
343 }
344 
345 static inline struct btf_var *btf_var(const struct btf_type *t)
346 {
347 	return (struct btf_var *)(t + 1);
348 }
349 
350 static inline struct btf_var_secinfo *
351 btf_var_secinfos(const struct btf_type *t)
352 {
353 	return (struct btf_var_secinfo *)(t + 1);
354 }
355 
356 #ifdef __cplusplus
357 } /* extern "C" */
358 #endif
359 
360 #endif /* __LIBBPF_BTF_H */
361