blob: b0610dcdae6bd7697fd6ae36bc38d107a691e60a [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002/* Copyright (c) 2018 Facebook */
3
Andrey Ignatoveff81902018-10-03 15:26:42 -07004#ifndef __LIBBPF_BTF_H
5#define __LIBBPF_BTF_H
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07006
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07007#include <linux/types.h>
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07008
Stanislav Fomichev8c4905b2018-11-21 09:29:44 -08009#ifdef __cplusplus
10extern "C" {
11#endif
12
Andrey Ignatovab9e0842018-10-15 22:50:34 -070013#ifndef LIBBPF_API
14#define LIBBPF_API __attribute__((visibility("default")))
15#endif
16
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070017#define BTF_ELF_SEC ".BTF"
Yonghong Song2993e052018-11-19 15:29:16 -080018#define BTF_EXT_ELF_SEC ".BTF.ext"
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070019
20struct btf;
Yonghong Song2993e052018-11-19 15:29:16 -080021struct btf_ext;
Martin KaFai Lau38d5d3b2018-07-24 08:40:22 -070022struct btf_type;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070023
Yonghong Song2993e052018-11-19 15:29:16 -080024/*
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 */
45struct 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;
Martin KaFai Lau3d650142018-12-07 16:42:31 -080054 __u32 line_info_off;
55 __u32 line_info_len;
Yonghong Song2993e052018-11-19 15:29:16 -080056};
57
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070058typedef int (*btf_print_fn_t)(const char *, ...)
59 __attribute__((format(printf, 1, 2)));
60
Andrey Ignatovab9e0842018-10-15 22:50:34 -070061LIBBPF_API void btf__free(struct btf *btf);
62LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log);
63LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
64 const char *type_name);
65LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
66 __u32 id);
67LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
68LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
69LIBBPF_API int btf__fd(const struct btf *btf);
70LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
Martin KaFai Lau1d2f44c2018-11-23 16:44:32 -080071LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070072
Yonghong Song2993e052018-11-19 15:29:16 -080073struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log);
74void btf_ext__free(struct btf_ext *btf_ext);
Martin KaFai Lau3d650142018-12-07 16:42:31 -080075int btf_ext__reloc_func_info(const struct btf *btf,
76 const struct btf_ext *btf_ext,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -080077 const char *sec_name, __u32 insns_cnt,
78 void **func_info, __u32 *func_info_len);
Martin KaFai Lau3d650142018-12-07 16:42:31 -080079int btf_ext__reloc_line_info(const struct btf *btf,
80 const struct btf_ext *btf_ext,
81 const char *sec_name, __u32 insns_cnt,
82 void **line_info, __u32 *cnt);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -080083__u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
Martin KaFai Lau3d650142018-12-07 16:42:31 -080084__u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
Yonghong Song2993e052018-11-19 15:29:16 -080085
Stanislav Fomichev8c4905b2018-11-21 09:29:44 -080086#ifdef __cplusplus
87} /* extern "C" */
88#endif
89
Andrey Ignatoveff81902018-10-03 15:26:42 -070090#endif /* __LIBBPF_BTF_H */