^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Internal libbpf helpers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2019 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #ifndef __LIBBPF_LIBBPF_INTERNAL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define __LIBBPF_LIBBPF_INTERNAL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* make sure libbpf doesn't use kernel-only integer typedefs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* prevent accidental re-addition of reallocarray() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #pragma GCC poison reallocarray
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "libbpf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define BTF_INFO_ENC(kind, kind_flag, vlen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) BTF_INT_ENC(encoding, bits_offset, bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define BTF_PARAM_ENC(name, type) (name), (type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #ifndef likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define likely(x) __builtin_expect(!!(x), 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #ifndef unlikely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define unlikely(x) __builtin_expect(!!(x), 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #ifndef min
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # define min(x, y) ((x) < (y) ? (x) : (y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #ifndef max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) # define max(x, y) ((x) < (y) ? (y) : (x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #ifndef offsetofend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) # define offsetofend(TYPE, FIELD) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) (offsetof(TYPE, FIELD) + sizeof(((TYPE *)0)->FIELD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Symbol versioning is different between static and shared library.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Properly versioned symbols are needed for shared library, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * only the symbol of the new version is needed for static library.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #ifdef SHARED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) # define COMPAT_VERSION(internal_name, api_name, version) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) asm(".symver " #internal_name "," #api_name "@" #version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) # define DEFAULT_VERSION(internal_name, api_name, version) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) asm(".symver " #internal_name "," #api_name "@@" #version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) # define COMPAT_VERSION(internal_name, api_name, version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) # define DEFAULT_VERSION(internal_name, api_name, version) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) extern typeof(internal_name) api_name \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) __attribute__((alias(#internal_name)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) extern void libbpf_print(enum libbpf_print_level level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) const char *format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __attribute__((format(printf, 2, 3)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define __pr(level, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) libbpf_print(level, "libbpf: " fmt, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define pr_warn(fmt, ...) __pr(LIBBPF_WARN, fmt, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #ifndef __has_builtin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define __has_builtin(x) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Re-implement glibc's reallocarray() for libbpf internal-only use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * reallocarray(), unfortunately, is not available in all versions of glibc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * so requires extra feature detection and using reallocarray() stub from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * <tools/libc_compat.h> and COMPAT_NEED_REALLOCARRAY. All this complicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * build of libbpf unnecessarily and is just a maintenance burden. Instead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * it's trivial to implement libbpf-specific internal version and use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * throughout libbpf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) size_t total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #if __has_builtin(__builtin_mul_overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (size == 0 || nmemb > ULONG_MAX / size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) total = nmemb * size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return realloc(ptr, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) size_t cur_cnt, size_t max_cnt, size_t add_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int btf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static inline bool libbpf_validate_opts(const char *opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) size_t opts_sz, size_t user_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) const char *type_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (user_sz < sizeof(size_t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pr_warn("%s size (%zu) is too small\n", type_name, user_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (user_sz > opts_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for (i = opts_sz; i < user_sz; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (opts[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pr_warn("%s has non-zero extra bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define OPTS_VALID(opts, type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) (!(opts) || libbpf_validate_opts((const char *)opts, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) offsetofend(struct type, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) type##__last_field), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) (opts)->sz, #type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #define OPTS_HAS(opts, field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define OPTS_GET(opts, field, fallback_value) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) (OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define OPTS_SET(opts, field, value) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (OPTS_HAS(opts, field)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) (opts)->field = value; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) const char *str_sec, size_t str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int bpf_object__section_size(const struct bpf_object *obj, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) __u32 *size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) __u32 *off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct btf_ext_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * info points to the individual info section (e.g. func_info and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) __u32 rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) __u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define for_each_btf_ext_sec(seg, sec) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) for (sec = (seg)->info; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) (void *)sec < (seg)->info + (seg)->len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sec = (void *)sec + sizeof(struct btf_ext_info_sec) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) (seg)->rec_size * sec->num_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) #define for_each_btf_ext_rec(seg, sec, i, rec) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) for (i = 0, rec = (void *)&(sec)->data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) i < (sec)->num_info; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) i++, rec = (void *)rec + (seg)->rec_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * The .BTF.ext ELF section layout defined as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * struct btf_ext_header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * func_info subsection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * The func_info subsection layout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * record size for struct bpf_func_info in the func_info subsection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * struct btf_sec_func_info for section #1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * a list of bpf_func_info records for section #1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * but may not be identical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * struct btf_sec_func_info for section #2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * a list of bpf_func_info records for section #2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * ......
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Note that the bpf_func_info record size in .BTF.ext may not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * be the same as the one defined in include/uapi/linux/bpf.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * The loader should ensure that record_size meets minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * requirement and pass the record as is to the kernel. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * kernel will handle the func_info properly based on its contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct btf_ext_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) __u16 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) __u8 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) __u8 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) __u32 hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* All offsets are in bytes relative to the end of this header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) __u32 func_info_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) __u32 func_info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) __u32 line_info_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) __u32 line_info_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* optional part of .BTF.ext header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) __u32 core_relo_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) __u32 core_relo_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct btf_ext {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct btf_ext_header *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct btf_ext_info func_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct btf_ext_info line_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct btf_ext_info core_relo_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) __u32 data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct btf_ext_info_sec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) __u32 sec_name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) __u32 num_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Followed by num_info * record_size number of bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) __u8 data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* The minimum bpf_func_info checked by the loader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct bpf_func_info_min {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) __u32 insn_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) __u32 type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* The minimum bpf_line_info checked by the loader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct bpf_line_info_min {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) __u32 insn_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) __u32 file_name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) __u32 line_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) __u32 line_col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * has to be adjusted by relocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) enum bpf_core_relo_kind {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) BPF_FIELD_BYTE_OFFSET = 0, /* field byte offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) BPF_FIELD_BYTE_SIZE = 1, /* field size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) BPF_FIELD_EXISTS = 2, /* field existence in target kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) BPF_FIELD_SIGNED = 3, /* field signedness (0 - unsigned, 1 - signed) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) BPF_FIELD_LSHIFT_U64 = 4, /* bitfield-specific left bitshift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) BPF_FIELD_RSHIFT_U64 = 5, /* bitfield-specific right bitshift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) BPF_TYPE_ID_LOCAL = 6, /* type ID in local BPF object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) BPF_TYPE_ID_TARGET = 7, /* type ID in target kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) BPF_TYPE_EXISTS = 8, /* type existence in target kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) BPF_TYPE_SIZE = 9, /* type size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) BPF_ENUMVAL_EXISTS = 10, /* enum value existence in target kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) BPF_ENUMVAL_VALUE = 11, /* enum value integer value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* The minimum bpf_core_relo checked by the loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * CO-RE relocation captures the following data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * - insn_off - instruction offset (in bytes) within a BPF program that needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * its insn->imm field to be relocated with actual field info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * type or field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * - access_str_off - offset into corresponding .BTF string section. String
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * interpretation depends on specific relocation kind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * - for field-based relocations, string encodes an accessed field using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * a sequence of field and array indices, separated by colon (:). It's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * conceptually very close to LLVM's getelementptr ([0]) instruction's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * arguments for identifying offset to a field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * - for type-based relocations, strings is expected to be just "0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * - for enum value-based relocations, string contains an index of enum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * value within its enum type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Example to provide a better feel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * struct sample {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * int a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * int b[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * struct sample *s = ...;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * int x = &s->a; // encoded as "0:0" (a is field #0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * int y = &s->b[5]; // encoded as "0:1:0:5" (anon struct is field #1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * // b is field #0 inside anon struct, accessing elem #5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * int z = &s[10]->b; // encoded as "10:1" (ptr is used as an array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * type_id for all relocs in this example will capture BTF type id of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * `struct sample`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * Such relocation is emitted when using __builtin_preserve_access_index()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * Clang built-in, passing expression that captures field address, e.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * bpf_probe_read(&dst, sizeof(dst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * __builtin_preserve_access_index(&src->a.b.c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * In this case Clang will emit field relocation recording necessary data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * be able to find offset of embedded `a.b.c` field within `src` struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct bpf_core_relo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) __u32 insn_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) __u32 type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) __u32 access_str_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) enum bpf_core_relo_kind kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) #endif /* __LIBBPF_LIBBPF_INTERNAL_H */