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