^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) /* Copyright (c) 2018 Facebook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <byteswap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <endian.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <gelf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "btf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "bpf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "libbpf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "libbpf_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "hashmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define BTF_MAX_NR_TYPES 0x7fffffffU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define BTF_MAX_STR_OFFSET 0x7fffffffU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct btf_type btf_void;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct btf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* raw BTF data in native endianness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void *raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* raw BTF data in non-native endianness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void *raw_data_swapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __u32 raw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* whether target endianness differs from the native one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) bool swapped_endian;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * When BTF is loaded from an ELF or raw memory it is stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * in a contiguous memory block. The hdr, type_data, and, strs_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * point inside that memory region to their respective parts of BTF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * representation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * +--------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * | Header | Types | Strings |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * +--------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * ^ ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * | | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * hdr | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * types_data-+ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * strs_data------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * If BTF data is later modified, e.g., due to types added or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * removed, BTF deduplication performed, etc, this contiguous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * representation is broken up into three independently allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * memory regions to be able to modify them independently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * raw_data is nulled out at that point, but can be later allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * and cached again if user calls btf__get_raw_data(), at which point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * raw_data will contain a contiguous copy of header, types, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * strings:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * +----------+ +---------+ +-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * | Header | | Types | | Strings |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * +----------+ +---------+ +-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * ^ ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * | | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * hdr | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * types_data----+ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * strs_data------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * +----------+---------+-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * | Header | Types | Strings |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * raw_data----->+----------+---------+-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct btf_header *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) void *types_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) size_t types_data_cap; /* used size stored in hdr->type_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* type ID to `struct btf_type *` lookup index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __u32 *type_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) size_t type_offs_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) __u32 nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) void *strs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) size_t strs_data_cap; /* used size stored in hdr->str_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* lookup index for each unique string in strings section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct hashmap *strs_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* whether strings are already deduplicated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bool strs_deduped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* BTF object FD, if loaded into kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Pointer size (in bytes) for a target architecture of this BTF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int ptr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline __u64 ptr_to_u64(const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return (__u64) (unsigned long) ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* Ensure given dynamically allocated memory region pointed to by *data* with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * memory to accomodate *add_cnt* new elements, assuming *cur_cnt* elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * are already used. At most *max_cnt* elements can be ever allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * If necessary, memory is reallocated and all existing data is copied over,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * new pointer to the memory region is stored at *data, new memory region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * capacity (in number of elements) is stored in *cap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * On success, memory pointer to the beginning of unused memory is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * On error, NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) size_t cur_cnt, size_t max_cnt, size_t add_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) size_t new_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void *new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (cur_cnt + add_cnt <= *cap_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return *data + cur_cnt * elem_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* requested more than the set limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (cur_cnt + add_cnt > max_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) new_cnt = *cap_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) new_cnt += new_cnt / 4; /* expand by 25% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (new_cnt < 16) /* but at least 16 elements */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) new_cnt = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (new_cnt > max_cnt) /* but not exceeding a set limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) new_cnt = max_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (new_cnt < cur_cnt + add_cnt) /* also ensure we have enough memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) new_cnt = cur_cnt + add_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) new_data = libbpf_reallocarray(*data, new_cnt, elem_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!new_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* zero out newly allocated portion of memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *data = new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *cap_cnt = new_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return new_data + cur_cnt * elem_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Ensure given dynamically allocated memory region has enough allocated space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * to accommodate *need_cnt* elements of size *elem_sz* bytes each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 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 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (need_cnt <= *cap_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) p = btf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) __u32 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) p = btf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) btf->nr_types + 1, BTF_MAX_NR_TYPES, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *p = type_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void btf_bswap_hdr(struct btf_header *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) h->magic = bswap_16(h->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) h->hdr_len = bswap_32(h->hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) h->type_off = bswap_32(h->type_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) h->type_len = bswap_32(h->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) h->str_off = bswap_32(h->str_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) h->str_len = bswap_32(h->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int btf_parse_hdr(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct btf_header *hdr = btf->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) __u32 meta_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (btf->raw_size < sizeof(struct btf_header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pr_debug("BTF header not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (hdr->magic == bswap_16(BTF_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) btf->swapped_endian = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) bswap_32(hdr->hdr_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) btf_bswap_hdr(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) } else if (hdr->magic != BTF_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) pr_debug("Invalid BTF magic: %x\n", hdr->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (btf->raw_size < hdr->hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) pr_debug("BTF header len %u larger than data size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) hdr->hdr_len, btf->raw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -EINVAL;
^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) meta_left = btf->raw_size - hdr->hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (meta_left < (long long)hdr->str_off + hdr->str_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pr_debug("Invalid BTF total size: %u\n", btf->raw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (hdr->type_off % 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pr_debug("BTF type section is not aligned to 4 bytes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int btf_parse_str_sec(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) const struct btf_header *hdr = btf->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) const char *start = btf->strs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const char *end = start + btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) start[0] || end[-1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pr_debug("Invalid BTF string section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -EINVAL;
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int btf_type_size(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) const int base_size = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case BTF_KIND_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return base_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return base_size + sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return base_size + vlen * sizeof(struct btf_enum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case BTF_KIND_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return base_size + sizeof(struct btf_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) case BTF_KIND_UNION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return base_size + vlen * sizeof(struct btf_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case BTF_KIND_FUNC_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return base_size + vlen * sizeof(struct btf_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) case BTF_KIND_VAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return base_size + sizeof(struct btf_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) case BTF_KIND_DATASEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return base_size + vlen * sizeof(struct btf_var_secinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static void btf_bswap_type_base(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) t->name_off = bswap_32(t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) t->info = bswap_32(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) t->type = bswap_32(t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int btf_bswap_type_rest(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct btf_var_secinfo *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct btf_member *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct btf_array *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct btf_param *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct btf_enum *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) case BTF_KIND_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) e->name_off = bswap_32(e->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) e->val = bswap_32(e->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) case BTF_KIND_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) a = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) a->type = bswap_32(a->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) a->index_type = bswap_32(a->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) a->nelems = bswap_32(a->nelems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case BTF_KIND_UNION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) for (i = 0, m = btf_members(t); i < vlen; i++, m++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) m->name_off = bswap_32(m->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) m->type = bswap_32(m->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) m->offset = bswap_32(m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) case BTF_KIND_FUNC_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) for (i = 0, p = btf_params(t); i < vlen; i++, p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) p->name_off = bswap_32(p->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) p->type = bswap_32(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) case BTF_KIND_VAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) btf_var(t)->linkage = bswap_32(btf_var(t)->linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) case BTF_KIND_DATASEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) v->type = bswap_32(v->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) v->offset = bswap_32(v->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) v->size = bswap_32(v->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int btf_parse_type_sec(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct btf_header *hdr = btf->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) void *next_type = btf->types_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) void *end_type = next_type + hdr->type_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int err, i = 0, type_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* VOID (type_id == 0) is specially handled by btf__get_type_by_id(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * so ensure we can never properly use its offset from index by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * setting it to a large value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err = btf_add_type_idx_entry(btf, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) while (next_type + sizeof(struct btf_type) <= end_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (btf->swapped_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) btf_bswap_type_base(next_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) type_size = btf_type_size(next_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (type_size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return type_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (next_type + type_size > end_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) pr_warn("BTF type [%d] is malformed\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (btf->swapped_endian && btf_bswap_type_rest(next_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) err = btf_add_type_idx_entry(btf, next_type - btf->types_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) next_type += type_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (next_type != end_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pr_warn("BTF types data is malformed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) __u32 btf__get_nr_types(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* internal helper returning non-const pointer to a type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (type_id == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return &btf_void;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return btf->types_data + btf->type_offs[type_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (type_id > btf->nr_types)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return btf_type_by_id((struct btf *)btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int determine_ptr_size(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) const struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) t = btf__type_by_id(btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!btf_is_int(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) name = btf__name_by_offset(btf, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (strcmp(name, "long int") == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) strcmp(name, "long unsigned int") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (t->size != 4 && t->size != 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return t->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static size_t btf_ptr_sz(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (!btf->ptr_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /* Return pointer size this BTF instance assumes. The size is heuristically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * determined by looking for 'long' or 'unsigned long' integer type and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * recording its size in bytes. If BTF type information doesn't have any such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * type, this function returns 0. In the latter case, native architecture's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * pointer size is assumed, so will be either 4 or 8, depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * architecture that libbpf was compiled for. It's possible to override
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * guessed value by using btf__set_pointer_size() API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) size_t btf__pointer_size(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (!btf->ptr_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (btf->ptr_sz < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* not enough BTF type info to guess */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return btf->ptr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* Override or set pointer size in bytes. Only values of 4 and 8 are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int btf__set_pointer_size(struct btf *btf, size_t ptr_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (ptr_sz != 4 && ptr_sz != 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) btf->ptr_sz = ptr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static bool is_host_big_endian(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) #if __BYTE_ORDER == __LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) #elif __BYTE_ORDER == __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) # error "Unrecognized __BYTE_ORDER__"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) enum btf_endianness btf__endianness(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (is_host_big_endian())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int btf__set_endianness(struct btf *btf, enum btf_endianness endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (!btf->swapped_endian) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) free(btf->raw_data_swapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) btf->raw_data_swapped = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static bool btf_type_is_void(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return t == &btf_void || btf_is_fwd(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static bool btf_type_is_void_or_null(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return !t || btf_type_is_void(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) #define MAX_RESOLVE_DEPTH 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) const struct btf_array *array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) const struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) __u32 nelems = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) __s64 size = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) t = btf__type_by_id(btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) case BTF_KIND_UNION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) case BTF_KIND_DATASEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) size = t->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) size = btf_ptr_sz(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) case BTF_KIND_VAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) type_id = t->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) case BTF_KIND_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) array = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (nelems && array->nelems > UINT32_MAX / nelems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) nelems *= array->nelems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) type_id = array->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) t = btf__type_by_id(btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (nelems && size > UINT32_MAX / nelems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return nelems * size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int btf__align_of(const struct btf *btf, __u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) const struct btf_type *t = btf__type_by_id(btf, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) __u16 kind = btf_kind(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) switch (kind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return min(btf_ptr_sz(btf), (size_t)t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return btf_ptr_sz(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return btf__align_of(btf, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) case BTF_KIND_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return btf__align_of(btf, btf_array(t)->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) case BTF_KIND_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) const struct btf_member *m = btf_members(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int i, max_align = 1, align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) for (i = 0; i < vlen; i++, m++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) align = btf__align_of(btf, m->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (align <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) max_align = max(max_align, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return max_align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int btf__resolve_type(const struct btf *btf, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) const struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) int depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) t = btf__type_by_id(btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) while (depth < MAX_RESOLVE_DEPTH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) !btf_type_is_void_or_null(t) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) type_id = t->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) t = btf__type_by_id(btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) __u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!strcmp(type_name, "void"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) const struct btf_type *t = btf__type_by_id(btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) const char *name = btf__name_by_offset(btf, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (name && !strcmp(type_name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) __u32 kind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) __u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) const struct btf_type *t = btf__type_by_id(btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (btf_kind(t) != kind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) name = btf__name_by_offset(btf, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (name && !strcmp(type_name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) static bool btf_is_modifiable(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return (void *)btf->hdr != btf->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) void btf__free(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (IS_ERR_OR_NULL(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (btf->fd >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) close(btf->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (btf_is_modifiable(btf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /* if BTF was modified after loading, it will have a split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * in-memory representation for header, types, and strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * sections, so we need to free all of them individually. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * might still have a cached contiguous raw data present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * which will be unconditionally freed below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) free(btf->hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) free(btf->types_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) free(btf->strs_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) free(btf->raw_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) free(btf->raw_data_swapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) free(btf->type_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) free(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct btf *btf__new_empty(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) struct btf *btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) btf = calloc(1, sizeof(*btf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (!btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) btf->fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) btf->ptr_sz = sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) btf->swapped_endian = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) /* +1 for empty string at offset 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) btf->raw_size = sizeof(struct btf_header) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) btf->raw_data = calloc(1, btf->raw_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (!btf->raw_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) free(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) btf->hdr = btf->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) btf->hdr->hdr_len = sizeof(struct btf_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) btf->hdr->magic = BTF_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) btf->hdr->version = BTF_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) btf->types_data = btf->raw_data + btf->hdr->hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) btf->hdr->str_len = 1; /* empty string at offset 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) struct btf *btf__new(const void *data, __u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct btf *btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) btf = calloc(1, sizeof(struct btf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (!btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) btf->raw_data = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (!btf->raw_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) memcpy(btf->raw_data, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) btf->raw_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) btf->hdr = btf->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) err = btf_parse_hdr(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) err = btf_parse_str_sec(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) err = err ?: btf_parse_type_sec(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) btf->fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) btf__free(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) int err = 0, fd = -1, idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) struct btf *btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) Elf_Scn *scn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) Elf *elf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) GElf_Ehdr ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (elf_version(EV_CURRENT) == EV_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) pr_warn("failed to init libelf for %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) fd = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) pr_warn("failed to open %s: %s\n", path, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) err = -LIBBPF_ERRNO__FORMAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) elf = elf_begin(fd, ELF_C_READ, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (!elf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) pr_warn("failed to open %s as ELF file\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (!gelf_getehdr(elf, &ehdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) pr_warn("failed to get EHDR from %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) pr_warn("failed to get e_shstrndx from %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) while ((scn = elf_nextscn(elf, scn)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) GElf_Shdr sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (gelf_getshdr(scn, &sh) != &sh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) pr_warn("failed to get section(%d) header from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) idx, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) pr_warn("failed to get section(%d) name from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) idx, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (strcmp(name, BTF_ELF_SEC) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) btf_data = elf_getdata(scn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (!btf_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) pr_warn("failed to get section(%d, %s) data from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) idx, name, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) btf_ext_data = elf_getdata(scn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (!btf_ext_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) pr_warn("failed to get section(%d, %s) data from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) idx, name, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (!btf_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) btf = btf__new(btf_data->d_buf, btf_data->d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (IS_ERR(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) switch (gelf_getclass(elf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) case ELFCLASS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) btf__set_pointer_size(btf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) case ELFCLASS64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) btf__set_pointer_size(btf, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) pr_warn("failed to get ELF class (bitness) for %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (btf_ext && btf_ext_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) *btf_ext = btf_ext__new(btf_ext_data->d_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) btf_ext_data->d_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) if (IS_ERR(*btf_ext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) } else if (btf_ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) *btf_ext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) elf_end(elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * btf is always parsed before btf_ext, so no need to clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * btf_ext, if btf loading failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (IS_ERR(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (btf_ext && IS_ERR(*btf_ext)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) btf__free(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) err = PTR_ERR(*btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) struct btf *btf__parse_raw(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct btf *btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) void *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) FILE *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) __u16 magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) long sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) f = fopen(path, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /* check BTF magic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /* definitely not a raw BTF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) err = -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) /* get file size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (fseek(f, 0, SEEK_END)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) sz = ftell(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (sz < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /* rewind to the start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (fseek(f, 0, SEEK_SET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) /* pre-alloc memory and read all of BTF data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) data = malloc(sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (fread(data, 1, sz, f) < sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /* finally parse BTF data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) btf = btf__new(data, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return err ? ERR_PTR(err) : btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct btf *btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) *btf_ext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) btf = btf__parse_raw(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) return btf__parse_elf(path, btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) static int compare_vsi_off(const void *_a, const void *_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) const struct btf_var_secinfo *a = _a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) const struct btf_var_secinfo *b = _b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) return a->offset - b->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) __u32 size = 0, off = 0, i, vars = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) const char *name = btf__name_by_offset(btf, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) const struct btf_type *t_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct btf_var_secinfo *vsi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) const struct btf_var *var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) pr_debug("No name found in string section for DATASEC kind.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) /* .extern datasec size and var offsets were set correctly during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * extern collection step, so just skip straight to sorting variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (t->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) goto sort_vars;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ret = bpf_object__section_size(obj, name, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (ret || !size || (t->size && t->size != size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) pr_debug("Invalid size for section %s: %u bytes\n", name, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) t->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) t_var = btf__type_by_id(btf, vsi->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) var = btf_var(t_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (!btf_is_var(t_var)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) pr_debug("Non-VAR type seen in section %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (var->linkage == BTF_VAR_STATIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) name = btf__name_by_offset(btf, t_var->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) pr_debug("No name found in string section for VAR kind\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) ret = bpf_object__variable_offset(obj, name, &off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) pr_debug("No offset found in symbol table for VAR %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) vsi->offset = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) sort_vars:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) __u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) struct btf_type *t = btf_type_by_id(btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /* Loader needs to fix up some of the things compiler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * couldn't get its hands on while emitting BTF. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * is section size and global variable offset. We use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * the info from the ELF itself for this purpose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (btf_is_datasec(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) err = btf_fixup_datasec(obj, btf, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) int btf__load(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) __u32 log_buf_size = 0, raw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) char *log_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) void *raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (btf->fd >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) retry_load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (log_buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) log_buf = malloc(log_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (!log_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) *log_buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) raw_data = btf_get_raw_data(btf, &raw_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (!raw_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /* cache native raw data representation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) btf->raw_size = raw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) btf->raw_data = raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) btf->fd = bpf_load_btf(raw_data, raw_size, log_buf, log_buf_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (btf->fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (!log_buf || errno == ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) log_buf_size << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) free(log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) goto retry_load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (*log_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) pr_warn("%s\n", log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) free(log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) int btf__fd(const struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return btf->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) void btf__set_fd(struct btf *btf, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) btf->fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) struct btf_header *hdr = btf->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) void *data, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) __u32 data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) data = swap_endian ? btf->raw_data_swapped : btf->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) if (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) *size = btf->raw_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) data = calloc(1, data_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) p = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) memcpy(p, hdr, hdr->hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) if (swap_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) btf_bswap_hdr(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) p += hdr->hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) memcpy(p, btf->types_data, hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) if (swap_endian) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) t = p + btf->type_offs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) /* btf_bswap_type_rest() relies on native t->info, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * we swap base type info after we swapped all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * additional information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (btf_bswap_type_rest(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) btf_bswap_type_base(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) p += hdr->type_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) memcpy(p, btf->strs_data, hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) p += hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) *size = data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct btf *btf = (struct btf *)btf_ro;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) __u32 data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) btf->raw_size = data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) if (btf->swapped_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) btf->raw_data_swapped = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) btf->raw_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) *size = data_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (offset < btf->hdr->str_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) return btf->strs_data + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return btf__str_by_offset(btf, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) int btf__get_from_id(__u32 id, struct btf **btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct bpf_btf_info btf_info = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) __u32 len = sizeof(btf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) __u32 last_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) int btf_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) *btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) btf_fd = bpf_btf_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (btf_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * let's start with a sane default - 4KiB here - and resize it only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * bpf_obj_get_info_by_fd() needs a bigger buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) btf_info.btf_size = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) last_size = btf_info.btf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) ptr = malloc(last_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) memset(ptr, 0, last_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) btf_info.btf = ptr_to_u64(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (!err && btf_info.btf_size > last_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) void *temp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) last_size = btf_info.btf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) temp_ptr = realloc(ptr, last_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) if (!temp_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) ptr = temp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) memset(ptr, 0, last_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) btf_info.btf = ptr_to_u64(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (err || btf_info.btf_size > last_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) if (IS_ERR(*btf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) err = PTR_ERR(*btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) *btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) exit_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) close(btf_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) __u32 expected_key_size, __u32 expected_value_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) __u32 *key_type_id, __u32 *value_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) const struct btf_type *container_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) const struct btf_member *key, *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) const size_t max_name = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) char container_name[max_name];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) __s64 key_size, value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) __s32 container_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) max_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) pr_warn("map:%s length of '____btf_map_%s' is too long\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) map_name, map_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) container_id = btf__find_by_name(btf, container_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) if (container_id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) map_name, container_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) return container_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) container_type = btf__type_by_id(btf, container_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) if (!container_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) pr_warn("map:%s cannot find BTF type for container_id:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) map_name, container_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) pr_warn("map:%s container_name:%s is an invalid container struct\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) map_name, container_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) key = btf_members(container_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) value = key + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) key_size = btf__resolve_size(btf, key->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) if (key_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) pr_warn("map:%s invalid BTF key_type_size\n", map_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) return key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (expected_key_size != key_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) map_name, (__u32)key_size, expected_key_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) value_size = btf__resolve_size(btf, value->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (value_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) pr_warn("map:%s invalid BTF value_type_size\n", map_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) return value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (expected_value_size != value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) map_name, (__u32)value_size, expected_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) *key_type_id = key->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) *value_type_id = value->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) static size_t strs_hash_fn(const void *key, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) struct btf *btf = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) const char *str = btf->strs_data + (long)key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) return str_hash(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) static bool strs_hash_equal_fn(const void *key1, const void *key2, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) struct btf *btf = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) const char *str1 = btf->strs_data + (long)key1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) const char *str2 = btf->strs_data + (long)key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) return strcmp(str1, str2) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) static void btf_invalidate_raw_data(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) if (btf->raw_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) free(btf->raw_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) btf->raw_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) if (btf->raw_data_swapped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) free(btf->raw_data_swapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) btf->raw_data_swapped = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) /* Ensure BTF is ready to be modified (by splitting into a three memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) * regions for header, types, and strings). Also invalidate cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) * raw_data, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) static int btf_ensure_modifiable(struct btf *btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) void *hdr, *types, *strs, *strs_end, *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) struct hashmap *hash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) long off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) if (btf_is_modifiable(btf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) /* any BTF modification invalidates raw_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) btf_invalidate_raw_data(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /* split raw data into three memory regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) hdr = malloc(btf->hdr->hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) types = malloc(btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) strs = malloc(btf->hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) if (!hdr || !types || !strs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) memcpy(types, btf->types_data, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) memcpy(strs, btf->strs_data, btf->hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) /* build lookup index for all strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (IS_ERR(hash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) err = PTR_ERR(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) hash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) strs_end = strs + btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) for (off = 0, s = strs; s < strs_end; off += strlen(s) + 1, s = strs + off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) /* hashmap__add() returns EEXIST if string with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) * content already is in the hash map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) err = hashmap__add(hash, (void *)off, (void *)off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (err == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) continue; /* duplicate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) /* only when everything was successful, update internal state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) btf->hdr = hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) btf->types_data = types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) btf->types_data_cap = btf->hdr->type_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) btf->strs_data = strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) btf->strs_data_cap = btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) btf->strs_hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) /* if BTF was created from scratch, all strings are guaranteed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * unique and deduplicated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) btf->strs_deduped = btf->hdr->str_len <= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) /* invalidate raw_data representation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) btf_invalidate_raw_data(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) hashmap__free(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) free(hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) free(types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) free(strs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static void *btf_add_str_mem(struct btf *btf, size_t add_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) return btf_add_mem(&btf->strs_data, &btf->strs_data_cap, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) btf->hdr->str_len, BTF_MAX_STR_OFFSET, add_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) /* Find an offset in BTF string section that corresponds to a given string *s*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) * - >0 offset into string section, if string is found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) * - -ENOENT, if string is not in the string section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) * - <0, on any other error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) int btf__find_str(struct btf *btf, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) long old_off, new_off, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) /* BTF needs to be in a modifiable state to build string lookup index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) /* see btf__add_str() for why we do this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) len = strlen(s) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) p = btf_add_str_mem(btf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) new_off = btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) memcpy(p, s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) if (hashmap__find(btf->strs_hash, (void *)new_off, (void **)&old_off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return old_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) /* Add a string s to the BTF string section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) * - > 0 offset into string section, on success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) * - < 0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) int btf__add_str(struct btf *btf, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) long old_off, new_off, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) /* Hashmap keys are always offsets within btf->strs_data, so to even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) * look up some string from the "outside", we need to first append it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) * at the end, so that it can be addressed with an offset. Luckily,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) * until btf->hdr->str_len is incremented, that string is just a piece
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) * of garbage for the rest of BTF code, so no harm, no foul. On the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) * other hand, if the string is unique, it's already appended and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) * ready to be used, only a simple btf->hdr->str_len increment away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) len = strlen(s) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) p = btf_add_str_mem(btf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) new_off = btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) memcpy(p, s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) /* Now attempt to add the string, but only if the string with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) * contents doesn't exist already (HASHMAP_ADD strategy). If such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) * string exists, we'll get its offset in old_off (that's old_key).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) err = hashmap__insert(btf->strs_hash, (void *)new_off, (void *)new_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) HASHMAP_ADD, (const void **)&old_off, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) if (err == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) return old_off; /* duplicated string, return existing offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) btf->hdr->str_len += len; /* new unique string, adjust data length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) return new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) return btf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) btf->hdr->type_len, UINT_MAX, add_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) static __u32 btf_type_info(int kind, int vlen, int kflag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) return (kflag << 31) | (kind << 24) | vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) static void btf_type_inc_vlen(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) * Append new BTF_KIND_INT type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) * - *name* - non-empty, non-NULL type name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) int sz, err, name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) /* non-empty name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) /* byte_sz must be power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) /* deconstruct BTF, if necessary, and invalidate raw_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) sz = sizeof(struct btf_type) + sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) /* if something goes wrong later, we might end up with an extra string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) * but that shouldn't be a problem, because BTF can't be constructed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) * completely anyway and will most probably be just discarded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) t->info = btf_type_info(BTF_KIND_INT, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) t->size = byte_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) /* set INT info, we don't allow setting legacy bit offset/size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) /* it's completely legal to append BTF types with type IDs pointing forward to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) * types that haven't been appended yet, so we only make sure that id looks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) * sane, we can't guarantee that ID will always be valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) static int validate_type_id(int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (id < 0 || id > BTF_MAX_NR_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) int sz, name_off = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (validate_type_id(ref_type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) sz = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (name && name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) t->info = btf_type_info(kind, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) t->type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) * Append new BTF_KIND_PTR type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) * - *ref_type_id* - referenced type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) int btf__add_ptr(struct btf *btf, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) * Append new BTF_KIND_ARRAY type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) * - *index_type_id* - type ID of the type describing array index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) * - *elem_type_id* - type ID of the type describing array element;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) * - *nr_elems* - the size of the array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) struct btf_array *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) int sz, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) if (validate_type_id(index_type_id) || validate_type_id(elem_type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) sz = sizeof(struct btf_type) + sizeof(struct btf_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) t->name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) t->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) a = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) a->type = elem_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) a->index_type = index_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) a->nelems = nr_elems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) /* generic STRUCT/UNION append function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) int sz, err, name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) sz = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) if (name && name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) /* start out with vlen=0 and no kflag; this will be adjusted when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) * adding each member
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) t->info = btf_type_info(kind, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) t->size = bytes_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) * Append new BTF_KIND_STRUCT type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) * - *name* - name of the struct, can be NULL or empty for anonymous structs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) * - *byte_sz* - size of the struct, in bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) * Struct initially has no fields in it. Fields can be added by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) * btf__add_field() right after btf__add_struct() succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) * Append new BTF_KIND_UNION type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * - *name* - name of the union, can be NULL or empty for anonymous union;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) * - *byte_sz* - size of the union, in bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) * Union initially has no fields in it. Fields can be added by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) * btf__add_field() right after btf__add_union() succeeds. All fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) * should have *bit_offset* of 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) * Append new field for the current STRUCT/UNION type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) * - *name* - name of the field, can be NULL or empty for anonymous field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) * - *type_id* - type ID for the type describing field type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) * - *bit_offset* - bit offset of the start of the field within struct/union;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) * - 0, on success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) int btf__add_field(struct btf *btf, const char *name, int type_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) __u32 bit_offset, __u32 bit_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) struct btf_member *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) bool is_bitfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) int sz, name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) /* last type should be union/struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) if (btf->nr_types == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) if (!btf_is_composite(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) if (validate_type_id(type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) /* best-effort bit field offset/size enforcement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) is_bitfield = bit_size || (bit_offset % 8 != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) /* only offset 0 is allowed for unions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) if (btf_is_union(t) && bit_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) /* decompose and invalidate raw data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) sz = sizeof(struct btf_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) m = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) if (!m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) if (name && name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) m->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) m->type = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) m->offset = bit_offset | (bit_size << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) /* btf_add_type_mem can invalidate t pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) /* update parent type's vlen and kflag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) * Append new BTF_KIND_ENUM type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) * - *name* - name of the enum, can be NULL or empty for anonymous enums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) * - *byte_sz* - size of the enum, in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) * Enum initially has no enum values in it (and corresponds to enum forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) * declaration). Enumerator values can be added by btf__add_enum_value()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) * immediately after btf__add_enum() succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) int sz, err, name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) /* byte_sz must be power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) sz = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) if (name && name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) /* start out with vlen=0; it will be adjusted when adding enum values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) t->size = byte_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) * Append new enum value for the current ENUM type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) * - *name* - name of the enumerator value, can't be NULL or empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) * - *value* - integer value corresponding to enum value *name*;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) * - 0, on success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) struct btf_enum *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) int sz, name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) /* last type should be BTF_KIND_ENUM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) if (btf->nr_types == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) if (!btf_is_enum(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) /* non-empty name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) if (value < INT_MIN || value > UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) /* decompose and invalidate raw data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) sz = sizeof(struct btf_enum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) v = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) v->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) v->val = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) /* update parent type's vlen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) btf_type_inc_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * Append new BTF_KIND_FWD type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) * - *name*, non-empty/non-NULL name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) * BTF_FWD_UNION, or BTF_FWD_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) switch (fwd_kind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) case BTF_FWD_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) case BTF_FWD_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) if (id <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) t = btf_type_by_id(btf, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) case BTF_FWD_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) /* enum forward in BTF currently is just an enum with no enum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) * values; we also assume a standard 4-byte size for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) return btf__add_enum(btf, name, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) * Append new BTF_KING_TYPEDEF type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) * - *name*, non-empty/non-NULL name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) * - *ref_type_id* - referenced type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) * Append new BTF_KIND_VOLATILE type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) * - *ref_type_id* - referenced type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) int btf__add_volatile(struct btf *btf, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) * Append new BTF_KIND_CONST type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) * - *ref_type_id* - referenced type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) int btf__add_const(struct btf *btf, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) * Append new BTF_KIND_RESTRICT type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) * - *ref_type_id* - referenced type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) int btf__add_restrict(struct btf *btf, int ref_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) * Append new BTF_KIND_FUNC type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) * - *name*, non-empty/non-NULL name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) int btf__add_func(struct btf *btf, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) enum btf_func_linkage linkage, int proto_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) linkage != BTF_FUNC_EXTERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) if (id > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) struct btf_type *t = btf_type_by_id(btf, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) * Append new BTF_KIND_FUNC_PROTO with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) * - *ret_type_id* - type ID for return result of a function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) * Function prototype initially has no arguments, but they can be added by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) * btf__add_func_param() one by one, immediately after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) * btf__add_func_proto() succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) int btf__add_func_proto(struct btf *btf, int ret_type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) int sz, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (validate_type_id(ret_type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) sz = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) /* start out with vlen=0; this will be adjusted when adding enum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) * values, if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) t->name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) t->type = ret_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) * Append new function parameter for current FUNC_PROTO type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) * - *name* - parameter name, can be NULL or empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) * - *type_id* - type ID describing the type of the parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) * - 0, on success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) int btf__add_func_param(struct btf *btf, const char *name, int type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) struct btf_param *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) int sz, name_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) if (validate_type_id(type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) /* last type should be BTF_KIND_FUNC_PROTO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) if (btf->nr_types == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) if (!btf_is_func_proto(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) /* decompose and invalidate raw data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) sz = sizeof(struct btf_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) p = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) if (name && name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) p->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) p->type = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) /* update parent type's vlen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) btf_type_inc_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) * Append new BTF_KIND_VAR type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) * - *name* - non-empty/non-NULL name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) * - *linkage* - variable linkage, one of BTF_VAR_STATIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) * BTF_VAR_GLOBAL_ALLOCATED, or BTF_VAR_GLOBAL_EXTERN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) * - *type_id* - type ID of the type describing the type of the variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) struct btf_var *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) int sz, err, name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) /* non-empty name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) linkage != BTF_VAR_GLOBAL_EXTERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) if (validate_type_id(type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) /* deconstruct BTF, if necessary, and invalidate raw_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) sz = sizeof(struct btf_type) + sizeof(struct btf_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) t->info = btf_type_info(BTF_KIND_VAR, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) t->type = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) v = btf_var(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) v->linkage = linkage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) * Append new BTF_KIND_DATASEC type with:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) * - *name* - non-empty/non-NULL name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) * - *byte_sz* - data section size, in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) * Data section is initially empty. Variables info can be added with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) * btf__add_datasec_var_info() calls, after btf__add_datasec() succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) * - >0, type ID of newly added BTF type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) int sz, err, name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) /* non-empty name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) if (!name || !name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) sz = sizeof(struct btf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) t = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) name_off = btf__add_str(btf, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) if (name_off < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) return name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) /* start with vlen=0, which will be update as var_secinfos are added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) t->name_off = name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) t->size = byte_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) btf->nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) return btf->nr_types;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) * Append new data section variable information entry for current DATASEC type:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) * - *var_type_id* - type ID, describing type of the variable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) * - *offset* - variable offset within data section, in bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) * - *byte_sz* - variable size, in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) * - 0, on success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) struct btf_var_secinfo *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) int sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) /* last type should be BTF_KIND_DATASEC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) if (btf->nr_types == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) if (!btf_is_datasec(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) if (validate_type_id(var_type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) /* decompose and invalidate raw data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) sz = sizeof(struct btf_var_secinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) v = btf_add_type_mem(btf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) v->type = var_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) v->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) v->size = byte_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) /* update parent type's vlen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) t = btf_type_by_id(btf, btf->nr_types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) btf_type_inc_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) btf->hdr->type_len += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) btf->hdr->str_off += sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) struct btf_ext_sec_setup_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) __u32 off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) __u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) __u32 min_rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) struct btf_ext_info *ext_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) const char *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) static int btf_ext_setup_info(struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) struct btf_ext_sec_setup_param *ext_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) const struct btf_ext_info_sec *sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) struct btf_ext_info *ext_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) __u32 info_left, record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) /* The start of the info sec (including the __u32 record_size). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) void *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) if (ext_sec->len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) if (ext_sec->off & 0x03) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) info_left = ext_sec->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) ext_sec->desc, ext_sec->off, ext_sec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) /* At least a record size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) if (info_left < sizeof(__u32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) /* The record size needs to meet the minimum standard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) record_size = *(__u32 *)info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) if (record_size < ext_sec->min_rec_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) record_size & 0x03) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) pr_debug("%s section in .BTF.ext has invalid record size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) ext_sec->desc, record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) sinfo = info + sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) info_left -= sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) /* If no records, return failure now so .BTF.ext won't be used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) if (!info_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) while (info_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) __u64 total_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) __u32 num_records;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) if (info_left < sec_hdrlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) pr_debug("%s section header is not found in .BTF.ext\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) num_records = sinfo->num_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) if (num_records == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) pr_debug("%s section has incorrect num_records in .BTF.ext\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) total_record_size = sec_hdrlen +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) (__u64)num_records * record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) if (info_left < total_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) pr_debug("%s section has incorrect num_records in .BTF.ext\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) ext_sec->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) info_left -= total_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) sinfo = (void *)sinfo + total_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) ext_info = ext_sec->ext_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) ext_info->len = ext_sec->len - sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) ext_info->rec_size = record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) ext_info->info = info + sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) struct btf_ext_sec_setup_param param = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) .off = btf_ext->hdr->func_info_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) .len = btf_ext->hdr->func_info_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) .min_rec_size = sizeof(struct bpf_func_info_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) .ext_info = &btf_ext->func_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) .desc = "func_info"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) return btf_ext_setup_info(btf_ext, ¶m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) struct btf_ext_sec_setup_param param = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) .off = btf_ext->hdr->line_info_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) .len = btf_ext->hdr->line_info_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) .min_rec_size = sizeof(struct bpf_line_info_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) .ext_info = &btf_ext->line_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) .desc = "line_info",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) return btf_ext_setup_info(btf_ext, ¶m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) struct btf_ext_sec_setup_param param = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) .off = btf_ext->hdr->core_relo_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) .len = btf_ext->hdr->core_relo_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) .min_rec_size = sizeof(struct bpf_core_relo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) .ext_info = &btf_ext->core_relo_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) .desc = "core_relo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) return btf_ext_setup_info(btf_ext, ¶m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) data_size < hdr->hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) pr_debug("BTF.ext header not found");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) if (hdr->magic == bswap_16(BTF_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) pr_warn("BTF.ext in non-native endianness is not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) return -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) } else if (hdr->magic != BTF_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) if (hdr->version != BTF_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) return -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) if (hdr->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) return -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) if (data_size == hdr->hdr_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) pr_debug("BTF.ext has no data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) void btf_ext__free(struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) if (IS_ERR_OR_NULL(btf_ext))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) free(btf_ext->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) free(btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) struct btf_ext *btf_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) err = btf_ext_parse_hdr(data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) btf_ext = calloc(1, sizeof(struct btf_ext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) if (!btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) btf_ext->data_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) btf_ext->data = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) if (!btf_ext->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) memcpy(btf_ext->data, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) if (btf_ext->hdr->hdr_len <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) offsetofend(struct btf_ext_header, line_info_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) err = btf_ext_setup_func_info(btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) err = btf_ext_setup_line_info(btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) err = btf_ext_setup_core_relos(btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) btf_ext__free(btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) return btf_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) *size = btf_ext->data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) return btf_ext->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) static int btf_ext_reloc_info(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) const struct btf_ext_info *ext_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) const char *sec_name, __u32 insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) void **info, __u32 *cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) __u32 i, record_size, existing_len, records_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) struct btf_ext_info_sec *sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) const char *info_sec_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) __u64 remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) record_size = ext_info->rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) sinfo = ext_info->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) remain_len = ext_info->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) while (remain_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) records_len = sinfo->num_info * record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) if (strcmp(info_sec_name, sec_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) remain_len -= sec_hdrlen + records_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) sinfo = (void *)sinfo + sec_hdrlen + records_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) existing_len = (*cnt) * record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) data = realloc(*info, existing_len + records_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) memcpy(data + existing_len, sinfo->data, records_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) /* adjust insn_off only, the rest data will be passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) * to the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) for (i = 0; i < sinfo->num_info; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) __u32 *insn_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) insn_off = data + existing_len + (i * record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) *insn_off = *insn_off / sizeof(struct bpf_insn) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) insns_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) *cnt += sinfo->num_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) int btf_ext__reloc_func_info(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) const char *sec_name, __u32 insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) void **func_info, __u32 *cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) insns_cnt, func_info, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) int btf_ext__reloc_line_info(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) const char *sec_name, __u32 insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) void **line_info, __u32 *cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) insns_cnt, line_info, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) return btf_ext->func_info.rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) return btf_ext->line_info.rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) struct btf_dedup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) const struct btf_dedup_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) static void btf_dedup_free(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) static int btf_dedup_strings(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) static int btf_dedup_prim_types(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) static int btf_dedup_struct_types(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) static int btf_dedup_ref_types(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) static int btf_dedup_compact_types(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) static int btf_dedup_remap_types(struct btf_dedup *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) * Deduplicate BTF types and strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) * section with all BTF type descriptors and string data. It overwrites that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) * memory in-place with deduplicated types and strings without any loss of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) * is provided, all the strings referenced from .BTF.ext section are honored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) * and updated to point to the right offsets after deduplication.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) * If function returns with error, type/string data might be garbled and should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) * be discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) * More verbose and detailed description of both problem btf_dedup is solving,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) * as well as solution could be found at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) * Problem description and justification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) * =====================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) * BTF type information is typically emitted either as a result of conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) * from DWARF to BTF or directly by compiler. In both cases, each compilation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) * unit contains information about a subset of all the types that are used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) * in an application. These subsets are frequently overlapping and contain a lot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) * of duplicated information when later concatenated together into a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) * binary. This algorithm ensures that each unique type is represented by single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) * BTF type descriptor, greatly reducing resulting size of BTF data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) * Compilation unit isolation and subsequent duplication of data is not the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) * problem. The same type hierarchy (e.g., struct and all the type that struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) * references) in different compilation units can be represented in BTF to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) * various degrees of completeness (or, rather, incompleteness) due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) * struct/union forward declarations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) * Let's take a look at an example, that we'll use to better understand the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) * problem (and solution). Suppose we have two compilation units, each using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) * same `struct S`, but each of them having incomplete type information about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) * struct's fields:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) * // CU #1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) * struct S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) * struct A {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) * int a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) * struct A* self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) * struct S* parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) * struct B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) * struct S {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) * struct A* a_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) * struct B* b_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) * // CU #2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) * struct S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) * struct A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) * struct B {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) * int b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) * struct B* self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) * struct S* parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) * struct S {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) * struct A* a_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) * struct B* b_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) * In case of CU #1, BTF data will know only that `struct B` exist (but no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) * more), but will know the complete type information about `struct A`. While
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) * for CU #2, it will know full type information about `struct B`, but will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) * only know about forward declaration of `struct A` (in BTF terms, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) * have `BTF_KIND_FWD` type descriptor with name `B`).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) * This compilation unit isolation means that it's possible that there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) * single CU with complete type information describing structs `S`, `A`, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) * `B`. Also, we might get tons of duplicated and redundant type information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) * Additional complication we need to keep in mind comes from the fact that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) * types, in general, can form graphs containing cycles, not just DAGs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) * While algorithm does deduplication, it also merges and resolves type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) * information (unless disabled throught `struct btf_opts`), whenever possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) * E.g., in the example above with two compilation units having partial type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) * information for structs `A` and `B`, the output of algorithm will emit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) * a single copy of each BTF type that describes structs `A`, `B`, and `S`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) * (as well as type information for `int` and pointers), as if they were defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) * in a single compilation unit as:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) * struct A {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) * int a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) * struct A* self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) * struct S* parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) * struct B {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) * int b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) * struct B* self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) * struct S* parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) * struct S {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) * struct A* a_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) * struct B* b_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) * Algorithm summary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) * =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) * Algorithm completes its work in 6 separate passes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) * 1. Strings deduplication.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) * 2. Primitive types deduplication (int, enum, fwd).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) * 3. Struct/union types deduplication.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) * protos, and const/volatile/restrict modifiers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) * 5. Types compaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) * 6. Types remapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) * Algorithm determines canonical type descriptor, which is a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) * representative type for each truly unique type. This canonical type is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) * one that will go into final deduplicated BTF type information. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) * struct/unions, it is also the type that algorithm will merge additional type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) * information into (while resolving FWDs), as it discovers it from data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) * other CUs. Each input BTF type eventually gets either mapped to itself, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) * that type is canonical, or to some other type, if that type is equivalent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) * and was chosen as canonical representative. This mapping is stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) * FWD type got resolved to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) * To facilitate fast discovery of canonical types, we also maintain canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) * that match that signature. With sufficiently good choice of type signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) * hashing function, we can limit number of canonical types for each unique type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) * signature to a very small number, allowing to find canonical type for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) * duplicated type very quickly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) * Struct/union deduplication is the most critical part and algorithm for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) * deduplicating structs/unions is described in greater details in comments for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) * `btf_dedup_is_equiv` function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) const struct btf_dedup_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) if (IS_ERR(d)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) if (btf_ensure_modifiable(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) err = btf_dedup_strings(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) pr_debug("btf_dedup_strings failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) err = btf_dedup_prim_types(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) pr_debug("btf_dedup_prim_types failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) err = btf_dedup_struct_types(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) pr_debug("btf_dedup_struct_types failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) err = btf_dedup_ref_types(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) pr_debug("btf_dedup_ref_types failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) err = btf_dedup_compact_types(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) pr_debug("btf_dedup_compact_types failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) err = btf_dedup_remap_types(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) pr_debug("btf_dedup_remap_types failed:%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) btf_dedup_free(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) #define BTF_UNPROCESSED_ID ((__u32)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) #define BTF_IN_PROGRESS_ID ((__u32)-2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) struct btf_dedup {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) /* .BTF section to be deduped in-place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) struct btf *btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) * Optional .BTF.ext section. When provided, any strings referenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) * from it will be taken into account when deduping strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) struct btf_ext *btf_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) * This is a map from any type's signature hash to a list of possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) * canonical representative type candidates. Hash collisions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) * ignored, so even types of various kinds can share same list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) * candidates, which is fine because we rely on subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) * btf_xxx_equal() checks to authoritatively verify type equality.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) struct hashmap *dedup_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) /* Canonical types map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) __u32 *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) /* Hypothetical mapping, used during type graph equivalence checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) __u32 *hypot_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) __u32 *hypot_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) size_t hypot_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) size_t hypot_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) /* Various option modifying behavior of algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) struct btf_dedup_opts opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) struct btf_str_ptr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) __u32 new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) bool used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) struct btf_str_ptrs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) struct btf_str_ptr *ptrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) const char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) __u32 cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) __u32 cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) static long hash_combine(long h, long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) return h * 31 + value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) #define for_each_dedup_cand(d, node, hash) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) return hashmap__append(d->dedup_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) (void *)hash, (void *)(long)type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) static int btf_dedup_hypot_map_add(struct btf_dedup *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) __u32 from_id, __u32 to_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) if (d->hypot_cnt == d->hypot_cap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) __u32 *new_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) if (!new_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) d->hypot_list = new_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) d->hypot_list[d->hypot_cnt++] = from_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) d->hypot_map[from_id] = to_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) for (i = 0; i < d->hypot_cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) d->hypot_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) static void btf_dedup_free(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) hashmap__free(d->dedup_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) d->dedup_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) free(d->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) d->map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) free(d->hypot_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) d->hypot_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) free(d->hypot_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) d->hypot_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) free(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) return (size_t)key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) return k1 == k2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) const struct btf_dedup_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) /* dedup_table_size is now used only to force collisions in tests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) if (opts && opts->dedup_table_size == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) hash_fn = btf_dedup_collision_hash_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) d->btf = btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) d->btf_ext = btf_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) if (IS_ERR(d->dedup_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) err = PTR_ERR(d->dedup_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) d->dedup_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) if (!d->map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) /* special BTF "void" type is made canonical immediately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) d->map[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) for (i = 1; i <= btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) struct btf_type *t = btf_type_by_id(d->btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) /* VAR and DATASEC are never deduped and are self-canonical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) if (btf_is_var(t) || btf_is_datasec(t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) d->map[i] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) d->map[i] = BTF_UNPROCESSED_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) if (!d->hypot_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) for (i = 0; i <= btf->nr_types; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) d->hypot_map[i] = BTF_UNPROCESSED_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) btf_dedup_free(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) * Iterate over all possible places in .BTF and .BTF.ext that can reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) * string and pass pointer to it to a provided callback `fn`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) void *line_data_cur, *line_data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) int i, j, r, rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) t = btf_type_by_id(d->btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) r = fn(&t->name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) case BTF_KIND_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) struct btf_member *m = btf_members(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) for (j = 0; j < vlen; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) r = fn(&m->name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) case BTF_KIND_ENUM: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) struct btf_enum *m = btf_enum(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) for (j = 0; j < vlen; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) r = fn(&m->name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) case BTF_KIND_FUNC_PROTO: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) struct btf_param *m = btf_params(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) for (j = 0; j < vlen; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) r = fn(&m->name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) if (!d->btf_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) line_data_cur = d->btf_ext->line_info.info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) rec_size = d->btf_ext->line_info.rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) while (line_data_cur < line_data_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) struct btf_ext_info_sec *sec = line_data_cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) struct bpf_line_info_min *line_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) __u32 num_info = sec->num_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) r = fn(&sec->sec_name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) line_data_cur += sizeof(struct btf_ext_info_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) for (i = 0; i < num_info; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) line_info = line_data_cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) r = fn(&line_info->file_name_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) r = fn(&line_info->line_off, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) line_data_cur += rec_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) static int str_sort_by_content(const void *a1, const void *a2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) const struct btf_str_ptr *p1 = a1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) const struct btf_str_ptr *p2 = a2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) return strcmp(p1->str, p2->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) static int str_sort_by_offset(const void *a1, const void *a2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) const struct btf_str_ptr *p1 = a1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) const struct btf_str_ptr *p2 = a2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) if (p1->str != p2->str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) return p1->str < p2->str ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) const struct btf_str_ptr *p = pelem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) if (str_ptr != p->str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) return (const char *)str_ptr < p->str ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) struct btf_str_ptrs *strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) struct btf_str_ptr *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) if (*str_off_ptr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) strs = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) s->used = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) struct btf_str_ptrs *strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) struct btf_str_ptr *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) if (*str_off_ptr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) strs = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) *str_off_ptr = s->new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) * Dedup string and filter out those that are not referenced from either .BTF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) * or .BTF.ext (if provided) sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) * This is done by building index of all strings in BTF's string section,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) * then iterating over all entities that can reference strings (e.g., type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) * names, struct field names, .BTF.ext line info, etc) and marking corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) * strings as used. After that all used strings are deduped and compacted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) * sequential blob of memory and new offsets are calculated. Then all the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) * references are iterated again and rewritten using new offsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) static int btf_dedup_strings(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) char *start = d->btf->strs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) char *end = start + d->btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) char *p = start, *tmp_strs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) struct btf_str_ptrs strs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) .cnt = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) .cap = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) .ptrs = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) .data = start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) int i, j, err = 0, grp_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) bool grp_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) if (d->btf->strs_deduped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) /* build index of all strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) while (p < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) if (strs.cnt + 1 > strs.cap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) struct btf_str_ptr *new_ptrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) strs.cap += max(strs.cnt / 2, 16U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) new_ptrs = libbpf_reallocarray(strs.ptrs, strs.cap, sizeof(strs.ptrs[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) if (!new_ptrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) strs.ptrs = new_ptrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) strs.ptrs[strs.cnt].str = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) strs.ptrs[strs.cnt].used = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) p += strlen(p) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) strs.cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) /* temporary storage for deduplicated strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) tmp_strs = malloc(d->btf->hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) if (!tmp_strs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) /* mark all used strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) strs.ptrs[0].used = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) /* sort strings by context, so that we can identify duplicates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) * iterate groups of equal strings and if any instance in a group was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) * referenced, emit single instance and remember new offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) p = tmp_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) grp_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) grp_used = strs.ptrs[0].used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) /* iterate past end to avoid code duplication after loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) for (i = 1; i <= strs.cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) * when i == strs.cnt, we want to skip string comparison and go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) * straight to handling last group of strings (otherwise we'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) * need to handle last group after the loop w/ duplicated code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) if (i < strs.cnt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) grp_used = grp_used || strs.ptrs[i].used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) * this check would have been required after the loop to handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) * last group of strings, but due to <= condition in a loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) * we avoid that duplication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) if (grp_used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) int new_off = p - tmp_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) __u32 len = strlen(strs.ptrs[grp_idx].str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) memmove(p, strs.ptrs[grp_idx].str, len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) for (j = grp_idx; j < i; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) strs.ptrs[j].new_off = new_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) p += len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) if (i < strs.cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) grp_idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) grp_used = strs.ptrs[i].used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) /* replace original strings with deduped ones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) d->btf->hdr->str_len = p - tmp_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) memmove(start, tmp_strs, d->btf->hdr->str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) end = start + d->btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) /* restore original order for further binary search lookups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) /* remap string offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) d->btf->hdr->str_len = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) d->btf->strs_deduped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) free(tmp_strs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) free(strs.ptrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) static long btf_hash_common(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) h = hash_combine(0, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) h = hash_combine(h, t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) h = hash_combine(h, t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) return t1->name_off == t2->name_off &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) t1->info == t2->info &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) t1->size == t2->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) /* Calculate type signature hash of INT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) static long btf_hash_int(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) __u32 info = *(__u32 *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) h = hash_combine(h, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) /* Check structural equality of two INTs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) __u32 info1, info2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) info1 = *(__u32 *)(t1 + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) info2 = *(__u32 *)(t2 + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) return info1 == info2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) /* Calculate type signature hash of ENUM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) static long btf_hash_enum(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) /* don't hash vlen and enum members to support enum fwd resolving */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) h = hash_combine(0, t->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) h = hash_combine(h, t->info & ~0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) h = hash_combine(h, t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) /* Check structural equality of two ENUMs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) const struct btf_enum *m1, *m2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) vlen = btf_vlen(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) m1 = btf_enum(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) m2 = btf_enum(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) if (m1->name_off != m2->name_off || m1->val != m2->val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) m1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) m2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) static inline bool btf_is_enum_fwd(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) return btf_is_enum(t) && btf_vlen(t) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) return btf_equal_enum(t1, t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) /* ignore vlen when comparing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) return t1->name_off == t2->name_off &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) t1->size == t2->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) * as referenced type IDs equivalence is established separately during type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) * graph equivalence check algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) static long btf_hash_struct(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) const struct btf_member *member = btf_members(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) __u32 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) long h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) h = hash_combine(h, member->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) h = hash_combine(h, member->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) /* no hashing of referenced type ID, it can be unresolved yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) member++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) * IDs. This check is performed during type graph equivalence check and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) * referenced types equivalence is checked separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) const struct btf_member *m1, *m2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) vlen = btf_vlen(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) m1 = btf_members(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) m2 = btf_members(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) if (m1->name_off != m2->name_off || m1->offset != m2->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) m1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) m2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) * Calculate type signature hash of ARRAY, including referenced type IDs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) * under assumption that they were already resolved to canonical type IDs and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) * are not going to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) static long btf_hash_array(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) const struct btf_array *info = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) long h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) h = hash_combine(h, info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) h = hash_combine(h, info->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) h = hash_combine(h, info->nelems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) * Check exact equality of two ARRAYs, taking into account referenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) * type IDs, under assumption that they were already resolved to canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) * type IDs and are not going to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) * This function is called during reference types deduplication to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) * ARRAY to potential canonical representative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) const struct btf_array *info1, *info2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) info1 = btf_array(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) info2 = btf_array(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) return info1->type == info2->type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) info1->index_type == info2->index_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) info1->nelems == info2->nelems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) * Check structural compatibility of two ARRAYs, ignoring referenced type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) * IDs. This check is performed during type graph equivalence check and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) * referenced types equivalence is checked separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) return btf_array(t1)->nelems == btf_array(t2)->nelems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) * under assumption that they were already resolved to canonical type IDs and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) * are not going to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) static long btf_hash_fnproto(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) const struct btf_param *member = btf_params(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) long h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) h = hash_combine(h, member->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) h = hash_combine(h, member->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) member++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) return h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) * Check exact equality of two FUNC_PROTOs, taking into account referenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) * type IDs, under assumption that they were already resolved to canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) * type IDs and are not going to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) * This function is called during reference types deduplication to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) * FUNC_PROTO to potential canonical representative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) const struct btf_param *m1, *m2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) if (!btf_equal_common(t1, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) vlen = btf_vlen(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) m1 = btf_params(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) m2 = btf_params(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) if (m1->name_off != m2->name_off || m1->type != m2->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) m1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) m2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) * IDs. This check is performed during type graph equivalence check and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) * referenced types equivalence is checked separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) const struct btf_param *m1, *m2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) /* skip return type ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) if (t1->name_off != t2->name_off || t1->info != t2->info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) vlen = btf_vlen(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) m1 = btf_params(t1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) m2 = btf_params(t2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) if (m1->name_off != m2->name_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) m1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) m2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) * Deduplicate primitive types, that can't reference other types, by calculating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) * their type signature hash and comparing them with any possible canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) * candidate. If no canonical candidate matches, type itself is marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) * canonical and is added into `btf_dedup->dedup_table` as another candidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) struct btf_type *t = btf_type_by_id(d->btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) struct hashmap_entry *hash_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) struct btf_type *cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) /* if we don't find equivalent type, then we are canonical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) __u32 new_id = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) __u32 cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) case BTF_KIND_ARRAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) case BTF_KIND_UNION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) case BTF_KIND_FUNC_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) case BTF_KIND_VAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) case BTF_KIND_DATASEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) h = btf_hash_int(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) if (btf_equal_int(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) h = btf_hash_enum(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) if (btf_equal_enum(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) if (d->opts.dont_resolve_fwds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) if (btf_compat_enum(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) if (btf_is_enum_fwd(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) /* resolve fwd to full enum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) /* resolve canonical enum fwd to full enum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) d->map[cand_id] = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) case BTF_KIND_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) if (btf_equal_common(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) d->map[type_id] = new_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) static int btf_dedup_prim_types(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) err = btf_dedup_prim_type(d, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) * Check whether type is already mapped into canonical one (could be to itself).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) return d->map[type_id] <= BTF_MAX_NR_TYPES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) * Resolve type ID into its canonical type ID, if any; otherwise return original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) * STRUCT/UNION link and resolve it into canonical type ID as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) type_id = d->map[type_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) return type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) * type ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) __u32 orig_type_id = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) return type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) type_id = d->map[type_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) return type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) return orig_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) static inline __u16 btf_fwd_kind(struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) * Check equivalence of BTF type graph formed by candidate struct/union (we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) * call it "candidate graph" in this description for brevity) to a type graph
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) * formed by (potential) canonical struct/union ("canonical graph" for brevity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) * here, though keep in mind that not all types in canonical graph are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) * necessarily canonical representatives themselves, some of them might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) * duplicates or its uniqueness might not have been established yet).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) * - >0, if type graphs are equivalent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) * - 0, if not equivalent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) * - <0, on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) * Algorithm performs side-by-side DFS traversal of both type graphs and checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) * equivalence of BTF types at each step. If at any point BTF types in candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) * and canonical graphs are not compatible structurally, whole graphs are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) * incompatible. If types are structurally equivalent (i.e., all information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) * except referenced type IDs is exactly the same), a mapping from `canon_id` to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) * If a type references other types, then those referenced types are checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) * for equivalence recursively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) * During DFS traversal, if we find that for current `canon_id` type we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) * already have some mapping in hypothetical map, we check for two possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) * situations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) * happen when type graphs have cycles. In this case we assume those two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) * types are equivalent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) * - `canon_id` is mapped to different type. This is contradiction in our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) * hypothetical mapping, because same graph in canonical graph corresponds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) * to two different types in candidate graph, which for equivalent type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) * graphs shouldn't happen. This condition terminates equivalence check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) * with negative result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) * If type graphs traversal exhausts types to check and find no contradiction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) * then type graphs are equivalent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) * When checking types for equivalence, there is one special case: FWD types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) * If FWD type resolution is allowed and one of the types (either from canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) * flag) and their names match, hypothetical mapping is updated to point from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) * if there are two exactly named (or anonymous) structs/unions that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) * compatible structurally, one of which has FWD field, while other is concrete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) * STRUCT/UNION, but according to C sources they are different structs/unions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) * that are referencing different types with the same name. This is extremely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) * this logic is causing problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) * Doing FWD resolution means that both candidate and/or canonical graphs can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) * consists of portions of the graph that come from multiple compilation units.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) * This is due to the fact that types within single compilation unit are always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) * deduplicated and FWDs are already resolved, if referenced struct/union
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) * definiton is available. So, if we had unresolved FWD and found corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) * STRUCT/UNION, they will be from different compilation units. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) * type graph will likely have at least two different BTF types that describe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) * same type (e.g., most probably there will be two different BTF types for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) * same 'int' primitive type) and could even have "overlapping" parts of type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) * graph that describe same subset of types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) * This in turn means that our assumption that each type in canonical graph
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) * must correspond to exactly one type in candidate graph might not hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) * anymore and will make it harder to detect contradictions using hypothetical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) * resolution only in canonical graph. FWDs in candidate graphs are never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) * that can occur:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) * - Both types in canonical and candidate graphs are FWDs. If they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) * structurally equivalent, then they can either be both resolved to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) * same STRUCT/UNION or not resolved at all. In both cases they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) * equivalent and there is no need to resolve FWD on candidate side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) * so nothing to resolve as well, algorithm will check equivalence anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) * - Type in canonical graph is FWD, while type in candidate is concrete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) * STRUCT/UNION. In this case candidate graph comes from single compilation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) * unit, so there is exactly one BTF type for each unique C type. After
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) * resolving FWD into STRUCT/UNION, there might be more than one BTF type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) * in canonical graph mapping to single BTF type in candidate graph, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) * because hypothetical mapping maps from canonical to candidate types, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) * alright, and we still maintain the property of having single `canon_id`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) * mapping to single `cand_id` (there could be two different `canon_id`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) * mapped to the same `cand_id`, but it's not contradictory).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) * graph is FWD. In this case we are just going to check compatibility of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) * canonical graph.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) __u32 canon_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) struct btf_type *cand_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) struct btf_type *canon_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) __u32 hypot_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) __u16 cand_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) __u16 canon_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) int i, eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) /* if both resolve to the same canonical, they must be equivalent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) canon_id = resolve_fwd_id(d, canon_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) hypot_type_id = d->hypot_map[canon_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) if (hypot_type_id <= BTF_MAX_NR_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) return hypot_type_id == cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) cand_type = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) canon_type = btf_type_by_id(d->btf, canon_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) cand_kind = btf_kind(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) canon_kind = btf_kind(canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) if (cand_type->name_off != canon_type->name_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) /* FWD <--> STRUCT/UNION equivalence check, if enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) if (!d->opts.dont_resolve_fwds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) && cand_kind != canon_kind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) __u16 real_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) __u16 fwd_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) if (cand_kind == BTF_KIND_FWD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) real_kind = canon_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) fwd_kind = btf_fwd_kind(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) real_kind = cand_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) fwd_kind = btf_fwd_kind(canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) return fwd_kind == real_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) if (cand_kind != canon_kind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) switch (cand_kind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) return btf_equal_int(cand_type, canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) if (d->opts.dont_resolve_fwds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) return btf_equal_enum(cand_type, canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) return btf_compat_enum(cand_type, canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) case BTF_KIND_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) return btf_equal_common(cand_type, canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) if (cand_type->info != canon_type->info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) case BTF_KIND_ARRAY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) const struct btf_array *cand_arr, *canon_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) if (!btf_compat_array(cand_type, canon_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) cand_arr = btf_array(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) canon_arr = btf_array(canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) eq = btf_dedup_is_equiv(d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) cand_arr->index_type, canon_arr->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) if (eq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) return eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) case BTF_KIND_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) const struct btf_member *cand_m, *canon_m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) if (!btf_shallow_equal_struct(cand_type, canon_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) vlen = btf_vlen(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) cand_m = btf_members(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) canon_m = btf_members(canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) if (eq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) return eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) cand_m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) canon_m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) case BTF_KIND_FUNC_PROTO: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) const struct btf_param *cand_p, *canon_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) if (!btf_compat_fnproto(cand_type, canon_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) if (eq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) return eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) vlen = btf_vlen(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) cand_p = btf_params(cand_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) canon_p = btf_params(canon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) if (eq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) return eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) cand_p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) canon_p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) * Use hypothetical mapping, produced by successful type graph equivalence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) * check, to augment existing struct/union canonical mapping, where possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) * it doesn't matter if FWD type was part of canonical graph or candidate one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) * we are recording the mapping anyway. As opposed to carefulness required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) * for struct/union correspondence mapping (described below), for FWD resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) * it's not important, as by the time that FWD type (reference type) will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) * deduplicated all structs/unions will be deduped already anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) * Recording STRUCT/UNION mapping is purely a performance optimization and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) * not required for correctness. It needs to be done carefully to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) * struct/union from candidate's type graph is not mapped into corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) * struct/union from canonical type graph that itself hasn't been resolved into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) * canonical representative. The only guarantee we have is that canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) * struct/union was determined as canonical and that won't change. But any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) * types referenced through that struct/union fields could have been not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) * resolved, so in case like that it's too early to establish any kind of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) * correspondence between structs/unions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) * No canonical correspondence is derived for primitive types (they are already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) * deduplicated completely already anyway) or reference types (they rely on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) * stability of struct/union canonical relationship for equivalence checks).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) __u32 cand_type_id, targ_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) __u16 t_kind, c_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) __u32 t_id, c_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) for (i = 0; i < d->hypot_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) cand_type_id = d->hypot_list[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) targ_type_id = d->hypot_map[cand_type_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) t_id = resolve_type_id(d, targ_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) c_id = resolve_type_id(d, cand_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) t_kind = btf_kind(btf__type_by_id(d->btf, t_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) c_kind = btf_kind(btf__type_by_id(d->btf, c_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) * Resolve FWD into STRUCT/UNION.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) * It's ok to resolve FWD into STRUCT/UNION that's not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) * mapped to canonical representative (as opposed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) * eventually that struct is going to be mapped and all resolved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) * FWDs will automatically resolve to correct canonical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) * representative. This will happen before ref type deduping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) * which critically depends on stability of these mapping. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) * stability is not a requirement for STRUCT/UNION equivalence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) * checks, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) d->map[c_id] = t_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) d->map[t_id] = c_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) c_kind != BTF_KIND_FWD &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) is_type_mapped(d, c_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) !is_type_mapped(d, t_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) * as a perf optimization, we can map struct/union
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) * that's part of type graph we just verified for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) * equivalence. We can do that for struct/union that has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) * canonical representative only, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) d->map[t_id] = c_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) * Deduplicate struct/union types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) * For each struct/union type its type signature hash is calculated, taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) * into account type's name, size, number, order and names of fields, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) * ignoring type ID's referenced from fields, because they might not be deduped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) * completely until after reference types deduplication phase. This type hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) * is used to iterate over all potential canonical types, sharing same hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) * For each canonical candidate we check whether type graphs that they form
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) * (through referenced types in fields and so on) are equivalent using algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) * potentially map other structs/unions to their canonical representatives,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) * if such relationship hasn't yet been established. This speeds up algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) * by eliminating some of the duplicate work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) * If no matching canonical representative was found, struct/union is marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) * as canonical for itself and is added into btf_dedup->dedup_table hash map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) * for further look ups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) struct btf_type *cand_type, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) struct hashmap_entry *hash_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) /* if we don't find equivalent type, then we are canonical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) __u32 new_id = type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) __u16 kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) /* already deduped or is in process of deduping (loop detected) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) if (d->map[type_id] <= BTF_MAX_NR_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) t = btf_type_by_id(d->btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) kind = btf_kind(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) h = btf_hash_struct(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) __u32 cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) int eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) * Even though btf_dedup_is_equiv() checks for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) * btf_shallow_equal_struct() internally when checking two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) * structs (unions) for equivalence, we need to guard here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) * from picking matching FWD type as a dedup candidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) * This can happen due to hash collision. In such case just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) * relying on btf_dedup_is_equiv() would lead to potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) * FWD and compatible STRUCT/UNION are considered equivalent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) cand_type = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) if (!btf_shallow_equal_struct(t, cand_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) btf_dedup_clear_hypot_map(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) eq = btf_dedup_is_equiv(d, type_id, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) if (eq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) return eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) if (!eq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) btf_dedup_merge_hypot_map(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) d->map[type_id] = new_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) static int btf_dedup_struct_types(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) err = btf_dedup_struct_type(d, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) * Deduplicate reference type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) * Once all primitive and struct/union types got deduplicated, we can easily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) * deduplicate all other (reference) BTF types. This is done in two steps:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) * 1. Resolve all referenced type IDs into their canonical type IDs. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) * resolution can be done either immediately for primitive or struct/union types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) * (because they were deduped in previous two phases) or recursively for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) * reference types. Recursion will always terminate at either primitive or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) * struct/union type, at which point we can "unwind" chain of reference types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) * one by one. There is no danger of encountering cycles because in C type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) * system the only way to form type cycle is through struct/union, so any chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) * of reference types, even those taking part in a type cycle, will inevitably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) * reach struct/union at some point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) * becomes "stable", in the sense that no further deduplication will cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) * any changes to it. With that, it's now possible to calculate type's signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) * hash (this time taking into account referenced type IDs) and loop over all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) * potential canonical representatives. If no match was found, current type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) * will become canonical representative of itself and will be added into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) * btf_dedup->dedup_table as another possible canonical representative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) struct hashmap_entry *hash_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) __u32 new_id = type_id, cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) struct btf_type *t, *cand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) /* if we don't find equivalent type, then we are representative type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) int ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) long h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) if (d->map[type_id] == BTF_IN_PROGRESS_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) return -ELOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) if (d->map[type_id] <= BTF_MAX_NR_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) return resolve_type_id(d, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) t = btf_type_by_id(d->btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) d->map[type_id] = BTF_IN_PROGRESS_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) ref_type_id = btf_dedup_ref_type(d, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) if (ref_type_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) return ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) t->type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) h = btf_hash_common(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) if (btf_equal_common(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) case BTF_KIND_ARRAY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) struct btf_array *info = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) ref_type_id = btf_dedup_ref_type(d, info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) if (ref_type_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) return ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) info->type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) ref_type_id = btf_dedup_ref_type(d, info->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) if (ref_type_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) return ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) info->index_type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) h = btf_hash_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) if (btf_equal_array(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) case BTF_KIND_FUNC_PROTO: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) struct btf_param *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) __u16 vlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) ref_type_id = btf_dedup_ref_type(d, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) if (ref_type_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) return ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) t->type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) param = btf_params(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) ref_type_id = btf_dedup_ref_type(d, param->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) if (ref_type_id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) return ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) param->type = ref_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) param++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) h = btf_hash_fnproto(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) for_each_dedup_cand(d, hash_entry, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) cand_id = (__u32)(long)hash_entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) cand = btf_type_by_id(d->btf, cand_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) if (btf_equal_fnproto(t, cand)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) new_id = cand_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) d->map[type_id] = new_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) return new_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) static int btf_dedup_ref_types(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) err = btf_dedup_ref_type(d, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) /* we won't need d->dedup_table anymore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) hashmap__free(d->dedup_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) d->dedup_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) * Compact types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) * After we established for each type its corresponding canonical representative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) * type, we now can eliminate types that are not canonical and leave only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) * canonical ones layed out sequentially in memory by copying them over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) * duplicates. During compaction btf_dedup->hypot_map array is reused to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) * a map from original type ID to a new compacted type ID, which will be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) * during next phase to "fix up" type IDs, referenced from struct/union and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) * reference types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) static int btf_dedup_compact_types(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) __u32 *new_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) __u32 next_type_id = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) /* we are going to reuse hypot_map to store compaction remapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) d->hypot_map[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) for (i = 1; i <= d->btf->nr_types; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) d->hypot_map[i] = BTF_UNPROCESSED_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) p = d->btf->types_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) if (d->map[i] != i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) len = btf_type_size(btf__type_by_id(d->btf, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) memmove(p, btf__type_by_id(d->btf, i), len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) d->hypot_map[i] = next_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) d->btf->type_offs[next_type_id] = p - d->btf->types_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) p += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) next_type_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) /* shrink struct btf's internal types index and update btf_header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) d->btf->nr_types = next_type_id - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) d->btf->type_offs_cap = d->btf->nr_types + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) d->btf->hdr->type_len = p - d->btf->types_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) sizeof(*new_offs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) if (!new_offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) d->btf->type_offs = new_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) d->btf->hdr->str_off = d->btf->hdr->type_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) * Figure out final (deduplicated and compacted) type ID for provided original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) * `type_id` by first resolving it into corresponding canonical type ID and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) * which is populated during compaction phase.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) __u32 resolved_type_id, new_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) resolved_type_id = resolve_type_id(d, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) new_type_id = d->hypot_map[resolved_type_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) if (new_type_id > BTF_MAX_NR_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) return new_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) * Remap referenced type IDs into deduped type IDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) * After BTF types are deduplicated and compacted, their final type IDs may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) * differ from original ones. The map from original to a corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) * deduped type ID is stored in btf_dedup->hypot_map and is populated during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) * compaction phase. During remapping phase we are rewriting all type IDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) * their final deduped type IDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) struct btf_type *t = btf_type_by_id(d->btf, type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) int i, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) switch (btf_kind(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) case BTF_KIND_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) case BTF_KIND_ENUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) case BTF_KIND_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) case BTF_KIND_FUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) case BTF_KIND_VAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) r = btf_dedup_remap_type_id(d, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) t->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) case BTF_KIND_ARRAY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) struct btf_array *arr_info = btf_array(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) r = btf_dedup_remap_type_id(d, arr_info->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) arr_info->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) r = btf_dedup_remap_type_id(d, arr_info->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) arr_info->index_type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) case BTF_KIND_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) struct btf_member *member = btf_members(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) r = btf_dedup_remap_type_id(d, member->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) member->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) member++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) case BTF_KIND_FUNC_PROTO: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) struct btf_param *param = btf_params(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) r = btf_dedup_remap_type_id(d, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) t->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) r = btf_dedup_remap_type_id(d, param->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) param->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) param++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) case BTF_KIND_DATASEC: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) struct btf_var_secinfo *var = btf_var_secinfos(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) __u16 vlen = btf_vlen(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) for (i = 0; i < vlen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) r = btf_dedup_remap_type_id(d, var->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) var->type = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) var++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) static int btf_dedup_remap_types(struct btf_dedup *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) int i, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) for (i = 1; i <= d->btf->nr_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) r = btf_dedup_remap_type(d, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) * Probe few well-known locations for vmlinux kernel image and try to load BTF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) * data out of it to use for target BTF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) struct btf *libbpf_find_kernel_btf(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) const char *path_fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) bool raw_btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) } locations[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) /* try canonical vmlinux BTF through sysfs first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) /* fall back to trying to find vmlinux ELF on disk otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) { "/boot/vmlinux-%1$s" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) { "/lib/modules/%1$s/vmlinux-%1$s" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) { "/lib/modules/%1$s/build/vmlinux" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) { "/usr/lib/modules/%1$s/kernel/vmlinux" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) { "/usr/lib/debug/boot/vmlinux-%1$s" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) char path[PATH_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) struct utsname buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) struct btf *btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) uname(&buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) for (i = 0; i < ARRAY_SIZE(locations); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) if (access(path, R_OK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) if (locations[i].raw_btf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) btf = btf__parse_raw(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) btf = btf__parse_elf(path, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) pr_debug("loading kernel BTF '%s': %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) if (IS_ERR(btf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) return btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) pr_warn("failed to find valid kernel BTF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) return ERR_PTR(-ESRCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) }