1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2018 Facebook */
10 #include <linux/types.h>
12 #include "libbpf_common.h"
18 #define BTF_ELF_SEC ".BTF"
19 #define BTF_EXT_ELF_SEC ".BTF.ext"
20 #define MAPS_ELF_SEC ".maps"
29 BTF_LITTLE_ENDIAN = 0,
33 LIBBPF_API void btf__free(struct btf *btf);
35 LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
36 LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
37 LIBBPF_API struct btf *btf__new_empty(void);
38 LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
40 LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
41 LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
42 LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
43 LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
44 LIBBPF_API struct btf *btf__parse_raw(const char *path);
45 LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
47 LIBBPF_API struct btf *btf__load_vmlinux_btf(void);
48 LIBBPF_API struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf);
49 LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
51 LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id);
52 LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf);
53 LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
55 LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
56 LIBBPF_API int btf__load(struct btf *btf);
57 LIBBPF_API int btf__load_into_kernel(struct btf *btf);
58 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
59 const char *type_name);
60 LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
61 const char *type_name, __u32 kind);
62 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
63 LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
64 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
66 LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
67 LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
68 LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
69 LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
70 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
71 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
72 LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
73 LIBBPF_API int btf__fd(const struct btf *btf);
74 LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
75 LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
76 LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
77 LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
78 LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
79 __u32 expected_key_size,
80 __u32 expected_value_size,
81 __u32 *key_type_id, __u32 *value_type_id);
83 LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size);
84 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
85 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
87 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")
88 int btf_ext__reloc_func_info(const struct btf *btf,
89 const struct btf_ext *btf_ext,
90 const char *sec_name, __u32 insns_cnt,
91 void **func_info, __u32 *cnt);
92 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")
93 int btf_ext__reloc_line_info(const struct btf *btf,
94 const struct btf_ext *btf_ext,
95 const char *sec_name, __u32 insns_cnt,
96 void **line_info, __u32 *cnt);
97 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
98 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
100 LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
101 LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
102 LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf,
103 const struct btf_type *src_type);
105 LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
106 LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);
107 LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
108 LIBBPF_API int btf__add_array(struct btf *btf,
109 int index_type_id, int elem_type_id, __u32 nr_elems);
110 /* struct/union construction APIs */
111 LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
112 LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
113 LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
114 __u32 bit_offset, __u32 bit_size);
116 /* enum construction APIs */
117 LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
118 LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
126 LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
127 LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
128 LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
129 LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
130 LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
132 /* func and func_proto construction APIs */
133 LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
134 enum btf_func_linkage linkage, int proto_type_id);
135 LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
136 LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
138 /* var & datasec construction APIs */
139 LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
140 LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
141 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
142 __u32 offset, __u32 byte_sz);
144 struct btf_dedup_opts {
145 unsigned int dedup_table_size;
146 bool dont_resolve_fwds;
149 LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
150 const struct btf_dedup_opts *opts);
154 struct btf_dump_opts {
158 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
160 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
161 const struct btf_ext *btf_ext,
162 const struct btf_dump_opts *opts,
163 btf_dump_printf_fn_t printf_fn);
164 LIBBPF_API void btf_dump__free(struct btf_dump *d);
166 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
168 struct btf_dump_emit_type_decl_opts {
169 /* size of this struct, for forward/backward compatiblity */
171 /* optional field name for type declaration, e.g.:
172 * - struct my_struct <FNAME>
173 * - void (*<FNAME>)(int)
174 * - char (*<FNAME>)[123]
176 const char *field_name;
177 /* extra indentation level (in number of tabs) to emit for multi-line
178 * type declarations (e.g., anonymous struct); applies for lines
179 * starting from the second one (first line is assumed to have
180 * necessary indentation already
183 /* strip all the const/volatile/restrict mods */
187 #define btf_dump_emit_type_decl_opts__last_field strip_mods
190 btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
191 const struct btf_dump_emit_type_decl_opts *opts);
194 struct btf_dump_type_data_opts {
195 /* size of this struct, for forward/backward compatibility */
197 const char *indent_str;
199 /* below match "show" flags for bpf_show_snprintf() */
200 bool compact; /* no newlines/indentation */
201 bool skip_names; /* skip member/type names */
202 bool emit_zeroes; /* show 0-valued fields */
205 #define btf_dump_type_data_opts__last_field emit_zeroes
208 btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
209 const void *data, size_t data_sz,
210 const struct btf_dump_type_data_opts *opts);
213 * A set of helpers for easier BTF types handling
215 static inline __u16 btf_kind(const struct btf_type *t)
217 return BTF_INFO_KIND(t->info);
220 static inline __u16 btf_vlen(const struct btf_type *t)
222 return BTF_INFO_VLEN(t->info);
225 static inline bool btf_kflag(const struct btf_type *t)
227 return BTF_INFO_KFLAG(t->info);
230 static inline bool btf_is_void(const struct btf_type *t)
232 return btf_kind(t) == BTF_KIND_UNKN;
235 static inline bool btf_is_int(const struct btf_type *t)
237 return btf_kind(t) == BTF_KIND_INT;
240 static inline bool btf_is_ptr(const struct btf_type *t)
242 return btf_kind(t) == BTF_KIND_PTR;
245 static inline bool btf_is_array(const struct btf_type *t)
247 return btf_kind(t) == BTF_KIND_ARRAY;
250 static inline bool btf_is_struct(const struct btf_type *t)
252 return btf_kind(t) == BTF_KIND_STRUCT;
255 static inline bool btf_is_union(const struct btf_type *t)
257 return btf_kind(t) == BTF_KIND_UNION;
260 static inline bool btf_is_composite(const struct btf_type *t)
262 __u16 kind = btf_kind(t);
264 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
267 static inline bool btf_is_enum(const struct btf_type *t)
269 return btf_kind(t) == BTF_KIND_ENUM;
272 static inline bool btf_is_fwd(const struct btf_type *t)
274 return btf_kind(t) == BTF_KIND_FWD;
277 static inline bool btf_is_typedef(const struct btf_type *t)
279 return btf_kind(t) == BTF_KIND_TYPEDEF;
282 static inline bool btf_is_volatile(const struct btf_type *t)
284 return btf_kind(t) == BTF_KIND_VOLATILE;
287 static inline bool btf_is_const(const struct btf_type *t)
289 return btf_kind(t) == BTF_KIND_CONST;
292 static inline bool btf_is_restrict(const struct btf_type *t)
294 return btf_kind(t) == BTF_KIND_RESTRICT;
297 static inline bool btf_is_mod(const struct btf_type *t)
299 __u16 kind = btf_kind(t);
301 return kind == BTF_KIND_VOLATILE ||
302 kind == BTF_KIND_CONST ||
303 kind == BTF_KIND_RESTRICT;
306 static inline bool btf_is_func(const struct btf_type *t)
308 return btf_kind(t) == BTF_KIND_FUNC;
311 static inline bool btf_is_func_proto(const struct btf_type *t)
313 return btf_kind(t) == BTF_KIND_FUNC_PROTO;
316 static inline bool btf_is_var(const struct btf_type *t)
318 return btf_kind(t) == BTF_KIND_VAR;
321 static inline bool btf_is_datasec(const struct btf_type *t)
323 return btf_kind(t) == BTF_KIND_DATASEC;
326 static inline bool btf_is_float(const struct btf_type *t)
328 return btf_kind(t) == BTF_KIND_FLOAT;
331 static inline __u8 btf_int_encoding(const struct btf_type *t)
333 return BTF_INT_ENCODING(*(__u32 *)(t + 1));
336 static inline __u8 btf_int_offset(const struct btf_type *t)
338 return BTF_INT_OFFSET(*(__u32 *)(t + 1));
341 static inline __u8 btf_int_bits(const struct btf_type *t)
343 return BTF_INT_BITS(*(__u32 *)(t + 1));
346 static inline struct btf_array *btf_array(const struct btf_type *t)
348 return (struct btf_array *)(t + 1);
351 static inline struct btf_enum *btf_enum(const struct btf_type *t)
353 return (struct btf_enum *)(t + 1);
356 static inline struct btf_member *btf_members(const struct btf_type *t)
358 return (struct btf_member *)(t + 1);
361 /* Get bit offset of a member with specified index. */
362 static inline __u32 btf_member_bit_offset(const struct btf_type *t,
365 const struct btf_member *m = btf_members(t) + member_idx;
366 bool kflag = btf_kflag(t);
368 return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
371 * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
372 * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
374 static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
377 const struct btf_member *m = btf_members(t) + member_idx;
378 bool kflag = btf_kflag(t);
380 return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
383 static inline struct btf_param *btf_params(const struct btf_type *t)
385 return (struct btf_param *)(t + 1);
388 static inline struct btf_var *btf_var(const struct btf_type *t)
390 return (struct btf_var *)(t + 1);
393 static inline struct btf_var_secinfo *
394 btf_var_secinfos(const struct btf_type *t)
396 return (struct btf_var_secinfo *)(t + 1);
403 #endif /* __LIBBPF_BTF_H */