btf.h 2.8 KB
Newer Older
1
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 3
/* Copyright (c) 2018 Facebook */

4 5
#ifndef __LIBBPF_BTF_H
#define __LIBBPF_BTF_H
6

7
#include <linux/types.h>
8

9 10 11 12
#ifdef __cplusplus
extern "C" {
#endif

13 14 15 16
#ifndef LIBBPF_API
#define LIBBPF_API __attribute__((visibility("default")))
#endif

17
#define BTF_ELF_SEC ".BTF"
18
#define BTF_EXT_ELF_SEC ".BTF.ext"
19 20

struct btf;
21
struct btf_ext;
22
struct btf_type;
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
 * The .BTF.ext ELF section layout defined as
 *   struct btf_ext_header
 *   func_info subsection
 *
 * The func_info subsection layout:
 *   record size for struct bpf_func_info in the func_info subsection
 *   struct btf_sec_func_info for section #1
 *   a list of bpf_func_info records for section #1
 *     where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
 *     but may not be identical
 *   struct btf_sec_func_info for section #2
 *   a list of bpf_func_info records for section #2
 *   ......
 *
 * Note that the bpf_func_info record size in .BTF.ext may not
 * be the same as the one defined in include/uapi/linux/bpf.h.
 * The loader should ensure that record_size meets minimum
 * requirement and pass the record as is to the kernel. The
 * kernel will handle the func_info properly based on its contents.
 */
struct btf_ext_header {
	__u16	magic;
	__u8	version;
	__u8	flags;
	__u32	hdr_len;

	/* All offsets are in bytes relative to the end of this header */
	__u32	func_info_off;
	__u32	func_info_len;
};

struct btf_sec_func_info {
	__u32	sec_name_off;
	__u32	num_func_info;
	/* Followed by num_func_info number of bpf func_info records */
	__u8	data[0];
};

63 64 65
typedef int (*btf_print_fn_t)(const char *, ...)
	__attribute__((format(printf, 1, 2)));

66 67 68 69 70 71 72 73 74 75
LIBBPF_API void btf__free(struct btf *btf);
LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log);
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
				   const char *type_name);
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
						  __u32 id);
LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__fd(const struct btf *btf);
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
76
LIBBPF_API int btf_get_from_id(__u32 id, struct btf **btf);
77

78 79 80 81 82 83 84 85 86
struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log);
void btf_ext__free(struct btf_ext *btf_ext);
int btf_ext__reloc_init(struct btf *btf, struct btf_ext *btf_ext,
			const char *sec_name, void **func_info,
			__u32 *func_info_rec_size, __u32 *func_info_len);
int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext,
		   const char *sec_name, __u32 insns_cnt, void **func_info,
		   __u32 *func_info_len);

87 88 89 90
#ifdef __cplusplus
} /* extern "C" */
#endif

91
#endif /* __LIBBPF_BTF_H */