1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker * Internal libbpf helpers.
5*f7c14bbaSAndroid Build Coastguard Worker *
6*f7c14bbaSAndroid Build Coastguard Worker * Copyright (c) 2019 Facebook
7*f7c14bbaSAndroid Build Coastguard Worker */
8*f7c14bbaSAndroid Build Coastguard Worker
9*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LIBBPF_LIBBPF_INTERNAL_H
10*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_LIBBPF_INTERNAL_H
11*f7c14bbaSAndroid Build Coastguard Worker
12*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include <limits.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <linux/err.h>
16*f7c14bbaSAndroid Build Coastguard Worker #include <fcntl.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
18*f7c14bbaSAndroid Build Coastguard Worker #include <sys/syscall.h>
19*f7c14bbaSAndroid Build Coastguard Worker #include <libelf.h>
20*f7c14bbaSAndroid Build Coastguard Worker #include "relo_core.h"
21*f7c14bbaSAndroid Build Coastguard Worker
22*f7c14bbaSAndroid Build Coastguard Worker /* Android's libc doesn't support AT_EACCESS in faccessat() implementation
23*f7c14bbaSAndroid Build Coastguard Worker * ([0]), and just returns -EINVAL even if file exists and is accessible.
24*f7c14bbaSAndroid Build Coastguard Worker * See [1] for issues caused by this.
25*f7c14bbaSAndroid Build Coastguard Worker *
26*f7c14bbaSAndroid Build Coastguard Worker * So just redefine it to 0 on Android.
27*f7c14bbaSAndroid Build Coastguard Worker *
28*f7c14bbaSAndroid Build Coastguard Worker * [0] https://android.googlesource.com/platform/bionic/+/refs/heads/android13-release/libc/bionic/faccessat.cpp#50
29*f7c14bbaSAndroid Build Coastguard Worker * [1] https://github.com/libbpf/libbpf-bootstrap/issues/250#issuecomment-1911324250
30*f7c14bbaSAndroid Build Coastguard Worker */
31*f7c14bbaSAndroid Build Coastguard Worker #ifdef __ANDROID__
32*f7c14bbaSAndroid Build Coastguard Worker #undef AT_EACCESS
33*f7c14bbaSAndroid Build Coastguard Worker #define AT_EACCESS 0
34*f7c14bbaSAndroid Build Coastguard Worker #endif
35*f7c14bbaSAndroid Build Coastguard Worker
36*f7c14bbaSAndroid Build Coastguard Worker /* make sure libbpf doesn't use kernel-only integer typedefs */
37*f7c14bbaSAndroid Build Coastguard Worker #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
38*f7c14bbaSAndroid Build Coastguard Worker
39*f7c14bbaSAndroid Build Coastguard Worker /* prevent accidental re-addition of reallocarray() */
40*f7c14bbaSAndroid Build Coastguard Worker #pragma GCC poison reallocarray
41*f7c14bbaSAndroid Build Coastguard Worker
42*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
43*f7c14bbaSAndroid Build Coastguard Worker #include "btf.h"
44*f7c14bbaSAndroid Build Coastguard Worker
45*f7c14bbaSAndroid Build Coastguard Worker #ifndef EM_BPF
46*f7c14bbaSAndroid Build Coastguard Worker #define EM_BPF 247
47*f7c14bbaSAndroid Build Coastguard Worker #endif
48*f7c14bbaSAndroid Build Coastguard Worker
49*f7c14bbaSAndroid Build Coastguard Worker #ifndef R_BPF_64_64
50*f7c14bbaSAndroid Build Coastguard Worker #define R_BPF_64_64 1
51*f7c14bbaSAndroid Build Coastguard Worker #endif
52*f7c14bbaSAndroid Build Coastguard Worker #ifndef R_BPF_64_ABS64
53*f7c14bbaSAndroid Build Coastguard Worker #define R_BPF_64_ABS64 2
54*f7c14bbaSAndroid Build Coastguard Worker #endif
55*f7c14bbaSAndroid Build Coastguard Worker #ifndef R_BPF_64_ABS32
56*f7c14bbaSAndroid Build Coastguard Worker #define R_BPF_64_ABS32 3
57*f7c14bbaSAndroid Build Coastguard Worker #endif
58*f7c14bbaSAndroid Build Coastguard Worker #ifndef R_BPF_64_32
59*f7c14bbaSAndroid Build Coastguard Worker #define R_BPF_64_32 10
60*f7c14bbaSAndroid Build Coastguard Worker #endif
61*f7c14bbaSAndroid Build Coastguard Worker
62*f7c14bbaSAndroid Build Coastguard Worker #ifndef SHT_LLVM_ADDRSIG
63*f7c14bbaSAndroid Build Coastguard Worker #define SHT_LLVM_ADDRSIG 0x6FFF4C03
64*f7c14bbaSAndroid Build Coastguard Worker #endif
65*f7c14bbaSAndroid Build Coastguard Worker
66*f7c14bbaSAndroid Build Coastguard Worker /* if libelf is old and doesn't support mmap(), fall back to read() */
67*f7c14bbaSAndroid Build Coastguard Worker #ifndef ELF_C_READ_MMAP
68*f7c14bbaSAndroid Build Coastguard Worker #define ELF_C_READ_MMAP ELF_C_READ
69*f7c14bbaSAndroid Build Coastguard Worker #endif
70*f7c14bbaSAndroid Build Coastguard Worker
71*f7c14bbaSAndroid Build Coastguard Worker /* Older libelf all end up in this expression, for both 32 and 64 bit */
72*f7c14bbaSAndroid Build Coastguard Worker #ifndef ELF64_ST_VISIBILITY
73*f7c14bbaSAndroid Build Coastguard Worker #define ELF64_ST_VISIBILITY(o) ((o) & 0x03)
74*f7c14bbaSAndroid Build Coastguard Worker #endif
75*f7c14bbaSAndroid Build Coastguard Worker
76*f7c14bbaSAndroid Build Coastguard Worker #define BTF_INFO_ENC(kind, kind_flag, vlen) \
77*f7c14bbaSAndroid Build Coastguard Worker ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
78*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
79*f7c14bbaSAndroid Build Coastguard Worker #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
80*f7c14bbaSAndroid Build Coastguard Worker ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
81*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
82*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
83*f7c14bbaSAndroid Build Coastguard Worker BTF_INT_ENC(encoding, bits_offset, bits)
84*f7c14bbaSAndroid Build Coastguard Worker #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
85*f7c14bbaSAndroid Build Coastguard Worker #define BTF_PARAM_ENC(name, type) (name), (type)
86*f7c14bbaSAndroid Build Coastguard Worker #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
87*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TYPE_FLOAT_ENC(name, sz) \
88*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
89*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TYPE_DECL_TAG_ENC(value, type, component_idx) \
90*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), type), (component_idx)
91*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TYPE_TYPE_TAG_ENC(value, type) \
92*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type)
93*f7c14bbaSAndroid Build Coastguard Worker
94*f7c14bbaSAndroid Build Coastguard Worker #ifndef likely
95*f7c14bbaSAndroid Build Coastguard Worker #define likely(x) __builtin_expect(!!(x), 1)
96*f7c14bbaSAndroid Build Coastguard Worker #endif
97*f7c14bbaSAndroid Build Coastguard Worker #ifndef unlikely
98*f7c14bbaSAndroid Build Coastguard Worker #define unlikely(x) __builtin_expect(!!(x), 0)
99*f7c14bbaSAndroid Build Coastguard Worker #endif
100*f7c14bbaSAndroid Build Coastguard Worker #ifndef min
101*f7c14bbaSAndroid Build Coastguard Worker # define min(x, y) ((x) < (y) ? (x) : (y))
102*f7c14bbaSAndroid Build Coastguard Worker #endif
103*f7c14bbaSAndroid Build Coastguard Worker #ifndef max
104*f7c14bbaSAndroid Build Coastguard Worker # define max(x, y) ((x) < (y) ? (y) : (x))
105*f7c14bbaSAndroid Build Coastguard Worker #endif
106*f7c14bbaSAndroid Build Coastguard Worker #ifndef offsetofend
107*f7c14bbaSAndroid Build Coastguard Worker # define offsetofend(TYPE, FIELD) \
108*f7c14bbaSAndroid Build Coastguard Worker (offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
109*f7c14bbaSAndroid Build Coastguard Worker #endif
110*f7c14bbaSAndroid Build Coastguard Worker #ifndef __alias
111*f7c14bbaSAndroid Build Coastguard Worker #define __alias(symbol) __attribute__((alias(#symbol)))
112*f7c14bbaSAndroid Build Coastguard Worker #endif
113*f7c14bbaSAndroid Build Coastguard Worker
114*f7c14bbaSAndroid Build Coastguard Worker /* Check whether a string `str` has prefix `pfx`, regardless if `pfx` is
115*f7c14bbaSAndroid Build Coastguard Worker * a string literal known at compilation time or char * pointer known only at
116*f7c14bbaSAndroid Build Coastguard Worker * runtime.
117*f7c14bbaSAndroid Build Coastguard Worker */
118*f7c14bbaSAndroid Build Coastguard Worker #define str_has_pfx(str, pfx) \
119*f7c14bbaSAndroid Build Coastguard Worker (strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
120*f7c14bbaSAndroid Build Coastguard Worker
121*f7c14bbaSAndroid Build Coastguard Worker /* suffix check */
str_has_sfx(const char * str,const char * sfx)122*f7c14bbaSAndroid Build Coastguard Worker static inline bool str_has_sfx(const char *str, const char *sfx)
123*f7c14bbaSAndroid Build Coastguard Worker {
124*f7c14bbaSAndroid Build Coastguard Worker size_t str_len = strlen(str);
125*f7c14bbaSAndroid Build Coastguard Worker size_t sfx_len = strlen(sfx);
126*f7c14bbaSAndroid Build Coastguard Worker
127*f7c14bbaSAndroid Build Coastguard Worker if (sfx_len > str_len)
128*f7c14bbaSAndroid Build Coastguard Worker return false;
129*f7c14bbaSAndroid Build Coastguard Worker return strcmp(str + str_len - sfx_len, sfx) == 0;
130*f7c14bbaSAndroid Build Coastguard Worker }
131*f7c14bbaSAndroid Build Coastguard Worker
132*f7c14bbaSAndroid Build Coastguard Worker /* Symbol versioning is different between static and shared library.
133*f7c14bbaSAndroid Build Coastguard Worker * Properly versioned symbols are needed for shared library, but
134*f7c14bbaSAndroid Build Coastguard Worker * only the symbol of the new version is needed for static library.
135*f7c14bbaSAndroid Build Coastguard Worker * Starting with GNU C 10, use symver attribute instead of .symver assembler
136*f7c14bbaSAndroid Build Coastguard Worker * directive, which works better with GCC LTO builds.
137*f7c14bbaSAndroid Build Coastguard Worker */
138*f7c14bbaSAndroid Build Coastguard Worker #if defined(SHARED) && defined(__GNUC__) && __GNUC__ >= 10
139*f7c14bbaSAndroid Build Coastguard Worker
140*f7c14bbaSAndroid Build Coastguard Worker #define DEFAULT_VERSION(internal_name, api_name, version) \
141*f7c14bbaSAndroid Build Coastguard Worker __attribute__((symver(#api_name "@@" #version)))
142*f7c14bbaSAndroid Build Coastguard Worker #define COMPAT_VERSION(internal_name, api_name, version) \
143*f7c14bbaSAndroid Build Coastguard Worker __attribute__((symver(#api_name "@" #version)))
144*f7c14bbaSAndroid Build Coastguard Worker
145*f7c14bbaSAndroid Build Coastguard Worker #elif defined(SHARED)
146*f7c14bbaSAndroid Build Coastguard Worker
147*f7c14bbaSAndroid Build Coastguard Worker #define COMPAT_VERSION(internal_name, api_name, version) \
148*f7c14bbaSAndroid Build Coastguard Worker asm(".symver " #internal_name "," #api_name "@" #version);
149*f7c14bbaSAndroid Build Coastguard Worker #define DEFAULT_VERSION(internal_name, api_name, version) \
150*f7c14bbaSAndroid Build Coastguard Worker asm(".symver " #internal_name "," #api_name "@@" #version);
151*f7c14bbaSAndroid Build Coastguard Worker
152*f7c14bbaSAndroid Build Coastguard Worker #else /* !SHARED */
153*f7c14bbaSAndroid Build Coastguard Worker
154*f7c14bbaSAndroid Build Coastguard Worker #define COMPAT_VERSION(internal_name, api_name, version)
155*f7c14bbaSAndroid Build Coastguard Worker #define DEFAULT_VERSION(internal_name, api_name, version) \
156*f7c14bbaSAndroid Build Coastguard Worker extern typeof(internal_name) api_name \
157*f7c14bbaSAndroid Build Coastguard Worker __attribute__((alias(#internal_name)));
158*f7c14bbaSAndroid Build Coastguard Worker
159*f7c14bbaSAndroid Build Coastguard Worker #endif
160*f7c14bbaSAndroid Build Coastguard Worker
161*f7c14bbaSAndroid Build Coastguard Worker extern void libbpf_print(enum libbpf_print_level level,
162*f7c14bbaSAndroid Build Coastguard Worker const char *format, ...)
163*f7c14bbaSAndroid Build Coastguard Worker __attribute__((format(printf, 2, 3)));
164*f7c14bbaSAndroid Build Coastguard Worker
165*f7c14bbaSAndroid Build Coastguard Worker #define __pr(level, fmt, ...) \
166*f7c14bbaSAndroid Build Coastguard Worker do { \
167*f7c14bbaSAndroid Build Coastguard Worker libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \
168*f7c14bbaSAndroid Build Coastguard Worker } while (0)
169*f7c14bbaSAndroid Build Coastguard Worker
170*f7c14bbaSAndroid Build Coastguard Worker #define pr_warn(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
171*f7c14bbaSAndroid Build Coastguard Worker #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
172*f7c14bbaSAndroid Build Coastguard Worker #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
173*f7c14bbaSAndroid Build Coastguard Worker
174*f7c14bbaSAndroid Build Coastguard Worker #ifndef __has_builtin
175*f7c14bbaSAndroid Build Coastguard Worker #define __has_builtin(x) 0
176*f7c14bbaSAndroid Build Coastguard Worker #endif
177*f7c14bbaSAndroid Build Coastguard Worker
178*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link {
179*f7c14bbaSAndroid Build Coastguard Worker int (*detach)(struct bpf_link *link);
180*f7c14bbaSAndroid Build Coastguard Worker void (*dealloc)(struct bpf_link *link);
181*f7c14bbaSAndroid Build Coastguard Worker char *pin_path; /* NULL, if not pinned */
182*f7c14bbaSAndroid Build Coastguard Worker int fd; /* hook FD, -1 if not applicable */
183*f7c14bbaSAndroid Build Coastguard Worker bool disconnected;
184*f7c14bbaSAndroid Build Coastguard Worker };
185*f7c14bbaSAndroid Build Coastguard Worker
186*f7c14bbaSAndroid Build Coastguard Worker /*
187*f7c14bbaSAndroid Build Coastguard Worker * Re-implement glibc's reallocarray() for libbpf internal-only use.
188*f7c14bbaSAndroid Build Coastguard Worker * reallocarray(), unfortunately, is not available in all versions of glibc,
189*f7c14bbaSAndroid Build Coastguard Worker * so requires extra feature detection and using reallocarray() stub from
190*f7c14bbaSAndroid Build Coastguard Worker * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
191*f7c14bbaSAndroid Build Coastguard Worker * build of libbpf unnecessarily and is just a maintenance burden. Instead,
192*f7c14bbaSAndroid Build Coastguard Worker * it's trivial to implement libbpf-specific internal version and use it
193*f7c14bbaSAndroid Build Coastguard Worker * throughout libbpf.
194*f7c14bbaSAndroid Build Coastguard Worker */
libbpf_reallocarray(void * ptr,size_t nmemb,size_t size)195*f7c14bbaSAndroid Build Coastguard Worker static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
196*f7c14bbaSAndroid Build Coastguard Worker {
197*f7c14bbaSAndroid Build Coastguard Worker size_t total;
198*f7c14bbaSAndroid Build Coastguard Worker
199*f7c14bbaSAndroid Build Coastguard Worker #if __has_builtin(__builtin_mul_overflow)
200*f7c14bbaSAndroid Build Coastguard Worker if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
201*f7c14bbaSAndroid Build Coastguard Worker return NULL;
202*f7c14bbaSAndroid Build Coastguard Worker #else
203*f7c14bbaSAndroid Build Coastguard Worker if (size == 0 || nmemb > ULONG_MAX / size)
204*f7c14bbaSAndroid Build Coastguard Worker return NULL;
205*f7c14bbaSAndroid Build Coastguard Worker total = nmemb * size;
206*f7c14bbaSAndroid Build Coastguard Worker #endif
207*f7c14bbaSAndroid Build Coastguard Worker return realloc(ptr, total);
208*f7c14bbaSAndroid Build Coastguard Worker }
209*f7c14bbaSAndroid Build Coastguard Worker
210*f7c14bbaSAndroid Build Coastguard Worker /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
211*f7c14bbaSAndroid Build Coastguard Worker * is zero-terminated string no matter what (unless sz == 0, in which case
212*f7c14bbaSAndroid Build Coastguard Worker * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
213*f7c14bbaSAndroid Build Coastguard Worker * in what is returned. Given this is internal helper, it's trivial to extend
214*f7c14bbaSAndroid Build Coastguard Worker * this, when necessary. Use this instead of strncpy inside libbpf source code.
215*f7c14bbaSAndroid Build Coastguard Worker */
libbpf_strlcpy(char * dst,const char * src,size_t sz)216*f7c14bbaSAndroid Build Coastguard Worker static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
217*f7c14bbaSAndroid Build Coastguard Worker {
218*f7c14bbaSAndroid Build Coastguard Worker size_t i;
219*f7c14bbaSAndroid Build Coastguard Worker
220*f7c14bbaSAndroid Build Coastguard Worker if (sz == 0)
221*f7c14bbaSAndroid Build Coastguard Worker return;
222*f7c14bbaSAndroid Build Coastguard Worker
223*f7c14bbaSAndroid Build Coastguard Worker sz--;
224*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < sz && src[i]; i++)
225*f7c14bbaSAndroid Build Coastguard Worker dst[i] = src[i];
226*f7c14bbaSAndroid Build Coastguard Worker dst[i] = '\0';
227*f7c14bbaSAndroid Build Coastguard Worker }
228*f7c14bbaSAndroid Build Coastguard Worker
229*f7c14bbaSAndroid Build Coastguard Worker __u32 get_kernel_version(void);
230*f7c14bbaSAndroid Build Coastguard Worker
231*f7c14bbaSAndroid Build Coastguard Worker struct btf;
232*f7c14bbaSAndroid Build Coastguard Worker struct btf_type;
233*f7c14bbaSAndroid Build Coastguard Worker
234*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id);
235*f7c14bbaSAndroid Build Coastguard Worker const char *btf_kind_str(const struct btf_type *t);
236*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
237*f7c14bbaSAndroid Build Coastguard Worker
btf_func_linkage(const struct btf_type * t)238*f7c14bbaSAndroid Build Coastguard Worker static inline enum btf_func_linkage btf_func_linkage(const struct btf_type *t)
239*f7c14bbaSAndroid Build Coastguard Worker {
240*f7c14bbaSAndroid Build Coastguard Worker return (enum btf_func_linkage)(int)btf_vlen(t);
241*f7c14bbaSAndroid Build Coastguard Worker }
242*f7c14bbaSAndroid Build Coastguard Worker
btf_type_info(int kind,int vlen,int kflag)243*f7c14bbaSAndroid Build Coastguard Worker static inline __u32 btf_type_info(int kind, int vlen, int kflag)
244*f7c14bbaSAndroid Build Coastguard Worker {
245*f7c14bbaSAndroid Build Coastguard Worker return (kflag << 31) | (kind << 24) | vlen;
246*f7c14bbaSAndroid Build Coastguard Worker }
247*f7c14bbaSAndroid Build Coastguard Worker
248*f7c14bbaSAndroid Build Coastguard Worker enum map_def_parts {
249*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_MAP_TYPE = 0x001,
250*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_KEY_TYPE = 0x002,
251*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_KEY_SIZE = 0x004,
252*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_VALUE_TYPE = 0x008,
253*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_VALUE_SIZE = 0x010,
254*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_MAX_ENTRIES = 0x020,
255*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_MAP_FLAGS = 0x040,
256*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_NUMA_NODE = 0x080,
257*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_PINNING = 0x100,
258*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_INNER_MAP = 0x200,
259*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_MAP_EXTRA = 0x400,
260*f7c14bbaSAndroid Build Coastguard Worker
261*f7c14bbaSAndroid Build Coastguard Worker MAP_DEF_ALL = 0x7ff, /* combination of all above */
262*f7c14bbaSAndroid Build Coastguard Worker };
263*f7c14bbaSAndroid Build Coastguard Worker
264*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def {
265*f7c14bbaSAndroid Build Coastguard Worker enum map_def_parts parts;
266*f7c14bbaSAndroid Build Coastguard Worker __u32 map_type;
267*f7c14bbaSAndroid Build Coastguard Worker __u32 key_type_id;
268*f7c14bbaSAndroid Build Coastguard Worker __u32 key_size;
269*f7c14bbaSAndroid Build Coastguard Worker __u32 value_type_id;
270*f7c14bbaSAndroid Build Coastguard Worker __u32 value_size;
271*f7c14bbaSAndroid Build Coastguard Worker __u32 max_entries;
272*f7c14bbaSAndroid Build Coastguard Worker __u32 map_flags;
273*f7c14bbaSAndroid Build Coastguard Worker __u32 numa_node;
274*f7c14bbaSAndroid Build Coastguard Worker __u32 pinning;
275*f7c14bbaSAndroid Build Coastguard Worker __u64 map_extra;
276*f7c14bbaSAndroid Build Coastguard Worker };
277*f7c14bbaSAndroid Build Coastguard Worker
278*f7c14bbaSAndroid Build Coastguard Worker int parse_btf_map_def(const char *map_name, struct btf *btf,
279*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *def_t, bool strict,
280*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def *map_def, struct btf_map_def *inner_def);
281*f7c14bbaSAndroid Build Coastguard Worker
282*f7c14bbaSAndroid Build Coastguard Worker void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
283*f7c14bbaSAndroid Build Coastguard Worker size_t cur_cnt, size_t max_cnt, size_t add_cnt);
284*f7c14bbaSAndroid Build Coastguard Worker int libbpf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt);
285*f7c14bbaSAndroid Build Coastguard Worker
libbpf_is_mem_zeroed(const char * p,ssize_t len)286*f7c14bbaSAndroid Build Coastguard Worker static inline bool libbpf_is_mem_zeroed(const char *p, ssize_t len)
287*f7c14bbaSAndroid Build Coastguard Worker {
288*f7c14bbaSAndroid Build Coastguard Worker while (len > 0) {
289*f7c14bbaSAndroid Build Coastguard Worker if (*p)
290*f7c14bbaSAndroid Build Coastguard Worker return false;
291*f7c14bbaSAndroid Build Coastguard Worker p++;
292*f7c14bbaSAndroid Build Coastguard Worker len--;
293*f7c14bbaSAndroid Build Coastguard Worker }
294*f7c14bbaSAndroid Build Coastguard Worker return true;
295*f7c14bbaSAndroid Build Coastguard Worker }
296*f7c14bbaSAndroid Build Coastguard Worker
libbpf_validate_opts(const char * opts,size_t opts_sz,size_t user_sz,const char * type_name)297*f7c14bbaSAndroid Build Coastguard Worker static inline bool libbpf_validate_opts(const char *opts,
298*f7c14bbaSAndroid Build Coastguard Worker size_t opts_sz, size_t user_sz,
299*f7c14bbaSAndroid Build Coastguard Worker const char *type_name)
300*f7c14bbaSAndroid Build Coastguard Worker {
301*f7c14bbaSAndroid Build Coastguard Worker if (user_sz < sizeof(size_t)) {
302*f7c14bbaSAndroid Build Coastguard Worker pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
303*f7c14bbaSAndroid Build Coastguard Worker return false;
304*f7c14bbaSAndroid Build Coastguard Worker }
305*f7c14bbaSAndroid Build Coastguard Worker if (!libbpf_is_mem_zeroed(opts + opts_sz, (ssize_t)user_sz - opts_sz)) {
306*f7c14bbaSAndroid Build Coastguard Worker pr_warn("%s has non-zero extra bytes\n", type_name);
307*f7c14bbaSAndroid Build Coastguard Worker return false;
308*f7c14bbaSAndroid Build Coastguard Worker }
309*f7c14bbaSAndroid Build Coastguard Worker return true;
310*f7c14bbaSAndroid Build Coastguard Worker }
311*f7c14bbaSAndroid Build Coastguard Worker
312*f7c14bbaSAndroid Build Coastguard Worker #define OPTS_VALID(opts, type) \
313*f7c14bbaSAndroid Build Coastguard Worker (!(opts) || libbpf_validate_opts((const char *)opts, \
314*f7c14bbaSAndroid Build Coastguard Worker offsetofend(struct type, \
315*f7c14bbaSAndroid Build Coastguard Worker type##__last_field), \
316*f7c14bbaSAndroid Build Coastguard Worker (opts)->sz, #type))
317*f7c14bbaSAndroid Build Coastguard Worker #define OPTS_HAS(opts, field) \
318*f7c14bbaSAndroid Build Coastguard Worker ((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
319*f7c14bbaSAndroid Build Coastguard Worker #define OPTS_GET(opts, field, fallback_value) \
320*f7c14bbaSAndroid Build Coastguard Worker (OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
321*f7c14bbaSAndroid Build Coastguard Worker #define OPTS_SET(opts, field, value) \
322*f7c14bbaSAndroid Build Coastguard Worker do { \
323*f7c14bbaSAndroid Build Coastguard Worker if (OPTS_HAS(opts, field)) \
324*f7c14bbaSAndroid Build Coastguard Worker (opts)->field = value; \
325*f7c14bbaSAndroid Build Coastguard Worker } while (0)
326*f7c14bbaSAndroid Build Coastguard Worker
327*f7c14bbaSAndroid Build Coastguard Worker #define OPTS_ZEROED(opts, last_nonzero_field) \
328*f7c14bbaSAndroid Build Coastguard Worker ({ \
329*f7c14bbaSAndroid Build Coastguard Worker ssize_t __off = offsetofend(typeof(*(opts)), last_nonzero_field); \
330*f7c14bbaSAndroid Build Coastguard Worker !(opts) || libbpf_is_mem_zeroed((const void *)opts + __off, \
331*f7c14bbaSAndroid Build Coastguard Worker (opts)->sz - __off); \
332*f7c14bbaSAndroid Build Coastguard Worker })
333*f7c14bbaSAndroid Build Coastguard Worker
334*f7c14bbaSAndroid Build Coastguard Worker enum kern_feature_id {
335*f7c14bbaSAndroid Build Coastguard Worker /* v4.14: kernel support for program & map names. */
336*f7c14bbaSAndroid Build Coastguard Worker FEAT_PROG_NAME,
337*f7c14bbaSAndroid Build Coastguard Worker /* v5.2: kernel support for global data sections. */
338*f7c14bbaSAndroid Build Coastguard Worker FEAT_GLOBAL_DATA,
339*f7c14bbaSAndroid Build Coastguard Worker /* BTF support */
340*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF,
341*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
342*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_FUNC,
343*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
344*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_DATASEC,
345*f7c14bbaSAndroid Build Coastguard Worker /* BTF_FUNC_GLOBAL is supported */
346*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_GLOBAL_FUNC,
347*f7c14bbaSAndroid Build Coastguard Worker /* BPF_F_MMAPABLE is supported for arrays */
348*f7c14bbaSAndroid Build Coastguard Worker FEAT_ARRAY_MMAP,
349*f7c14bbaSAndroid Build Coastguard Worker /* kernel support for expected_attach_type in BPF_PROG_LOAD */
350*f7c14bbaSAndroid Build Coastguard Worker FEAT_EXP_ATTACH_TYPE,
351*f7c14bbaSAndroid Build Coastguard Worker /* bpf_probe_read_{kernel,user}[_str] helpers */
352*f7c14bbaSAndroid Build Coastguard Worker FEAT_PROBE_READ_KERN,
353*f7c14bbaSAndroid Build Coastguard Worker /* BPF_PROG_BIND_MAP is supported */
354*f7c14bbaSAndroid Build Coastguard Worker FEAT_PROG_BIND_MAP,
355*f7c14bbaSAndroid Build Coastguard Worker /* Kernel support for module BTFs */
356*f7c14bbaSAndroid Build Coastguard Worker FEAT_MODULE_BTF,
357*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_FLOAT support */
358*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_FLOAT,
359*f7c14bbaSAndroid Build Coastguard Worker /* BPF perf link support */
360*f7c14bbaSAndroid Build Coastguard Worker FEAT_PERF_LINK,
361*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_DECL_TAG support */
362*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_DECL_TAG,
363*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_TYPE_TAG support */
364*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_TYPE_TAG,
365*f7c14bbaSAndroid Build Coastguard Worker /* memcg-based accounting for BPF maps and progs */
366*f7c14bbaSAndroid Build Coastguard Worker FEAT_MEMCG_ACCOUNT,
367*f7c14bbaSAndroid Build Coastguard Worker /* BPF cookie (bpf_get_attach_cookie() BPF helper) support */
368*f7c14bbaSAndroid Build Coastguard Worker FEAT_BPF_COOKIE,
369*f7c14bbaSAndroid Build Coastguard Worker /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
370*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_ENUM64,
371*f7c14bbaSAndroid Build Coastguard Worker /* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */
372*f7c14bbaSAndroid Build Coastguard Worker FEAT_SYSCALL_WRAPPER,
373*f7c14bbaSAndroid Build Coastguard Worker /* BPF multi-uprobe link support */
374*f7c14bbaSAndroid Build Coastguard Worker FEAT_UPROBE_MULTI_LINK,
375*f7c14bbaSAndroid Build Coastguard Worker /* Kernel supports arg:ctx tag (__arg_ctx) for global subprogs natively */
376*f7c14bbaSAndroid Build Coastguard Worker FEAT_ARG_CTX_TAG,
377*f7c14bbaSAndroid Build Coastguard Worker /* Kernel supports '?' at the front of datasec names */
378*f7c14bbaSAndroid Build Coastguard Worker FEAT_BTF_QMARK_DATASEC,
379*f7c14bbaSAndroid Build Coastguard Worker __FEAT_CNT,
380*f7c14bbaSAndroid Build Coastguard Worker };
381*f7c14bbaSAndroid Build Coastguard Worker
382*f7c14bbaSAndroid Build Coastguard Worker enum kern_feature_result {
383*f7c14bbaSAndroid Build Coastguard Worker FEAT_UNKNOWN = 0,
384*f7c14bbaSAndroid Build Coastguard Worker FEAT_SUPPORTED = 1,
385*f7c14bbaSAndroid Build Coastguard Worker FEAT_MISSING = 2,
386*f7c14bbaSAndroid Build Coastguard Worker };
387*f7c14bbaSAndroid Build Coastguard Worker
388*f7c14bbaSAndroid Build Coastguard Worker struct kern_feature_cache {
389*f7c14bbaSAndroid Build Coastguard Worker enum kern_feature_result res[__FEAT_CNT];
390*f7c14bbaSAndroid Build Coastguard Worker int token_fd;
391*f7c14bbaSAndroid Build Coastguard Worker };
392*f7c14bbaSAndroid Build Coastguard Worker
393*f7c14bbaSAndroid Build Coastguard Worker bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id);
394*f7c14bbaSAndroid Build Coastguard Worker bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id);
395*f7c14bbaSAndroid Build Coastguard Worker
396*f7c14bbaSAndroid Build Coastguard Worker int probe_kern_syscall_wrapper(int token_fd);
397*f7c14bbaSAndroid Build Coastguard Worker int probe_memcg_account(int token_fd);
398*f7c14bbaSAndroid Build Coastguard Worker int bump_rlimit_memlock(void);
399*f7c14bbaSAndroid Build Coastguard Worker
400*f7c14bbaSAndroid Build Coastguard Worker int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
401*f7c14bbaSAndroid Build Coastguard Worker int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
402*f7c14bbaSAndroid Build Coastguard Worker int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
403*f7c14bbaSAndroid Build Coastguard Worker const char *str_sec, size_t str_len,
404*f7c14bbaSAndroid Build Coastguard Worker int token_fd);
405*f7c14bbaSAndroid Build Coastguard Worker int btf_load_into_kernel(struct btf *btf,
406*f7c14bbaSAndroid Build Coastguard Worker char *log_buf, size_t log_sz, __u32 log_level,
407*f7c14bbaSAndroid Build Coastguard Worker int token_fd);
408*f7c14bbaSAndroid Build Coastguard Worker
409*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
410*f7c14bbaSAndroid Build Coastguard Worker void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
411*f7c14bbaSAndroid Build Coastguard Worker const char **prefix, int *kind);
412*f7c14bbaSAndroid Build Coastguard Worker
413*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info {
414*f7c14bbaSAndroid Build Coastguard Worker /*
415*f7c14bbaSAndroid Build Coastguard Worker * info points to the individual info section (e.g. func_info and
416*f7c14bbaSAndroid Build Coastguard Worker * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
417*f7c14bbaSAndroid Build Coastguard Worker */
418*f7c14bbaSAndroid Build Coastguard Worker void *info;
419*f7c14bbaSAndroid Build Coastguard Worker __u32 rec_size;
420*f7c14bbaSAndroid Build Coastguard Worker __u32 len;
421*f7c14bbaSAndroid Build Coastguard Worker /* optional (maintained internally by libbpf) mapping between .BTF.ext
422*f7c14bbaSAndroid Build Coastguard Worker * section and corresponding ELF section. This is used to join
423*f7c14bbaSAndroid Build Coastguard Worker * information like CO-RE relocation records with corresponding BPF
424*f7c14bbaSAndroid Build Coastguard Worker * programs defined in ELF sections
425*f7c14bbaSAndroid Build Coastguard Worker */
426*f7c14bbaSAndroid Build Coastguard Worker __u32 *sec_idxs;
427*f7c14bbaSAndroid Build Coastguard Worker int sec_cnt;
428*f7c14bbaSAndroid Build Coastguard Worker };
429*f7c14bbaSAndroid Build Coastguard Worker
430*f7c14bbaSAndroid Build Coastguard Worker #define for_each_btf_ext_sec(seg, sec) \
431*f7c14bbaSAndroid Build Coastguard Worker for (sec = (seg)->info; \
432*f7c14bbaSAndroid Build Coastguard Worker (void *)sec < (seg)->info + (seg)->len; \
433*f7c14bbaSAndroid Build Coastguard Worker sec = (void *)sec + sizeof(struct btf_ext_info_sec) + \
434*f7c14bbaSAndroid Build Coastguard Worker (seg)->rec_size * sec->num_info)
435*f7c14bbaSAndroid Build Coastguard Worker
436*f7c14bbaSAndroid Build Coastguard Worker #define for_each_btf_ext_rec(seg, sec, i, rec) \
437*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, rec = (void *)&(sec)->data; \
438*f7c14bbaSAndroid Build Coastguard Worker i < (sec)->num_info; \
439*f7c14bbaSAndroid Build Coastguard Worker i++, rec = (void *)rec + (seg)->rec_size)
440*f7c14bbaSAndroid Build Coastguard Worker
441*f7c14bbaSAndroid Build Coastguard Worker /*
442*f7c14bbaSAndroid Build Coastguard Worker * The .BTF.ext ELF section layout defined as
443*f7c14bbaSAndroid Build Coastguard Worker * struct btf_ext_header
444*f7c14bbaSAndroid Build Coastguard Worker * func_info subsection
445*f7c14bbaSAndroid Build Coastguard Worker *
446*f7c14bbaSAndroid Build Coastguard Worker * The func_info subsection layout:
447*f7c14bbaSAndroid Build Coastguard Worker * record size for struct bpf_func_info in the func_info subsection
448*f7c14bbaSAndroid Build Coastguard Worker * struct btf_sec_func_info for section #1
449*f7c14bbaSAndroid Build Coastguard Worker * a list of bpf_func_info records for section #1
450*f7c14bbaSAndroid Build Coastguard Worker * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
451*f7c14bbaSAndroid Build Coastguard Worker * but may not be identical
452*f7c14bbaSAndroid Build Coastguard Worker * struct btf_sec_func_info for section #2
453*f7c14bbaSAndroid Build Coastguard Worker * a list of bpf_func_info records for section #2
454*f7c14bbaSAndroid Build Coastguard Worker * ......
455*f7c14bbaSAndroid Build Coastguard Worker *
456*f7c14bbaSAndroid Build Coastguard Worker * Note that the bpf_func_info record size in .BTF.ext may not
457*f7c14bbaSAndroid Build Coastguard Worker * be the same as the one defined in include/uapi/linux/bpf.h.
458*f7c14bbaSAndroid Build Coastguard Worker * The loader should ensure that record_size meets minimum
459*f7c14bbaSAndroid Build Coastguard Worker * requirement and pass the record as is to the kernel. The
460*f7c14bbaSAndroid Build Coastguard Worker * kernel will handle the func_info properly based on its contents.
461*f7c14bbaSAndroid Build Coastguard Worker */
462*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_header {
463*f7c14bbaSAndroid Build Coastguard Worker __u16 magic;
464*f7c14bbaSAndroid Build Coastguard Worker __u8 version;
465*f7c14bbaSAndroid Build Coastguard Worker __u8 flags;
466*f7c14bbaSAndroid Build Coastguard Worker __u32 hdr_len;
467*f7c14bbaSAndroid Build Coastguard Worker
468*f7c14bbaSAndroid Build Coastguard Worker /* All offsets are in bytes relative to the end of this header */
469*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_off;
470*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_len;
471*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_off;
472*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_len;
473*f7c14bbaSAndroid Build Coastguard Worker
474*f7c14bbaSAndroid Build Coastguard Worker /* optional part of .BTF.ext header */
475*f7c14bbaSAndroid Build Coastguard Worker __u32 core_relo_off;
476*f7c14bbaSAndroid Build Coastguard Worker __u32 core_relo_len;
477*f7c14bbaSAndroid Build Coastguard Worker };
478*f7c14bbaSAndroid Build Coastguard Worker
479*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext {
480*f7c14bbaSAndroid Build Coastguard Worker union {
481*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_header *hdr;
482*f7c14bbaSAndroid Build Coastguard Worker void *data;
483*f7c14bbaSAndroid Build Coastguard Worker };
484*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info func_info;
485*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info line_info;
486*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info core_relo_info;
487*f7c14bbaSAndroid Build Coastguard Worker __u32 data_size;
488*f7c14bbaSAndroid Build Coastguard Worker };
489*f7c14bbaSAndroid Build Coastguard Worker
490*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info_sec {
491*f7c14bbaSAndroid Build Coastguard Worker __u32 sec_name_off;
492*f7c14bbaSAndroid Build Coastguard Worker __u32 num_info;
493*f7c14bbaSAndroid Build Coastguard Worker /* Followed by num_info * record_size number of bytes */
494*f7c14bbaSAndroid Build Coastguard Worker __u8 data[];
495*f7c14bbaSAndroid Build Coastguard Worker };
496*f7c14bbaSAndroid Build Coastguard Worker
497*f7c14bbaSAndroid Build Coastguard Worker /* The minimum bpf_func_info checked by the loader */
498*f7c14bbaSAndroid Build Coastguard Worker struct bpf_func_info_min {
499*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_off;
500*f7c14bbaSAndroid Build Coastguard Worker __u32 type_id;
501*f7c14bbaSAndroid Build Coastguard Worker };
502*f7c14bbaSAndroid Build Coastguard Worker
503*f7c14bbaSAndroid Build Coastguard Worker /* The minimum bpf_line_info checked by the loader */
504*f7c14bbaSAndroid Build Coastguard Worker struct bpf_line_info_min {
505*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_off;
506*f7c14bbaSAndroid Build Coastguard Worker __u32 file_name_off;
507*f7c14bbaSAndroid Build Coastguard Worker __u32 line_off;
508*f7c14bbaSAndroid Build Coastguard Worker __u32 line_col;
509*f7c14bbaSAndroid Build Coastguard Worker };
510*f7c14bbaSAndroid Build Coastguard Worker
511*f7c14bbaSAndroid Build Coastguard Worker
512*f7c14bbaSAndroid Build Coastguard Worker typedef int (*type_id_visit_fn)(__u32 *type_id, void *ctx);
513*f7c14bbaSAndroid Build Coastguard Worker typedef int (*str_off_visit_fn)(__u32 *str_off, void *ctx);
514*f7c14bbaSAndroid Build Coastguard Worker int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx);
515*f7c14bbaSAndroid Build Coastguard Worker int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ctx);
516*f7c14bbaSAndroid Build Coastguard Worker int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx);
517*f7c14bbaSAndroid Build Coastguard Worker int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx);
518*f7c14bbaSAndroid Build Coastguard Worker __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name,
519*f7c14bbaSAndroid Build Coastguard Worker __u32 kind);
520*f7c14bbaSAndroid Build Coastguard Worker
521*f7c14bbaSAndroid Build Coastguard Worker typedef int (*kallsyms_cb_t)(unsigned long long sym_addr, char sym_type,
522*f7c14bbaSAndroid Build Coastguard Worker const char *sym_name, void *ctx);
523*f7c14bbaSAndroid Build Coastguard Worker
524*f7c14bbaSAndroid Build Coastguard Worker int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *arg);
525*f7c14bbaSAndroid Build Coastguard Worker
526*f7c14bbaSAndroid Build Coastguard Worker /* handle direct returned errors */
libbpf_err(int ret)527*f7c14bbaSAndroid Build Coastguard Worker static inline int libbpf_err(int ret)
528*f7c14bbaSAndroid Build Coastguard Worker {
529*f7c14bbaSAndroid Build Coastguard Worker if (ret < 0)
530*f7c14bbaSAndroid Build Coastguard Worker errno = -ret;
531*f7c14bbaSAndroid Build Coastguard Worker return ret;
532*f7c14bbaSAndroid Build Coastguard Worker }
533*f7c14bbaSAndroid Build Coastguard Worker
534*f7c14bbaSAndroid Build Coastguard Worker /* handle errno-based (e.g., syscall or libc) errors according to libbpf's
535*f7c14bbaSAndroid Build Coastguard Worker * strict mode settings
536*f7c14bbaSAndroid Build Coastguard Worker */
libbpf_err_errno(int ret)537*f7c14bbaSAndroid Build Coastguard Worker static inline int libbpf_err_errno(int ret)
538*f7c14bbaSAndroid Build Coastguard Worker {
539*f7c14bbaSAndroid Build Coastguard Worker /* errno is already assumed to be set on error */
540*f7c14bbaSAndroid Build Coastguard Worker return ret < 0 ? -errno : ret;
541*f7c14bbaSAndroid Build Coastguard Worker }
542*f7c14bbaSAndroid Build Coastguard Worker
543*f7c14bbaSAndroid Build Coastguard Worker /* handle error for pointer-returning APIs, err is assumed to be < 0 always */
libbpf_err_ptr(int err)544*f7c14bbaSAndroid Build Coastguard Worker static inline void *libbpf_err_ptr(int err)
545*f7c14bbaSAndroid Build Coastguard Worker {
546*f7c14bbaSAndroid Build Coastguard Worker /* set errno on error, this doesn't break anything */
547*f7c14bbaSAndroid Build Coastguard Worker errno = -err;
548*f7c14bbaSAndroid Build Coastguard Worker return NULL;
549*f7c14bbaSAndroid Build Coastguard Worker }
550*f7c14bbaSAndroid Build Coastguard Worker
551*f7c14bbaSAndroid Build Coastguard Worker /* handle pointer-returning APIs' error handling */
libbpf_ptr(void * ret)552*f7c14bbaSAndroid Build Coastguard Worker static inline void *libbpf_ptr(void *ret)
553*f7c14bbaSAndroid Build Coastguard Worker {
554*f7c14bbaSAndroid Build Coastguard Worker /* set errno on error, this doesn't break anything */
555*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(ret))
556*f7c14bbaSAndroid Build Coastguard Worker errno = -PTR_ERR(ret);
557*f7c14bbaSAndroid Build Coastguard Worker
558*f7c14bbaSAndroid Build Coastguard Worker return IS_ERR(ret) ? NULL : ret;
559*f7c14bbaSAndroid Build Coastguard Worker }
560*f7c14bbaSAndroid Build Coastguard Worker
str_is_empty(const char * s)561*f7c14bbaSAndroid Build Coastguard Worker static inline bool str_is_empty(const char *s)
562*f7c14bbaSAndroid Build Coastguard Worker {
563*f7c14bbaSAndroid Build Coastguard Worker return !s || !s[0];
564*f7c14bbaSAndroid Build Coastguard Worker }
565*f7c14bbaSAndroid Build Coastguard Worker
is_ldimm64_insn(struct bpf_insn * insn)566*f7c14bbaSAndroid Build Coastguard Worker static inline bool is_ldimm64_insn(struct bpf_insn *insn)
567*f7c14bbaSAndroid Build Coastguard Worker {
568*f7c14bbaSAndroid Build Coastguard Worker return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
569*f7c14bbaSAndroid Build Coastguard Worker }
570*f7c14bbaSAndroid Build Coastguard Worker
571*f7c14bbaSAndroid Build Coastguard Worker /* Unconditionally dup FD, ensuring it doesn't use [0, 2] range.
572*f7c14bbaSAndroid Build Coastguard Worker * Original FD is not closed or altered in any other way.
573*f7c14bbaSAndroid Build Coastguard Worker * Preserves original FD value, if it's invalid (negative).
574*f7c14bbaSAndroid Build Coastguard Worker */
dup_good_fd(int fd)575*f7c14bbaSAndroid Build Coastguard Worker static inline int dup_good_fd(int fd)
576*f7c14bbaSAndroid Build Coastguard Worker {
577*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0)
578*f7c14bbaSAndroid Build Coastguard Worker return fd;
579*f7c14bbaSAndroid Build Coastguard Worker return fcntl(fd, F_DUPFD_CLOEXEC, 3);
580*f7c14bbaSAndroid Build Coastguard Worker }
581*f7c14bbaSAndroid Build Coastguard Worker
582*f7c14bbaSAndroid Build Coastguard Worker /* if fd is stdin, stdout, or stderr, dup to a fd greater than 2
583*f7c14bbaSAndroid Build Coastguard Worker * Takes ownership of the fd passed in, and closes it if calling
584*f7c14bbaSAndroid Build Coastguard Worker * fcntl(fd, F_DUPFD_CLOEXEC, 3).
585*f7c14bbaSAndroid Build Coastguard Worker */
ensure_good_fd(int fd)586*f7c14bbaSAndroid Build Coastguard Worker static inline int ensure_good_fd(int fd)
587*f7c14bbaSAndroid Build Coastguard Worker {
588*f7c14bbaSAndroid Build Coastguard Worker int old_fd = fd, saved_errno;
589*f7c14bbaSAndroid Build Coastguard Worker
590*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0)
591*f7c14bbaSAndroid Build Coastguard Worker return fd;
592*f7c14bbaSAndroid Build Coastguard Worker if (fd < 3) {
593*f7c14bbaSAndroid Build Coastguard Worker fd = dup_good_fd(fd);
594*f7c14bbaSAndroid Build Coastguard Worker saved_errno = errno;
595*f7c14bbaSAndroid Build Coastguard Worker close(old_fd);
596*f7c14bbaSAndroid Build Coastguard Worker errno = saved_errno;
597*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0) {
598*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to dup FD %d to FD > 2: %d\n", old_fd, -saved_errno);
599*f7c14bbaSAndroid Build Coastguard Worker errno = saved_errno;
600*f7c14bbaSAndroid Build Coastguard Worker }
601*f7c14bbaSAndroid Build Coastguard Worker }
602*f7c14bbaSAndroid Build Coastguard Worker return fd;
603*f7c14bbaSAndroid Build Coastguard Worker }
604*f7c14bbaSAndroid Build Coastguard Worker
sys_dup3(int oldfd,int newfd,int flags)605*f7c14bbaSAndroid Build Coastguard Worker static inline int sys_dup3(int oldfd, int newfd, int flags)
606*f7c14bbaSAndroid Build Coastguard Worker {
607*f7c14bbaSAndroid Build Coastguard Worker return syscall(__NR_dup3, oldfd, newfd, flags);
608*f7c14bbaSAndroid Build Coastguard Worker }
609*f7c14bbaSAndroid Build Coastguard Worker
610*f7c14bbaSAndroid Build Coastguard Worker /* Point *fixed_fd* to the same file that *tmp_fd* points to.
611*f7c14bbaSAndroid Build Coastguard Worker * Regardless of success, *tmp_fd* is closed.
612*f7c14bbaSAndroid Build Coastguard Worker * Whatever *fixed_fd* pointed to is closed silently.
613*f7c14bbaSAndroid Build Coastguard Worker */
reuse_fd(int fixed_fd,int tmp_fd)614*f7c14bbaSAndroid Build Coastguard Worker static inline int reuse_fd(int fixed_fd, int tmp_fd)
615*f7c14bbaSAndroid Build Coastguard Worker {
616*f7c14bbaSAndroid Build Coastguard Worker int err;
617*f7c14bbaSAndroid Build Coastguard Worker
618*f7c14bbaSAndroid Build Coastguard Worker err = sys_dup3(tmp_fd, fixed_fd, O_CLOEXEC);
619*f7c14bbaSAndroid Build Coastguard Worker err = err < 0 ? -errno : 0;
620*f7c14bbaSAndroid Build Coastguard Worker close(tmp_fd); /* clean up temporary FD */
621*f7c14bbaSAndroid Build Coastguard Worker return err;
622*f7c14bbaSAndroid Build Coastguard Worker }
623*f7c14bbaSAndroid Build Coastguard Worker
624*f7c14bbaSAndroid Build Coastguard Worker /* The following two functions are exposed to bpftool */
625*f7c14bbaSAndroid Build Coastguard Worker int bpf_core_add_cands(struct bpf_core_cand *local_cand,
626*f7c14bbaSAndroid Build Coastguard Worker size_t local_essent_len,
627*f7c14bbaSAndroid Build Coastguard Worker const struct btf *targ_btf,
628*f7c14bbaSAndroid Build Coastguard Worker const char *targ_btf_name,
629*f7c14bbaSAndroid Build Coastguard Worker int targ_start_id,
630*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand_list *cands);
631*f7c14bbaSAndroid Build Coastguard Worker void bpf_core_free_cands(struct bpf_core_cand_list *cands);
632*f7c14bbaSAndroid Build Coastguard Worker
633*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *usdt_manager_new(struct bpf_object *obj);
634*f7c14bbaSAndroid Build Coastguard Worker void usdt_manager_free(struct usdt_manager *man);
635*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link * usdt_manager_attach_usdt(struct usdt_manager *man,
636*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *prog,
637*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, const char *path,
638*f7c14bbaSAndroid Build Coastguard Worker const char *usdt_provider, const char *usdt_name,
639*f7c14bbaSAndroid Build Coastguard Worker __u64 usdt_cookie);
640*f7c14bbaSAndroid Build Coastguard Worker
is_pow_of_2(size_t x)641*f7c14bbaSAndroid Build Coastguard Worker static inline bool is_pow_of_2(size_t x)
642*f7c14bbaSAndroid Build Coastguard Worker {
643*f7c14bbaSAndroid Build Coastguard Worker return x && (x & (x - 1)) == 0;
644*f7c14bbaSAndroid Build Coastguard Worker }
645*f7c14bbaSAndroid Build Coastguard Worker
646*f7c14bbaSAndroid Build Coastguard Worker #define PROG_LOAD_ATTEMPTS 5
647*f7c14bbaSAndroid Build Coastguard Worker int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
648*f7c14bbaSAndroid Build Coastguard Worker
649*f7c14bbaSAndroid Build Coastguard Worker bool glob_match(const char *str, const char *pat);
650*f7c14bbaSAndroid Build Coastguard Worker
651*f7c14bbaSAndroid Build Coastguard Worker long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name);
652*f7c14bbaSAndroid Build Coastguard Worker long elf_find_func_offset_from_file(const char *binary_path, const char *name);
653*f7c14bbaSAndroid Build Coastguard Worker
654*f7c14bbaSAndroid Build Coastguard Worker struct elf_fd {
655*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
656*f7c14bbaSAndroid Build Coastguard Worker int fd;
657*f7c14bbaSAndroid Build Coastguard Worker };
658*f7c14bbaSAndroid Build Coastguard Worker
659*f7c14bbaSAndroid Build Coastguard Worker int elf_open(const char *binary_path, struct elf_fd *elf_fd);
660*f7c14bbaSAndroid Build Coastguard Worker void elf_close(struct elf_fd *elf_fd);
661*f7c14bbaSAndroid Build Coastguard Worker
662*f7c14bbaSAndroid Build Coastguard Worker int elf_resolve_syms_offsets(const char *binary_path, int cnt,
663*f7c14bbaSAndroid Build Coastguard Worker const char **syms, unsigned long **poffsets,
664*f7c14bbaSAndroid Build Coastguard Worker int st_type);
665*f7c14bbaSAndroid Build Coastguard Worker int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
666*f7c14bbaSAndroid Build Coastguard Worker unsigned long **poffsets, size_t *pcnt);
667*f7c14bbaSAndroid Build Coastguard Worker
668*f7c14bbaSAndroid Build Coastguard Worker int probe_fd(int fd);
669*f7c14bbaSAndroid Build Coastguard Worker
670*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
671