1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
13 #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
14 #define max(a, b) ((a) > (b) ? (a) : (b))
15 #define min(a, b) ((a) < (b) ? (a) : (b))
17 #define BTF_MAX_NR_TYPES 65535
19 #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
20 ((k) == BTF_KIND_VOLATILE) || \
21 ((k) == BTF_KIND_CONST) || \
22 ((k) == BTF_KIND_RESTRICT))
24 static struct btf_type btf_void;
28 struct btf_header *hdr;
31 struct btf_type **types;
40 static int btf_add_type(struct btf *btf, struct btf_type *t)
42 if (btf->types_size - btf->nr_types < 2) {
43 struct btf_type **new_types;
44 __u32 expand_by, new_size;
46 if (btf->types_size == BTF_MAX_NR_TYPES)
49 expand_by = max(btf->types_size >> 2, 16);
50 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
52 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
56 if (btf->nr_types == 0)
57 new_types[0] = &btf_void;
59 btf->types = new_types;
60 btf->types_size = new_size;
63 btf->types[++(btf->nr_types)] = t;
68 static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
70 const struct btf_header *hdr = btf->hdr;
73 if (btf->data_size < sizeof(struct btf_header)) {
74 elog("BTF header not found\n");
78 if (hdr->magic != BTF_MAGIC) {
79 elog("Invalid BTF magic:%x\n", hdr->magic);
83 if (hdr->version != BTF_VERSION) {
84 elog("Unsupported BTF version:%u\n", hdr->version);
89 elog("Unsupported BTF flags:%x\n", hdr->flags);
93 meta_left = btf->data_size - sizeof(*hdr);
95 elog("BTF has no data\n");
99 if (meta_left < hdr->type_off) {
100 elog("Invalid BTF type section offset:%u\n", hdr->type_off);
104 if (meta_left < hdr->str_off) {
105 elog("Invalid BTF string section offset:%u\n", hdr->str_off);
109 if (hdr->type_off >= hdr->str_off) {
110 elog("BTF type section offset >= string section offset. No type?\n");
114 if (hdr->type_off & 0x02) {
115 elog("BTF type section is not aligned to 4 bytes\n");
119 btf->nohdr_data = btf->hdr + 1;
124 static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
126 const struct btf_header *hdr = btf->hdr;
127 const char *start = btf->nohdr_data + hdr->str_off;
128 const char *end = start + btf->hdr->str_len;
130 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
131 start[0] || end[-1]) {
132 elog("Invalid BTF string section\n");
136 btf->strings = start;
141 static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
143 struct btf_header *hdr = btf->hdr;
144 void *nohdr_data = btf->nohdr_data;
145 void *next_type = nohdr_data + hdr->type_off;
146 void *end_type = nohdr_data + hdr->str_off;
148 while (next_type < end_type) {
149 struct btf_type *t = next_type;
150 __u16 vlen = BTF_INFO_VLEN(t->info);
153 next_type += sizeof(*t);
154 switch (BTF_INFO_KIND(t->info)) {
156 next_type += sizeof(int);
159 next_type += sizeof(struct btf_array);
161 case BTF_KIND_STRUCT:
163 next_type += vlen * sizeof(struct btf_member);
166 next_type += vlen * sizeof(struct btf_enum);
168 case BTF_KIND_TYPEDEF:
171 case BTF_KIND_VOLATILE:
173 case BTF_KIND_RESTRICT:
176 elog("Unsupported BTF_KIND:%u\n",
177 BTF_INFO_KIND(t->info));
181 err = btf_add_type(btf, t);
189 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
191 if (type_id > btf->nr_types)
194 return btf->types[type_id];
197 static bool btf_type_is_void(const struct btf_type *t)
199 return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
202 static bool btf_type_is_void_or_null(const struct btf_type *t)
204 return !t || btf_type_is_void(t);
207 static __s64 btf_type_size(const struct btf_type *t)
209 switch (BTF_INFO_KIND(t->info)) {
211 case BTF_KIND_STRUCT:
216 return sizeof(void *);
222 #define MAX_RESOLVE_DEPTH 32
224 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
226 const struct btf_array *array;
227 const struct btf_type *t;
232 t = btf__type_by_id(btf, type_id);
233 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
235 size = btf_type_size(t);
239 switch (BTF_INFO_KIND(t->info)) {
240 case BTF_KIND_TYPEDEF:
241 case BTF_KIND_VOLATILE:
243 case BTF_KIND_RESTRICT:
247 array = (const struct btf_array *)(t + 1);
248 if (nelems && array->nelems > UINT32_MAX / nelems)
250 nelems *= array->nelems;
251 type_id = array->type;
257 t = btf__type_by_id(btf, type_id);
263 if (nelems && size > UINT32_MAX / nelems)
266 return nelems * size;
269 int btf__resolve_type(const struct btf *btf, __u32 type_id)
271 const struct btf_type *t;
274 t = btf__type_by_id(btf, type_id);
275 while (depth < MAX_RESOLVE_DEPTH &&
276 !btf_type_is_void_or_null(t) &&
277 IS_MODIFIER(BTF_INFO_KIND(t->info))) {
279 t = btf__type_by_id(btf, type_id);
283 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
289 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
293 if (!strcmp(type_name, "void"))
296 for (i = 1; i <= btf->nr_types; i++) {
297 const struct btf_type *t = btf->types[i];
298 const char *name = btf__name_by_offset(btf, t->name_off);
300 if (name && !strcmp(type_name, name))
307 void btf__free(struct btf *btf)
320 struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log)
322 __u32 log_buf_size = 0;
323 char *log_buf = NULL;
327 btf = calloc(1, sizeof(struct btf));
329 return ERR_PTR(-ENOMEM);
334 log_buf = malloc(BPF_LOG_BUF_SIZE);
340 log_buf_size = BPF_LOG_BUF_SIZE;
343 btf->data = malloc(size);
349 memcpy(btf->data, data, size);
350 btf->data_size = size;
352 btf->fd = bpf_load_btf(btf->data, btf->data_size,
353 log_buf, log_buf_size, false);
357 elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
358 if (log_buf && *log_buf)
359 elog("%s\n", log_buf);
363 err = btf_parse_hdr(btf, err_log);
367 err = btf_parse_str_sec(btf, err_log);
371 err = btf_parse_type_sec(btf, err_log);
384 int btf__fd(const struct btf *btf)
389 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
391 if (offset < btf->hdr->str_len)
392 return &btf->strings[offset];