xref: /openbmc/linux/tools/lib/bpf/libbpf_internal.h (revision 3fc32f40)
1d7c4b398SAndrii Nakryiko /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2d7c4b398SAndrii Nakryiko 
3d7c4b398SAndrii Nakryiko /*
4d7c4b398SAndrii Nakryiko  * Internal libbpf helpers.
5d7c4b398SAndrii Nakryiko  *
6d7c4b398SAndrii Nakryiko  * Copyright (c) 2019 Facebook
7d7c4b398SAndrii Nakryiko  */
8d7c4b398SAndrii Nakryiko 
9d7c4b398SAndrii Nakryiko #ifndef __LIBBPF_LIBBPF_INTERNAL_H
10d7c4b398SAndrii Nakryiko #define __LIBBPF_LIBBPF_INTERNAL_H
11d7c4b398SAndrii Nakryiko 
12029258d7SAndrii Nakryiko #include <stdlib.h>
1385367030SAndrii Nakryiko 
1485367030SAndrii Nakryiko /* make sure libbpf doesn't use kernel-only integer typedefs */
1585367030SAndrii Nakryiko #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
1685367030SAndrii Nakryiko 
1785367030SAndrii Nakryiko /* prevent accidental re-addition of reallocarray() */
1885367030SAndrii Nakryiko #pragma GCC poison reallocarray
1985367030SAndrii Nakryiko 
201d7a08b3SAndrii Nakryiko #include "libbpf.h"
211d7a08b3SAndrii Nakryiko 
22d7c4b398SAndrii Nakryiko #define BTF_INFO_ENC(kind, kind_flag, vlen) \
23d7c4b398SAndrii Nakryiko 	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
24d7c4b398SAndrii Nakryiko #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
25d7c4b398SAndrii Nakryiko #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
26d7c4b398SAndrii Nakryiko 	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
27d7c4b398SAndrii Nakryiko #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
28d7c4b398SAndrii Nakryiko 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
29d7c4b398SAndrii Nakryiko 	BTF_INT_ENC(encoding, bits_offset, bits)
30d7c4b398SAndrii Nakryiko #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
31d7c4b398SAndrii Nakryiko #define BTF_PARAM_ENC(name, type) (name), (type)
32d7c4b398SAndrii Nakryiko #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
33d7c4b398SAndrii Nakryiko 
34029258d7SAndrii Nakryiko #ifndef likely
35029258d7SAndrii Nakryiko #define likely(x) __builtin_expect(!!(x), 1)
36029258d7SAndrii Nakryiko #endif
37029258d7SAndrii Nakryiko #ifndef unlikely
38029258d7SAndrii Nakryiko #define unlikely(x) __builtin_expect(!!(x), 0)
39029258d7SAndrii Nakryiko #endif
40d7fe74f9SAndrii Nakryiko #ifndef min
41d7fe74f9SAndrii Nakryiko # define min(x, y) ((x) < (y) ? (x) : (y))
42d7fe74f9SAndrii Nakryiko #endif
43d7fe74f9SAndrii Nakryiko #ifndef max
44d7fe74f9SAndrii Nakryiko # define max(x, y) ((x) < (y) ? (y) : (x))
45d7fe74f9SAndrii Nakryiko #endif
464cedc0daSAndrii Nakryiko #ifndef offsetofend
474cedc0daSAndrii Nakryiko # define offsetofend(TYPE, FIELD) \
484cedc0daSAndrii Nakryiko 	(offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
494cedc0daSAndrii Nakryiko #endif
50d7fe74f9SAndrii Nakryiko 
511bd63524SYonghong Song /* Symbol versioning is different between static and shared library.
521bd63524SYonghong Song  * Properly versioned symbols are needed for shared library, but
531bd63524SYonghong Song  * only the symbol of the new version is needed for static library.
541bd63524SYonghong Song  */
551bd63524SYonghong Song #ifdef SHARED
561bd63524SYonghong Song # define COMPAT_VERSION(internal_name, api_name, version) \
571bd63524SYonghong Song 	asm(".symver " #internal_name "," #api_name "@" #version);
581bd63524SYonghong Song # define DEFAULT_VERSION(internal_name, api_name, version) \
591bd63524SYonghong Song 	asm(".symver " #internal_name "," #api_name "@@" #version);
601bd63524SYonghong Song #else
611bd63524SYonghong Song # define COMPAT_VERSION(internal_name, api_name, version)
621bd63524SYonghong Song # define DEFAULT_VERSION(internal_name, api_name, version) \
631bd63524SYonghong Song 	extern typeof(internal_name) api_name \
641bd63524SYonghong Song 	__attribute__((alias(#internal_name)));
651bd63524SYonghong Song #endif
661bd63524SYonghong Song 
67d72386feSAndrii Nakryiko extern void libbpf_print(enum libbpf_print_level level,
68d72386feSAndrii Nakryiko 			 const char *format, ...)
69d72386feSAndrii Nakryiko 	__attribute__((format(printf, 2, 3)));
70d72386feSAndrii Nakryiko 
71d72386feSAndrii Nakryiko #define __pr(level, fmt, ...)	\
72d72386feSAndrii Nakryiko do {				\
73d72386feSAndrii Nakryiko 	libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__);	\
74d72386feSAndrii Nakryiko } while (0)
75d72386feSAndrii Nakryiko 
76be18010eSKefeng Wang #define pr_warn(fmt, ...)	__pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
77d72386feSAndrii Nakryiko #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
78d72386feSAndrii Nakryiko #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
79d72386feSAndrii Nakryiko 
80029258d7SAndrii Nakryiko /*
81029258d7SAndrii Nakryiko  * Re-implement glibc's reallocarray() for libbpf internal-only use.
82029258d7SAndrii Nakryiko  * reallocarray(), unfortunately, is not available in all versions of glibc,
83029258d7SAndrii Nakryiko  * so requires extra feature detection and using reallocarray() stub from
84029258d7SAndrii Nakryiko  * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
85029258d7SAndrii Nakryiko  * build of libbpf unnecessarily and is just a maintenance burden. Instead,
86029258d7SAndrii Nakryiko  * it's trivial to implement libbpf-specific internal version and use it
87029258d7SAndrii Nakryiko  * throughout libbpf.
88029258d7SAndrii Nakryiko  */
89029258d7SAndrii Nakryiko static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
90029258d7SAndrii Nakryiko {
91029258d7SAndrii Nakryiko 	size_t total;
92029258d7SAndrii Nakryiko 
93029258d7SAndrii Nakryiko 	if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
94029258d7SAndrii Nakryiko 		return NULL;
95029258d7SAndrii Nakryiko 	return realloc(ptr, total);
96029258d7SAndrii Nakryiko }
97029258d7SAndrii Nakryiko 
982ce8450eSAndrii Nakryiko static inline bool libbpf_validate_opts(const char *opts,
992ce8450eSAndrii Nakryiko 					size_t opts_sz, size_t user_sz,
1002ce8450eSAndrii Nakryiko 					const char *type_name)
1012ce8450eSAndrii Nakryiko {
1022ce8450eSAndrii Nakryiko 	if (user_sz < sizeof(size_t)) {
103be18010eSKefeng Wang 		pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
1042ce8450eSAndrii Nakryiko 		return false;
1052ce8450eSAndrii Nakryiko 	}
1062ce8450eSAndrii Nakryiko 	if (user_sz > opts_sz) {
1072ce8450eSAndrii Nakryiko 		size_t i;
1082ce8450eSAndrii Nakryiko 
1092ce8450eSAndrii Nakryiko 		for (i = opts_sz; i < user_sz; i++) {
1102ce8450eSAndrii Nakryiko 			if (opts[i]) {
11112dd14b2SToke Høiland-Jørgensen 				pr_warn("%s has non-zero extra bytes\n",
1122ce8450eSAndrii Nakryiko 					type_name);
1132ce8450eSAndrii Nakryiko 				return false;
1142ce8450eSAndrii Nakryiko 			}
1152ce8450eSAndrii Nakryiko 		}
1162ce8450eSAndrii Nakryiko 	}
1172ce8450eSAndrii Nakryiko 	return true;
1182ce8450eSAndrii Nakryiko }
1192ce8450eSAndrii Nakryiko 
1202ce8450eSAndrii Nakryiko #define OPTS_VALID(opts, type)						      \
1212ce8450eSAndrii Nakryiko 	(!(opts) || libbpf_validate_opts((const char *)opts,		      \
1222ce8450eSAndrii Nakryiko 					 offsetofend(struct type,	      \
1232ce8450eSAndrii Nakryiko 						     type##__last_field),     \
1242ce8450eSAndrii Nakryiko 					 (opts)->sz, #type))
1252ce8450eSAndrii Nakryiko #define OPTS_HAS(opts, field) \
1262ce8450eSAndrii Nakryiko 	((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
1272ce8450eSAndrii Nakryiko #define OPTS_GET(opts, field, fallback_value) \
1282ce8450eSAndrii Nakryiko 	(OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
1292ce8450eSAndrii Nakryiko 
1306803ee25SAndrii Nakryiko int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
1316803ee25SAndrii Nakryiko int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
132cfd49210SMichal Rostecki int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
133d7c4b398SAndrii Nakryiko 			 const char *str_sec, size_t str_len);
134d7c4b398SAndrii Nakryiko 
135612d05beSAndrii Nakryiko int bpf_object__section_size(const struct bpf_object *obj, const char *name,
136612d05beSAndrii Nakryiko 			     __u32 *size);
137612d05beSAndrii Nakryiko int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
138612d05beSAndrii Nakryiko 				__u32 *off);
139612d05beSAndrii Nakryiko 
1404cedc0daSAndrii Nakryiko struct btf_ext_info {
1414cedc0daSAndrii Nakryiko 	/*
1424cedc0daSAndrii Nakryiko 	 * info points to the individual info section (e.g. func_info and
1434cedc0daSAndrii Nakryiko 	 * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
1444cedc0daSAndrii Nakryiko 	 */
1454cedc0daSAndrii Nakryiko 	void *info;
1464cedc0daSAndrii Nakryiko 	__u32 rec_size;
1474cedc0daSAndrii Nakryiko 	__u32 len;
1484cedc0daSAndrii Nakryiko };
1494cedc0daSAndrii Nakryiko 
1504cedc0daSAndrii Nakryiko #define for_each_btf_ext_sec(seg, sec)					\
1514cedc0daSAndrii Nakryiko 	for (sec = (seg)->info;						\
1524cedc0daSAndrii Nakryiko 	     (void *)sec < (seg)->info + (seg)->len;			\
1534cedc0daSAndrii Nakryiko 	     sec = (void *)sec + sizeof(struct btf_ext_info_sec) +	\
1544cedc0daSAndrii Nakryiko 		   (seg)->rec_size * sec->num_info)
1554cedc0daSAndrii Nakryiko 
1564cedc0daSAndrii Nakryiko #define for_each_btf_ext_rec(seg, sec, i, rec)				\
1574cedc0daSAndrii Nakryiko 	for (i = 0, rec = (void *)&(sec)->data;				\
1584cedc0daSAndrii Nakryiko 	     i < (sec)->num_info;					\
1594cedc0daSAndrii Nakryiko 	     i++, rec = (void *)rec + (seg)->rec_size)
1604cedc0daSAndrii Nakryiko 
16128b93c64SAndrii Nakryiko /*
16228b93c64SAndrii Nakryiko  * The .BTF.ext ELF section layout defined as
16328b93c64SAndrii Nakryiko  *   struct btf_ext_header
16428b93c64SAndrii Nakryiko  *   func_info subsection
16528b93c64SAndrii Nakryiko  *
16628b93c64SAndrii Nakryiko  * The func_info subsection layout:
16728b93c64SAndrii Nakryiko  *   record size for struct bpf_func_info in the func_info subsection
16828b93c64SAndrii Nakryiko  *   struct btf_sec_func_info for section #1
16928b93c64SAndrii Nakryiko  *   a list of bpf_func_info records for section #1
17028b93c64SAndrii Nakryiko  *     where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
17128b93c64SAndrii Nakryiko  *     but may not be identical
17228b93c64SAndrii Nakryiko  *   struct btf_sec_func_info for section #2
17328b93c64SAndrii Nakryiko  *   a list of bpf_func_info records for section #2
17428b93c64SAndrii Nakryiko  *   ......
17528b93c64SAndrii Nakryiko  *
17628b93c64SAndrii Nakryiko  * Note that the bpf_func_info record size in .BTF.ext may not
17728b93c64SAndrii Nakryiko  * be the same as the one defined in include/uapi/linux/bpf.h.
17828b93c64SAndrii Nakryiko  * The loader should ensure that record_size meets minimum
17928b93c64SAndrii Nakryiko  * requirement and pass the record as is to the kernel. The
18028b93c64SAndrii Nakryiko  * kernel will handle the func_info properly based on its contents.
18128b93c64SAndrii Nakryiko  */
18228b93c64SAndrii Nakryiko struct btf_ext_header {
18328b93c64SAndrii Nakryiko 	__u16	magic;
18428b93c64SAndrii Nakryiko 	__u8	version;
18528b93c64SAndrii Nakryiko 	__u8	flags;
18628b93c64SAndrii Nakryiko 	__u32	hdr_len;
18728b93c64SAndrii Nakryiko 
18828b93c64SAndrii Nakryiko 	/* All offsets are in bytes relative to the end of this header */
18928b93c64SAndrii Nakryiko 	__u32	func_info_off;
19028b93c64SAndrii Nakryiko 	__u32	func_info_len;
19128b93c64SAndrii Nakryiko 	__u32	line_info_off;
19228b93c64SAndrii Nakryiko 	__u32	line_info_len;
19328b93c64SAndrii Nakryiko 
19428b93c64SAndrii Nakryiko 	/* optional part of .BTF.ext header */
19528b93c64SAndrii Nakryiko 	__u32	core_relo_off;
19628b93c64SAndrii Nakryiko 	__u32	core_relo_len;
19728b93c64SAndrii Nakryiko };
19828b93c64SAndrii Nakryiko 
1994cedc0daSAndrii Nakryiko struct btf_ext {
2004cedc0daSAndrii Nakryiko 	union {
2014cedc0daSAndrii Nakryiko 		struct btf_ext_header *hdr;
2024cedc0daSAndrii Nakryiko 		void *data;
2034cedc0daSAndrii Nakryiko 	};
2044cedc0daSAndrii Nakryiko 	struct btf_ext_info func_info;
2054cedc0daSAndrii Nakryiko 	struct btf_ext_info line_info;
20628b93c64SAndrii Nakryiko 	struct btf_ext_info core_relo_info;
2074cedc0daSAndrii Nakryiko 	__u32 data_size;
2084cedc0daSAndrii Nakryiko };
2094cedc0daSAndrii Nakryiko 
2104cedc0daSAndrii Nakryiko struct btf_ext_info_sec {
2114cedc0daSAndrii Nakryiko 	__u32	sec_name_off;
2124cedc0daSAndrii Nakryiko 	__u32	num_info;
2134cedc0daSAndrii Nakryiko 	/* Followed by num_info * record_size number of bytes */
214385bbf7bSGustavo A. R. Silva 	__u8	data[];
2154cedc0daSAndrii Nakryiko };
2164cedc0daSAndrii Nakryiko 
2174cedc0daSAndrii Nakryiko /* The minimum bpf_func_info checked by the loader */
2184cedc0daSAndrii Nakryiko struct bpf_func_info_min {
2194cedc0daSAndrii Nakryiko 	__u32   insn_off;
2204cedc0daSAndrii Nakryiko 	__u32   type_id;
2214cedc0daSAndrii Nakryiko };
2224cedc0daSAndrii Nakryiko 
2234cedc0daSAndrii Nakryiko /* The minimum bpf_line_info checked by the loader */
2244cedc0daSAndrii Nakryiko struct bpf_line_info_min {
2254cedc0daSAndrii Nakryiko 	__u32	insn_off;
2264cedc0daSAndrii Nakryiko 	__u32	file_name_off;
2274cedc0daSAndrii Nakryiko 	__u32	line_off;
2284cedc0daSAndrii Nakryiko 	__u32	line_col;
2294cedc0daSAndrii Nakryiko };
2304cedc0daSAndrii Nakryiko 
23128b93c64SAndrii Nakryiko /* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
23228b93c64SAndrii Nakryiko  * has to be adjusted by relocations.
233511bb008SAndrii Nakryiko  */
23428b93c64SAndrii Nakryiko enum bpf_core_relo_kind {
235511bb008SAndrii Nakryiko 	BPF_FIELD_BYTE_OFFSET = 0,	/* field byte offset */
23628b93c64SAndrii Nakryiko 	BPF_FIELD_BYTE_SIZE = 1,	/* field size in bytes */
237511bb008SAndrii Nakryiko 	BPF_FIELD_EXISTS = 2,		/* field existence in target kernel */
23828b93c64SAndrii Nakryiko 	BPF_FIELD_SIGNED = 3,		/* field signedness (0 - unsigned, 1 - signed) */
23928b93c64SAndrii Nakryiko 	BPF_FIELD_LSHIFT_U64 = 4,	/* bitfield-specific left bitshift */
24028b93c64SAndrii Nakryiko 	BPF_FIELD_RSHIFT_U64 = 5,	/* bitfield-specific right bitshift */
2413fc32f40SAndrii Nakryiko 	BPF_TYPE_ID_LOCAL = 6,		/* type ID in local BPF object */
2423fc32f40SAndrii Nakryiko 	BPF_TYPE_ID_TARGET = 7,		/* type ID in target kernel */
2433fc32f40SAndrii Nakryiko 	BPF_TYPE_EXISTS = 8,		/* type existence in target kernel */
2443fc32f40SAndrii Nakryiko 	BPF_TYPE_SIZE = 9,		/* type size in bytes */
245511bb008SAndrii Nakryiko };
246511bb008SAndrii Nakryiko 
24728b93c64SAndrii Nakryiko /* The minimum bpf_core_relo checked by the loader
2484cedc0daSAndrii Nakryiko  *
24928b93c64SAndrii Nakryiko  * CO-RE relocation captures the following data:
2504cedc0daSAndrii Nakryiko  * - insn_off - instruction offset (in bytes) within a BPF program that needs
251511bb008SAndrii Nakryiko  *   its insn->imm field to be relocated with actual field info;
2524cedc0daSAndrii Nakryiko  * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
25328b93c64SAndrii Nakryiko  *   type or field;
2544cedc0daSAndrii Nakryiko  * - access_str_off - offset into corresponding .BTF string section. String
25528b93c64SAndrii Nakryiko  *   interpretation depends on specific relocation kind:
25628b93c64SAndrii Nakryiko  *     - for field-based relocations, string encodes an accessed field using
25728b93c64SAndrii Nakryiko  *     a sequence of field and array indices, separated by colon (:). It's
25828b93c64SAndrii Nakryiko  *     conceptually very close to LLVM's getelementptr ([0]) instruction's
25928b93c64SAndrii Nakryiko  *     arguments for identifying offset to a field.
26028b93c64SAndrii Nakryiko  *     - for type-based relocations, strings is expected to be just "0";
26128b93c64SAndrii Nakryiko  *     - for enum value-based relocations, string contains an index of enum
26228b93c64SAndrii Nakryiko  *     value within its enum type;
2634cedc0daSAndrii Nakryiko  *
2644cedc0daSAndrii Nakryiko  * Example to provide a better feel.
2654cedc0daSAndrii Nakryiko  *
2664cedc0daSAndrii Nakryiko  *   struct sample {
2674cedc0daSAndrii Nakryiko  *       int a;
2684cedc0daSAndrii Nakryiko  *       struct {
2694cedc0daSAndrii Nakryiko  *           int b[10];
2704cedc0daSAndrii Nakryiko  *       };
2714cedc0daSAndrii Nakryiko  *   };
2724cedc0daSAndrii Nakryiko  *
2734cedc0daSAndrii Nakryiko  *   struct sample *s = ...;
2744cedc0daSAndrii Nakryiko  *   int x = &s->a;     // encoded as "0:0" (a is field #0)
2754cedc0daSAndrii Nakryiko  *   int y = &s->b[5];  // encoded as "0:1:0:5" (anon struct is field #1,
2764cedc0daSAndrii Nakryiko  *                      // b is field #0 inside anon struct, accessing elem #5)
2774cedc0daSAndrii Nakryiko  *   int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
2784cedc0daSAndrii Nakryiko  *
2794cedc0daSAndrii Nakryiko  * type_id for all relocs in this example  will capture BTF type id of
2804cedc0daSAndrii Nakryiko  * `struct sample`.
2814cedc0daSAndrii Nakryiko  *
2824cedc0daSAndrii Nakryiko  * Such relocation is emitted when using __builtin_preserve_access_index()
2834cedc0daSAndrii Nakryiko  * Clang built-in, passing expression that captures field address, e.g.:
2844cedc0daSAndrii Nakryiko  *
2854cedc0daSAndrii Nakryiko  * bpf_probe_read(&dst, sizeof(dst),
2864cedc0daSAndrii Nakryiko  *		  __builtin_preserve_access_index(&src->a.b.c));
2874cedc0daSAndrii Nakryiko  *
288511bb008SAndrii Nakryiko  * In this case Clang will emit field relocation recording necessary data to
2894cedc0daSAndrii Nakryiko  * be able to find offset of embedded `a.b.c` field within `src` struct.
2904cedc0daSAndrii Nakryiko  *
2914cedc0daSAndrii Nakryiko  *   [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
2924cedc0daSAndrii Nakryiko  */
29328b93c64SAndrii Nakryiko struct bpf_core_relo {
2944cedc0daSAndrii Nakryiko 	__u32   insn_off;
2954cedc0daSAndrii Nakryiko 	__u32   type_id;
2964cedc0daSAndrii Nakryiko 	__u32   access_str_off;
29728b93c64SAndrii Nakryiko 	enum bpf_core_relo_kind kind;
2984cedc0daSAndrii Nakryiko };
2994cedc0daSAndrii Nakryiko 
300d7c4b398SAndrii Nakryiko #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
301