xref: /linux/tools/lib/bpf/btf.h (revision a6cdeeb16bff89c8486324f53577db058cbe81ba)
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/types.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #ifndef LIBBPF_API
15 #define LIBBPF_API __attribute__((visibility("default")))
16 #endif
17 
18 #define BTF_ELF_SEC ".BTF"
19 #define BTF_EXT_ELF_SEC ".BTF.ext"
20 
21 struct btf;
22 struct btf_ext;
23 struct btf_type;
24 
25 struct bpf_object;
26 
27 /*
28  * The .BTF.ext ELF section layout defined as
29  *   struct btf_ext_header
30  *   func_info subsection
31  *
32  * The func_info subsection layout:
33  *   record size for struct bpf_func_info in the func_info subsection
34  *   struct btf_sec_func_info for section #1
35  *   a list of bpf_func_info records for section #1
36  *     where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
37  *     but may not be identical
38  *   struct btf_sec_func_info for section #2
39  *   a list of bpf_func_info records for section #2
40  *   ......
41  *
42  * Note that the bpf_func_info record size in .BTF.ext may not
43  * be the same as the one defined in include/uapi/linux/bpf.h.
44  * The loader should ensure that record_size meets minimum
45  * requirement and pass the record as is to the kernel. The
46  * kernel will handle the func_info properly based on its contents.
47  */
48 struct btf_ext_header {
49 	__u16	magic;
50 	__u8	version;
51 	__u8	flags;
52 	__u32	hdr_len;
53 
54 	/* All offsets are in bytes relative to the end of this header */
55 	__u32	func_info_off;
56 	__u32	func_info_len;
57 	__u32	line_info_off;
58 	__u32	line_info_len;
59 };
60 
61 LIBBPF_API void btf__free(struct btf *btf);
62 LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size);
63 LIBBPF_API struct btf *btf__parse_elf(const char *path,
64 				      struct btf_ext **btf_ext);
65 LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
66 LIBBPF_API int btf__load(struct btf *btf);
67 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
68 				   const char *type_name);
69 LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
70 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
71 						  __u32 id);
72 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
73 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
74 LIBBPF_API int btf__fd(const struct btf *btf);
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 int btf__get_from_id(__u32 id, struct btf **btf);
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);
82 
83 LIBBPF_API struct btf_ext *btf_ext__new(__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,
86 					     __u32 *size);
87 LIBBPF_API int btf_ext__reloc_func_info(const struct btf *btf,
88 					const struct btf_ext *btf_ext,
89 					const char *sec_name, __u32 insns_cnt,
90 					void **func_info, __u32 *cnt);
91 LIBBPF_API int btf_ext__reloc_line_info(const struct btf *btf,
92 					const struct btf_ext *btf_ext,
93 					const char *sec_name, __u32 insns_cnt,
94 					void **line_info, __u32 *cnt);
95 LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
96 LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
97 
98 struct btf_dedup_opts {
99 	unsigned int dedup_table_size;
100 	bool dont_resolve_fwds;
101 };
102 
103 LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
104 			  const struct btf_dedup_opts *opts);
105 
106 struct btf_dump;
107 
108 struct btf_dump_opts {
109 	void *ctx;
110 };
111 
112 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
113 
114 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
115 					  const struct btf_ext *btf_ext,
116 					  const struct btf_dump_opts *opts,
117 					  btf_dump_printf_fn_t printf_fn);
118 LIBBPF_API void btf_dump__free(struct btf_dump *d);
119 
120 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
121 
122 #ifdef __cplusplus
123 } /* extern "C" */
124 #endif
125 
126 #endif /* __LIBBPF_BTF_H */
127