1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
10 #include <linux/btf.h>
14 #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
15 #define max(a, b) ((a) > (b) ? (a) : (b))
16 #define min(a, b) ((a) < (b) ? (a) : (b))
18 #define BTF_MAX_NR_TYPES 65535
20 static struct btf_type btf_void;
24 struct btf_header *hdr;
27 struct btf_type **types;
36 static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
38 if (!BTF_STR_TBL_ELF_ID(offset) &&
39 BTF_STR_OFFSET(offset) < btf->hdr->str_len)
40 return &btf->strings[BTF_STR_OFFSET(offset)];
45 static int btf_add_type(struct btf *btf, struct btf_type *t)
47 if (btf->types_size - btf->nr_types < 2) {
48 struct btf_type **new_types;
49 u32 expand_by, new_size;
51 if (btf->types_size == BTF_MAX_NR_TYPES)
54 expand_by = max(btf->types_size >> 2, 16);
55 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
57 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
61 if (btf->nr_types == 0)
62 new_types[0] = &btf_void;
64 btf->types = new_types;
65 btf->types_size = new_size;
68 btf->types[++(btf->nr_types)] = t;
73 static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
75 const struct btf_header *hdr = btf->hdr;
78 if (btf->data_size < sizeof(struct btf_header)) {
79 elog("BTF header not found\n");
83 if (hdr->magic != BTF_MAGIC) {
84 elog("Invalid BTF magic:%x\n", hdr->magic);
88 if (hdr->version != BTF_VERSION) {
89 elog("Unsupported BTF version:%u\n", hdr->version);
94 elog("Unsupported BTF flags:%x\n", hdr->flags);
98 meta_left = btf->data_size - sizeof(*hdr);
100 elog("BTF has no data\n");
104 if (meta_left < hdr->type_off) {
105 elog("Invalid BTF type section offset:%u\n", hdr->type_off);
109 if (meta_left < hdr->str_off) {
110 elog("Invalid BTF string section offset:%u\n", hdr->str_off);
114 if (hdr->type_off >= hdr->str_off) {
115 elog("BTF type section offset >= string section offset. No type?\n");
119 if (hdr->type_off & 0x02) {
120 elog("BTF type section is not aligned to 4 bytes\n");
124 btf->nohdr_data = btf->hdr + 1;
129 static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
131 const struct btf_header *hdr = btf->hdr;
132 const char *start = btf->nohdr_data + hdr->str_off;
133 const char *end = start + btf->hdr->str_len;
135 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
136 start[0] || end[-1]) {
137 elog("Invalid BTF string section\n");
141 btf->strings = start;
146 static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
148 struct btf_header *hdr = btf->hdr;
149 void *nohdr_data = btf->nohdr_data;
150 void *next_type = nohdr_data + hdr->type_off;
151 void *end_type = nohdr_data + hdr->str_off;
153 while (next_type < end_type) {
154 struct btf_type *t = next_type;
155 uint16_t vlen = BTF_INFO_VLEN(t->info);
158 next_type += sizeof(*t);
159 switch (BTF_INFO_KIND(t->info)) {
161 next_type += sizeof(int);
164 next_type += sizeof(struct btf_array);
166 case BTF_KIND_STRUCT:
168 next_type += vlen * sizeof(struct btf_member);
171 next_type += vlen * sizeof(struct btf_enum);
173 case BTF_KIND_TYPEDEF:
176 case BTF_KIND_VOLATILE:
178 case BTF_KIND_RESTRICT:
181 elog("Unsupported BTF_KIND:%u\n",
182 BTF_INFO_KIND(t->info));
186 err = btf_add_type(btf, t);
194 static const struct btf_type *btf_type_by_id(const struct btf *btf,
197 if (type_id > btf->nr_types)
200 return btf->types[type_id];
203 static bool btf_type_is_void(const struct btf_type *t)
205 return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
208 static bool btf_type_is_void_or_null(const struct btf_type *t)
210 return !t || btf_type_is_void(t);
213 static int64_t btf_type_size(const struct btf_type *t)
215 switch (BTF_INFO_KIND(t->info)) {
217 case BTF_KIND_STRUCT:
222 return sizeof(void *);
228 #define MAX_RESOLVE_DEPTH 32
230 int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id)
232 const struct btf_array *array;
233 const struct btf_type *t;
238 t = btf_type_by_id(btf, type_id);
239 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
241 size = btf_type_size(t);
245 switch (BTF_INFO_KIND(t->info)) {
246 case BTF_KIND_TYPEDEF:
247 case BTF_KIND_VOLATILE:
249 case BTF_KIND_RESTRICT:
253 array = (const struct btf_array *)(t + 1);
254 if (nelems && array->nelems > UINT32_MAX / nelems)
256 nelems *= array->nelems;
257 type_id = array->type;
263 t = btf_type_by_id(btf, type_id);
269 if (nelems && size > UINT32_MAX / nelems)
272 return nelems * size;
275 int32_t btf__find_by_name(const struct btf *btf, const char *type_name)
279 if (!strcmp(type_name, "void"))
282 for (i = 1; i <= btf->nr_types; i++) {
283 const struct btf_type *t = btf->types[i];
284 const char *name = btf_name_by_offset(btf, t->name);
286 if (name && !strcmp(type_name, name))
293 void btf__free(struct btf *btf)
306 struct btf *btf__new(uint8_t *data, uint32_t size,
307 btf_print_fn_t err_log)
309 uint32_t log_buf_size = 0;
310 char *log_buf = NULL;
314 btf = calloc(1, sizeof(struct btf));
316 return ERR_PTR(-ENOMEM);
321 log_buf = malloc(BPF_LOG_BUF_SIZE);
327 log_buf_size = BPF_LOG_BUF_SIZE;
330 btf->data = malloc(size);
336 memcpy(btf->data, data, size);
337 btf->data_size = size;
339 btf->fd = bpf_load_btf(btf->data, btf->data_size,
340 log_buf, log_buf_size, false);
344 elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
345 if (log_buf && *log_buf)
346 elog("%s\n", log_buf);
350 err = btf_parse_hdr(btf, err_log);
354 err = btf_parse_str_sec(btf, err_log);
358 err = btf_parse_type_sec(btf, err_log);
371 int btf__fd(const struct btf *btf)