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