Alexei Starovoitov | 1bc38b8 | 2018-10-05 16:40:00 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2018 Facebook */ |
| 3 | |
Andrey Ignatov | eff8190 | 2018-10-03 15:26:42 -0700 | [diff] [blame] | 4 | #ifndef __LIBBPF_BTF_H |
| 5 | #define __LIBBPF_BTF_H |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 6 | |
Andrii Nakryiko | 351131b | 2019-05-24 11:59:03 -0700 | [diff] [blame] | 7 | #include <stdarg.h> |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 8 | #include <linux/types.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 9 | |
Stanislav Fomichev | 8c4905b | 2018-11-21 09:29:44 -0800 | [diff] [blame] | 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 14 | #ifndef LIBBPF_API |
| 15 | #define LIBBPF_API __attribute__((visibility("default"))) |
| 16 | #endif |
| 17 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 18 | #define BTF_ELF_SEC ".BTF" |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 19 | #define BTF_EXT_ELF_SEC ".BTF.ext" |
Andrii Nakryiko | abd29c9 | 2019-06-17 12:26:56 -0700 | [diff] [blame] | 20 | #define MAPS_ELF_SEC ".maps" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 21 | |
| 22 | struct btf; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 23 | struct btf_ext; |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 24 | struct btf_type; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 25 | |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 26 | struct bpf_object; |
| 27 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 28 | /* |
| 29 | * The .BTF.ext ELF section layout defined as |
| 30 | * struct btf_ext_header |
| 31 | * func_info subsection |
| 32 | * |
| 33 | * The func_info subsection layout: |
| 34 | * record size for struct bpf_func_info in the func_info subsection |
| 35 | * struct btf_sec_func_info for section #1 |
| 36 | * a list of bpf_func_info records for section #1 |
| 37 | * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h |
| 38 | * but may not be identical |
| 39 | * struct btf_sec_func_info for section #2 |
| 40 | * a list of bpf_func_info records for section #2 |
| 41 | * ...... |
| 42 | * |
| 43 | * Note that the bpf_func_info record size in .BTF.ext may not |
| 44 | * be the same as the one defined in include/uapi/linux/bpf.h. |
| 45 | * The loader should ensure that record_size meets minimum |
| 46 | * requirement and pass the record as is to the kernel. The |
| 47 | * kernel will handle the func_info properly based on its contents. |
| 48 | */ |
| 49 | struct btf_ext_header { |
| 50 | __u16 magic; |
| 51 | __u8 version; |
| 52 | __u8 flags; |
| 53 | __u32 hdr_len; |
| 54 | |
| 55 | /* All offsets are in bytes relative to the end of this header */ |
| 56 | __u32 func_info_off; |
| 57 | __u32 func_info_len; |
Martin KaFai Lau | 3d65014 | 2018-12-07 16:42:31 -0800 | [diff] [blame] | 58 | __u32 line_info_off; |
| 59 | __u32 line_info_len; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 62 | LIBBPF_API void btf__free(struct btf *btf); |
Yonghong Song | 8461ef8 | 2019-02-01 16:14:14 -0800 | [diff] [blame] | 63 | LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size); |
Andrii Nakryiko | e6c6485 | 2019-05-24 11:58:57 -0700 | [diff] [blame] | 64 | LIBBPF_API struct btf *btf__parse_elf(const char *path, |
| 65 | struct btf_ext **btf_ext); |
Daniel Borkmann | 1713d68 | 2019-04-09 23:20:14 +0200 | [diff] [blame] | 66 | LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); |
Andrii Nakryiko | d29d87f | 2019-02-08 11:19:36 -0800 | [diff] [blame] | 67 | LIBBPF_API int btf__load(struct btf *btf); |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 68 | LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, |
| 69 | const char *type_name); |
Andrii Nakryiko | 9c65112 | 2019-02-04 17:29:46 -0800 | [diff] [blame] | 70 | LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf); |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 71 | LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, |
| 72 | __u32 id); |
| 73 | LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); |
| 74 | LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); |
| 75 | LIBBPF_API int btf__fd(const struct btf *btf); |
Andrii Nakryiko | 02c8744 | 2019-02-08 11:19:37 -0800 | [diff] [blame] | 76 | LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size); |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 77 | LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); |
Martin KaFai Lau | 1d2f44c | 2018-11-23 16:44:32 -0800 | [diff] [blame] | 78 | LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); |
Yonghong Song | a6c109a | 2019-02-05 11:48:22 -0800 | [diff] [blame] | 79 | LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, |
Yonghong Song | 96408c4 | 2019-02-04 11:00:58 -0800 | [diff] [blame] | 80 | __u32 expected_key_size, |
| 81 | __u32 expected_value_size, |
| 82 | __u32 *key_type_id, __u32 *value_type_id); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 83 | |
Yonghong Song | b8dcf8d | 2019-02-04 11:00:57 -0800 | [diff] [blame] | 84 | LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size); |
| 85 | LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); |
Andrii Nakryiko | 1baabdc | 2019-02-28 15:31:22 -0800 | [diff] [blame] | 86 | LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, |
Andrii Nakryiko | ae4ab4b | 2019-02-08 11:19:38 -0800 | [diff] [blame] | 87 | __u32 *size); |
Yonghong Song | b8dcf8d | 2019-02-04 11:00:57 -0800 | [diff] [blame] | 88 | LIBBPF_API 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 int btf_ext__reloc_line_info(const struct btf *btf, |
| 93 | const struct btf_ext *btf_ext, |
| 94 | const char *sec_name, __u32 insns_cnt, |
| 95 | void **line_info, __u32 *cnt); |
| 96 | LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext); |
| 97 | LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext); |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 98 | |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 99 | struct btf_dedup_opts { |
Andrii Nakryiko | 51edf5f | 2019-02-28 15:31:23 -0800 | [diff] [blame] | 100 | unsigned int dedup_table_size; |
Andrii Nakryiko | d5caef5 | 2019-02-04 17:29:45 -0800 | [diff] [blame] | 101 | bool dont_resolve_fwds; |
| 102 | }; |
| 103 | |
| 104 | LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext, |
| 105 | const struct btf_dedup_opts *opts); |
| 106 | |
Andrii Nakryiko | 351131b | 2019-05-24 11:59:03 -0700 | [diff] [blame] | 107 | struct btf_dump; |
| 108 | |
| 109 | struct btf_dump_opts { |
| 110 | void *ctx; |
| 111 | }; |
| 112 | |
| 113 | typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args); |
| 114 | |
| 115 | LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf, |
| 116 | const struct btf_ext *btf_ext, |
| 117 | const struct btf_dump_opts *opts, |
| 118 | btf_dump_printf_fn_t printf_fn); |
| 119 | LIBBPF_API void btf_dump__free(struct btf_dump *d); |
| 120 | |
| 121 | LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id); |
| 122 | |
Stanislav Fomichev | 8c4905b | 2018-11-21 09:29:44 -0800 | [diff] [blame] | 123 | #ifdef __cplusplus |
| 124 | } /* extern "C" */ |
| 125 | #endif |
| 126 | |
Andrey Ignatov | eff8190 | 2018-10-03 15:26:42 -0700 | [diff] [blame] | 127 | #endif /* __LIBBPF_BTF_H */ |