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 | |
Martin KaFai Lau | 5b891af | 2018-07-24 08:40:21 -0700 | [diff] [blame] | 7 | #include <linux/types.h> |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 8 | |
Stanislav Fomichev | 8c4905b | 2018-11-21 09:29:44 -0800 | [diff] [blame] | 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 13 | #ifndef LIBBPF_API |
| 14 | #define LIBBPF_API __attribute__((visibility("default"))) |
| 15 | #endif |
| 16 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 17 | #define BTF_ELF_SEC ".BTF" |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 18 | #define BTF_EXT_ELF_SEC ".BTF.ext" |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 19 | |
| 20 | struct btf; |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 21 | struct btf_ext; |
Martin KaFai Lau | 38d5d3b | 2018-07-24 08:40:22 -0700 | [diff] [blame] | 22 | struct btf_type; |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 23 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 24 | /* |
| 25 | * The .BTF.ext ELF section layout defined as |
| 26 | * struct btf_ext_header |
| 27 | * func_info subsection |
| 28 | * |
| 29 | * The func_info subsection layout: |
| 30 | * record size for struct bpf_func_info in the func_info subsection |
| 31 | * struct btf_sec_func_info for section #1 |
| 32 | * a list of bpf_func_info records for section #1 |
| 33 | * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h |
| 34 | * but may not be identical |
| 35 | * struct btf_sec_func_info for section #2 |
| 36 | * a list of bpf_func_info records for section #2 |
| 37 | * ...... |
| 38 | * |
| 39 | * Note that the bpf_func_info record size in .BTF.ext may not |
| 40 | * be the same as the one defined in include/uapi/linux/bpf.h. |
| 41 | * The loader should ensure that record_size meets minimum |
| 42 | * requirement and pass the record as is to the kernel. The |
| 43 | * kernel will handle the func_info properly based on its contents. |
| 44 | */ |
| 45 | struct btf_ext_header { |
| 46 | __u16 magic; |
| 47 | __u8 version; |
| 48 | __u8 flags; |
| 49 | __u32 hdr_len; |
| 50 | |
| 51 | /* All offsets are in bytes relative to the end of this header */ |
| 52 | __u32 func_info_off; |
| 53 | __u32 func_info_len; |
| 54 | }; |
| 55 | |
| 56 | struct btf_sec_func_info { |
| 57 | __u32 sec_name_off; |
| 58 | __u32 num_func_info; |
| 59 | /* Followed by num_func_info number of bpf func_info records */ |
| 60 | __u8 data[0]; |
| 61 | }; |
| 62 | |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 63 | typedef int (*btf_print_fn_t)(const char *, ...) |
| 64 | __attribute__((format(printf, 1, 2))); |
| 65 | |
Andrey Ignatov | ab9e084 | 2018-10-15 22:50:34 -0700 | [diff] [blame] | 66 | LIBBPF_API void btf__free(struct btf *btf); |
| 67 | LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log); |
| 68 | LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, |
| 69 | const char *type_name); |
| 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 char *btf__name_by_offset(const struct btf *btf, __u32 offset); |
Martin KaFai Lau | 1d2f44c | 2018-11-23 16:44:32 -0800 | [diff] [blame^] | 76 | LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); |
Martin KaFai Lau | 8a138ae | 2018-04-18 15:56:05 -0700 | [diff] [blame] | 77 | |
Yonghong Song | 2993e05 | 2018-11-19 15:29:16 -0800 | [diff] [blame] | 78 | struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log); |
| 79 | void btf_ext__free(struct btf_ext *btf_ext); |
| 80 | int btf_ext__reloc_init(struct btf *btf, struct btf_ext *btf_ext, |
| 81 | const char *sec_name, void **func_info, |
| 82 | __u32 *func_info_rec_size, __u32 *func_info_len); |
| 83 | int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext, |
| 84 | const char *sec_name, __u32 insns_cnt, void **func_info, |
| 85 | __u32 *func_info_len); |
| 86 | |
Stanislav Fomichev | 8c4905b | 2018-11-21 09:29:44 -0800 | [diff] [blame] | 87 | #ifdef __cplusplus |
| 88 | } /* extern "C" */ |
| 89 | #endif |
| 90 | |
Andrey Ignatov | eff8190 | 2018-10-03 15:26:42 -0700 | [diff] [blame] | 91 | #endif /* __LIBBPF_BTF_H */ |