1
2
3
4#include <byteswap.h>
5#include <endian.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <errno.h>
12#include <sys/utsname.h>
13#include <sys/param.h>
14#include <sys/stat.h>
15#include <linux/kernel.h>
16#include <linux/err.h>
17#include <linux/btf.h>
18#include <gelf.h>
19#include "btf.h"
20#include "bpf.h"
21#include "libbpf.h"
22#include "libbpf_internal.h"
23#include "hashmap.h"
24
25#define BTF_MAX_NR_TYPES 0x7fffffffU
26#define BTF_MAX_STR_OFFSET 0x7fffffffU
27
28static struct btf_type btf_void;
29
30struct btf {
31
32 void *raw_data;
33
34 void *raw_data_swapped;
35 __u32 raw_size;
36
37 bool swapped_endian;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 struct btf_header *hdr;
77
78 void *types_data;
79 size_t types_data_cap;
80
81
82
83
84
85
86 __u32 *type_offs;
87 size_t type_offs_cap;
88
89
90
91
92 __u32 nr_types;
93
94
95
96 struct btf *base_btf;
97
98
99
100
101 int start_id;
102
103
104
105
106 int start_str_off;
107
108 void *strs_data;
109 size_t strs_data_cap;
110
111
112 struct hashmap *strs_hash;
113
114 bool strs_deduped;
115
116
117
118
119
120
121 void **strs_data_ptr;
122
123
124 int fd;
125
126
127 int ptr_sz;
128};
129
130static inline __u64 ptr_to_u64(const void *ptr)
131{
132 return (__u64) (unsigned long) ptr;
133}
134
135
136
137
138
139
140
141
142
143
144
145void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
146 size_t cur_cnt, size_t max_cnt, size_t add_cnt)
147{
148 size_t new_cnt;
149 void *new_data;
150
151 if (cur_cnt + add_cnt <= *cap_cnt)
152 return *data + cur_cnt * elem_sz;
153
154
155 if (cur_cnt + add_cnt > max_cnt)
156 return NULL;
157
158 new_cnt = *cap_cnt;
159 new_cnt += new_cnt / 4;
160 if (new_cnt < 16)
161 new_cnt = 16;
162 if (new_cnt > max_cnt)
163 new_cnt = max_cnt;
164 if (new_cnt < cur_cnt + add_cnt)
165 new_cnt = cur_cnt + add_cnt;
166
167 new_data = libbpf_reallocarray(*data, new_cnt, elem_sz);
168 if (!new_data)
169 return NULL;
170
171
172 memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz);
173
174 *data = new_data;
175 *cap_cnt = new_cnt;
176 return new_data + cur_cnt * elem_sz;
177}
178
179
180
181
182int btf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt)
183{
184 void *p;
185
186 if (need_cnt <= *cap_cnt)
187 return 0;
188
189 p = btf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt);
190 if (!p)
191 return -ENOMEM;
192
193 return 0;
194}
195
196static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
197{
198 __u32 *p;
199
200 p = btf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32),
201 btf->nr_types, BTF_MAX_NR_TYPES, 1);
202 if (!p)
203 return -ENOMEM;
204
205 *p = type_off;
206 return 0;
207}
208
209static void btf_bswap_hdr(struct btf_header *h)
210{
211 h->magic = bswap_16(h->magic);
212 h->hdr_len = bswap_32(h->hdr_len);
213 h->type_off = bswap_32(h->type_off);
214 h->type_len = bswap_32(h->type_len);
215 h->str_off = bswap_32(h->str_off);
216 h->str_len = bswap_32(h->str_len);
217}
218
219static int btf_parse_hdr(struct btf *btf)
220{
221 struct btf_header *hdr = btf->hdr;
222 __u32 meta_left;
223
224 if (btf->raw_size < sizeof(struct btf_header)) {
225 pr_debug("BTF header not found\n");
226 return -EINVAL;
227 }
228
229 if (hdr->magic == bswap_16(BTF_MAGIC)) {
230 btf->swapped_endian = true;
231 if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
232 pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
233 bswap_32(hdr->hdr_len));
234 return -ENOTSUP;
235 }
236 btf_bswap_hdr(hdr);
237 } else if (hdr->magic != BTF_MAGIC) {
238 pr_debug("Invalid BTF magic:%x\n", hdr->magic);
239 return -EINVAL;
240 }
241
242 meta_left = btf->raw_size - sizeof(*hdr);
243 if (meta_left < hdr->str_off + hdr->str_len) {
244 pr_debug("Invalid BTF total size:%u\n", btf->raw_size);
245 return -EINVAL;
246 }
247
248 if (hdr->type_off + hdr->type_len > hdr->str_off) {
249 pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
250 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
251 return -EINVAL;
252 }
253
254 if (hdr->type_off % 4) {
255 pr_debug("BTF type section is not aligned to 4 bytes\n");
256 return -EINVAL;
257 }
258
259 return 0;
260}
261
262static int btf_parse_str_sec(struct btf *btf)
263{
264 const struct btf_header *hdr = btf->hdr;
265 const char *start = btf->strs_data;
266 const char *end = start + btf->hdr->str_len;
267
268 if (btf->base_btf && hdr->str_len == 0)
269 return 0;
270 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) {
271 pr_debug("Invalid BTF string section\n");
272 return -EINVAL;
273 }
274 if (!btf->base_btf && start[0]) {
275 pr_debug("Invalid BTF string section\n");
276 return -EINVAL;
277 }
278 return 0;
279}
280
281static int btf_type_size(const struct btf_type *t)
282{
283 const int base_size = sizeof(struct btf_type);
284 __u16 vlen = btf_vlen(t);
285
286 switch (btf_kind(t)) {
287 case BTF_KIND_FWD:
288 case BTF_KIND_CONST:
289 case BTF_KIND_VOLATILE:
290 case BTF_KIND_RESTRICT:
291 case BTF_KIND_PTR:
292 case BTF_KIND_TYPEDEF:
293 case BTF_KIND_FUNC:
294 case BTF_KIND_FLOAT:
295 return base_size;
296 case BTF_KIND_INT:
297 return base_size + sizeof(__u32);
298 case BTF_KIND_ENUM:
299 return base_size + vlen * sizeof(struct btf_enum);
300 case BTF_KIND_ARRAY:
301 return base_size + sizeof(struct btf_array);
302 case BTF_KIND_STRUCT:
303 case BTF_KIND_UNION:
304 return base_size + vlen * sizeof(struct btf_member);
305 case BTF_KIND_FUNC_PROTO:
306 return base_size + vlen * sizeof(struct btf_param);
307 case BTF_KIND_VAR:
308 return base_size + sizeof(struct btf_var);
309 case BTF_KIND_DATASEC:
310 return base_size + vlen * sizeof(struct btf_var_secinfo);
311 default:
312 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
313 return -EINVAL;
314 }
315}
316
317static void btf_bswap_type_base(struct btf_type *t)
318{
319 t->name_off = bswap_32(t->name_off);
320 t->info = bswap_32(t->info);
321 t->type = bswap_32(t->type);
322}
323
324static int btf_bswap_type_rest(struct btf_type *t)
325{
326 struct btf_var_secinfo *v;
327 struct btf_member *m;
328 struct btf_array *a;
329 struct btf_param *p;
330 struct btf_enum *e;
331 __u16 vlen = btf_vlen(t);
332 int i;
333
334 switch (btf_kind(t)) {
335 case BTF_KIND_FWD:
336 case BTF_KIND_CONST:
337 case BTF_KIND_VOLATILE:
338 case BTF_KIND_RESTRICT:
339 case BTF_KIND_PTR:
340 case BTF_KIND_TYPEDEF:
341 case BTF_KIND_FUNC:
342 case BTF_KIND_FLOAT:
343 return 0;
344 case BTF_KIND_INT:
345 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
346 return 0;
347 case BTF_KIND_ENUM:
348 for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
349 e->name_off = bswap_32(e->name_off);
350 e->val = bswap_32(e->val);
351 }
352 return 0;
353 case BTF_KIND_ARRAY:
354 a = btf_array(t);
355 a->type = bswap_32(a->type);
356 a->index_type = bswap_32(a->index_type);
357 a->nelems = bswap_32(a->nelems);
358 return 0;
359 case BTF_KIND_STRUCT:
360 case BTF_KIND_UNION:
361 for (i = 0, m = btf_members(t); i < vlen; i++, m++) {
362 m->name_off = bswap_32(m->name_off);
363 m->type = bswap_32(m->type);
364 m->offset = bswap_32(m->offset);
365 }
366 return 0;
367 case BTF_KIND_FUNC_PROTO:
368 for (i = 0, p = btf_params(t); i < vlen; i++, p++) {
369 p->name_off = bswap_32(p->name_off);
370 p->type = bswap_32(p->type);
371 }
372 return 0;
373 case BTF_KIND_VAR:
374 btf_var(t)->linkage = bswap_32(btf_var(t)->linkage);
375 return 0;
376 case BTF_KIND_DATASEC:
377 for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) {
378 v->type = bswap_32(v->type);
379 v->offset = bswap_32(v->offset);
380 v->size = bswap_32(v->size);
381 }
382 return 0;
383 default:
384 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
385 return -EINVAL;
386 }
387}
388
389static int btf_parse_type_sec(struct btf *btf)
390{
391 struct btf_header *hdr = btf->hdr;
392 void *next_type = btf->types_data;
393 void *end_type = next_type + hdr->type_len;
394 int err, type_size;
395
396 while (next_type + sizeof(struct btf_type) <= end_type) {
397 if (btf->swapped_endian)
398 btf_bswap_type_base(next_type);
399
400 type_size = btf_type_size(next_type);
401 if (type_size < 0)
402 return type_size;
403 if (next_type + type_size > end_type) {
404 pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types);
405 return -EINVAL;
406 }
407
408 if (btf->swapped_endian && btf_bswap_type_rest(next_type))
409 return -EINVAL;
410
411 err = btf_add_type_idx_entry(btf, next_type - btf->types_data);
412 if (err)
413 return err;
414
415 next_type += type_size;
416 btf->nr_types++;
417 }
418
419 if (next_type != end_type) {
420 pr_warn("BTF types data is malformed\n");
421 return -EINVAL;
422 }
423
424 return 0;
425}
426
427__u32 btf__get_nr_types(const struct btf *btf)
428{
429 return btf->start_id + btf->nr_types - 1;
430}
431
432const struct btf *btf__base_btf(const struct btf *btf)
433{
434 return btf->base_btf;
435}
436
437
438static struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id)
439{
440 if (type_id == 0)
441 return &btf_void;
442 if (type_id < btf->start_id)
443 return btf_type_by_id(btf->base_btf, type_id);
444 return btf->types_data + btf->type_offs[type_id - btf->start_id];
445}
446
447const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
448{
449 if (type_id >= btf->start_id + btf->nr_types)
450 return NULL;
451 return btf_type_by_id((struct btf *)btf, type_id);
452}
453
454static int determine_ptr_size(const struct btf *btf)
455{
456 const struct btf_type *t;
457 const char *name;
458 int i, n;
459
460 if (btf->base_btf && btf->base_btf->ptr_sz > 0)
461 return btf->base_btf->ptr_sz;
462
463 n = btf__get_nr_types(btf);
464 for (i = 1; i <= n; i++) {
465 t = btf__type_by_id(btf, i);
466 if (!btf_is_int(t))
467 continue;
468
469 name = btf__name_by_offset(btf, t->name_off);
470 if (!name)
471 continue;
472
473 if (strcmp(name, "long int") == 0 ||
474 strcmp(name, "long unsigned int") == 0) {
475 if (t->size != 4 && t->size != 8)
476 continue;
477 return t->size;
478 }
479 }
480
481 return -1;
482}
483
484static size_t btf_ptr_sz(const struct btf *btf)
485{
486 if (!btf->ptr_sz)
487 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
488 return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz;
489}
490
491
492
493
494
495
496
497
498
499size_t btf__pointer_size(const struct btf *btf)
500{
501 if (!btf->ptr_sz)
502 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
503
504 if (btf->ptr_sz < 0)
505
506 return 0;
507
508 return btf->ptr_sz;
509}
510
511
512
513
514int btf__set_pointer_size(struct btf *btf, size_t ptr_sz)
515{
516 if (ptr_sz != 4 && ptr_sz != 8)
517 return -EINVAL;
518 btf->ptr_sz = ptr_sz;
519 return 0;
520}
521
522static bool is_host_big_endian(void)
523{
524#if __BYTE_ORDER == __LITTLE_ENDIAN
525 return false;
526#elif __BYTE_ORDER == __BIG_ENDIAN
527 return true;
528#else
529# error "Unrecognized __BYTE_ORDER__"
530#endif
531}
532
533enum btf_endianness btf__endianness(const struct btf *btf)
534{
535 if (is_host_big_endian())
536 return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN;
537 else
538 return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
539}
540
541int btf__set_endianness(struct btf *btf, enum btf_endianness endian)
542{
543 if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN)
544 return -EINVAL;
545
546 btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN);
547 if (!btf->swapped_endian) {
548 free(btf->raw_data_swapped);
549 btf->raw_data_swapped = NULL;
550 }
551 return 0;
552}
553
554static bool btf_type_is_void(const struct btf_type *t)
555{
556 return t == &btf_void || btf_is_fwd(t);
557}
558
559static bool btf_type_is_void_or_null(const struct btf_type *t)
560{
561 return !t || btf_type_is_void(t);
562}
563
564#define MAX_RESOLVE_DEPTH 32
565
566__s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
567{
568 const struct btf_array *array;
569 const struct btf_type *t;
570 __u32 nelems = 1;
571 __s64 size = -1;
572 int i;
573
574 t = btf__type_by_id(btf, type_id);
575 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
576 i++) {
577 switch (btf_kind(t)) {
578 case BTF_KIND_INT:
579 case BTF_KIND_STRUCT:
580 case BTF_KIND_UNION:
581 case BTF_KIND_ENUM:
582 case BTF_KIND_DATASEC:
583 case BTF_KIND_FLOAT:
584 size = t->size;
585 goto done;
586 case BTF_KIND_PTR:
587 size = btf_ptr_sz(btf);
588 goto done;
589 case BTF_KIND_TYPEDEF:
590 case BTF_KIND_VOLATILE:
591 case BTF_KIND_CONST:
592 case BTF_KIND_RESTRICT:
593 case BTF_KIND_VAR:
594 type_id = t->type;
595 break;
596 case BTF_KIND_ARRAY:
597 array = btf_array(t);
598 if (nelems && array->nelems > UINT32_MAX / nelems)
599 return -E2BIG;
600 nelems *= array->nelems;
601 type_id = array->type;
602 break;
603 default:
604 return -EINVAL;
605 }
606
607 t = btf__type_by_id(btf, type_id);
608 }
609
610done:
611 if (size < 0)
612 return -EINVAL;
613 if (nelems && size > UINT32_MAX / nelems)
614 return -E2BIG;
615
616 return nelems * size;
617}
618
619int btf__align_of(const struct btf *btf, __u32 id)
620{
621 const struct btf_type *t = btf__type_by_id(btf, id);
622 __u16 kind = btf_kind(t);
623
624 switch (kind) {
625 case BTF_KIND_INT:
626 case BTF_KIND_ENUM:
627 case BTF_KIND_FLOAT:
628 return min(btf_ptr_sz(btf), (size_t)t->size);
629 case BTF_KIND_PTR:
630 return btf_ptr_sz(btf);
631 case BTF_KIND_TYPEDEF:
632 case BTF_KIND_VOLATILE:
633 case BTF_KIND_CONST:
634 case BTF_KIND_RESTRICT:
635 return btf__align_of(btf, t->type);
636 case BTF_KIND_ARRAY:
637 return btf__align_of(btf, btf_array(t)->type);
638 case BTF_KIND_STRUCT:
639 case BTF_KIND_UNION: {
640 const struct btf_member *m = btf_members(t);
641 __u16 vlen = btf_vlen(t);
642 int i, max_align = 1, align;
643
644 for (i = 0; i < vlen; i++, m++) {
645 align = btf__align_of(btf, m->type);
646 if (align <= 0)
647 return align;
648 max_align = max(max_align, align);
649 }
650
651 return max_align;
652 }
653 default:
654 pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
655 return 0;
656 }
657}
658
659int btf__resolve_type(const struct btf *btf, __u32 type_id)
660{
661 const struct btf_type *t;
662 int depth = 0;
663
664 t = btf__type_by_id(btf, type_id);
665 while (depth < MAX_RESOLVE_DEPTH &&
666 !btf_type_is_void_or_null(t) &&
667 (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
668 type_id = t->type;
669 t = btf__type_by_id(btf, type_id);
670 depth++;
671 }
672
673 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
674 return -EINVAL;
675
676 return type_id;
677}
678
679__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
680{
681 __u32 i, nr_types = btf__get_nr_types(btf);
682
683 if (!strcmp(type_name, "void"))
684 return 0;
685
686 for (i = 1; i <= nr_types; i++) {
687 const struct btf_type *t = btf__type_by_id(btf, i);
688 const char *name = btf__name_by_offset(btf, t->name_off);
689
690 if (name && !strcmp(type_name, name))
691 return i;
692 }
693
694 return -ENOENT;
695}
696
697__s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
698 __u32 kind)
699{
700 __u32 i, nr_types = btf__get_nr_types(btf);
701
702 if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
703 return 0;
704
705 for (i = 1; i <= nr_types; i++) {
706 const struct btf_type *t = btf__type_by_id(btf, i);
707 const char *name;
708
709 if (btf_kind(t) != kind)
710 continue;
711 name = btf__name_by_offset(btf, t->name_off);
712 if (name && !strcmp(type_name, name))
713 return i;
714 }
715
716 return -ENOENT;
717}
718
719static bool btf_is_modifiable(const struct btf *btf)
720{
721 return (void *)btf->hdr != btf->raw_data;
722}
723
724void btf__free(struct btf *btf)
725{
726 if (IS_ERR_OR_NULL(btf))
727 return;
728
729 if (btf->fd >= 0)
730 close(btf->fd);
731
732 if (btf_is_modifiable(btf)) {
733
734
735
736
737
738
739 free(btf->hdr);
740 free(btf->types_data);
741 free(btf->strs_data);
742 }
743 free(btf->raw_data);
744 free(btf->raw_data_swapped);
745 free(btf->type_offs);
746 free(btf);
747}
748
749static struct btf *btf_new_empty(struct btf *base_btf)
750{
751 struct btf *btf;
752
753 btf = calloc(1, sizeof(*btf));
754 if (!btf)
755 return ERR_PTR(-ENOMEM);
756
757 btf->nr_types = 0;
758 btf->start_id = 1;
759 btf->start_str_off = 0;
760 btf->fd = -1;
761 btf->ptr_sz = sizeof(void *);
762 btf->swapped_endian = false;
763
764 if (base_btf) {
765 btf->base_btf = base_btf;
766 btf->start_id = btf__get_nr_types(base_btf) + 1;
767 btf->start_str_off = base_btf->hdr->str_len;
768 }
769
770
771 btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1);
772 btf->raw_data = calloc(1, btf->raw_size);
773 if (!btf->raw_data) {
774 free(btf);
775 return ERR_PTR(-ENOMEM);
776 }
777
778 btf->hdr = btf->raw_data;
779 btf->hdr->hdr_len = sizeof(struct btf_header);
780 btf->hdr->magic = BTF_MAGIC;
781 btf->hdr->version = BTF_VERSION;
782
783 btf->types_data = btf->raw_data + btf->hdr->hdr_len;
784 btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
785 btf->hdr->str_len = base_btf ? 0 : 1;
786
787 return btf;
788}
789
790struct btf *btf__new_empty(void)
791{
792 return btf_new_empty(NULL);
793}
794
795struct btf *btf__new_empty_split(struct btf *base_btf)
796{
797 return btf_new_empty(base_btf);
798}
799
800static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
801{
802 struct btf *btf;
803 int err;
804
805 btf = calloc(1, sizeof(struct btf));
806 if (!btf)
807 return ERR_PTR(-ENOMEM);
808
809 btf->nr_types = 0;
810 btf->start_id = 1;
811 btf->start_str_off = 0;
812
813 if (base_btf) {
814 btf->base_btf = base_btf;
815 btf->start_id = btf__get_nr_types(base_btf) + 1;
816 btf->start_str_off = base_btf->hdr->str_len;
817 }
818
819 btf->raw_data = malloc(size);
820 if (!btf->raw_data) {
821 err = -ENOMEM;
822 goto done;
823 }
824 memcpy(btf->raw_data, data, size);
825 btf->raw_size = size;
826
827 btf->hdr = btf->raw_data;
828 err = btf_parse_hdr(btf);
829 if (err)
830 goto done;
831
832 btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
833 btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
834
835 err = btf_parse_str_sec(btf);
836 err = err ?: btf_parse_type_sec(btf);
837 if (err)
838 goto done;
839
840 btf->fd = -1;
841
842done:
843 if (err) {
844 btf__free(btf);
845 return ERR_PTR(err);
846 }
847
848 return btf;
849}
850
851struct btf *btf__new(const void *data, __u32 size)
852{
853 return btf_new(data, size, NULL);
854}
855
856static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
857 struct btf_ext **btf_ext)
858{
859 Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
860 int err = 0, fd = -1, idx = 0;
861 struct btf *btf = NULL;
862 Elf_Scn *scn = NULL;
863 Elf *elf = NULL;
864 GElf_Ehdr ehdr;
865
866 if (elf_version(EV_CURRENT) == EV_NONE) {
867 pr_warn("failed to init libelf for %s\n", path);
868 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
869 }
870
871 fd = open(path, O_RDONLY);
872 if (fd < 0) {
873 err = -errno;
874 pr_warn("failed to open %s: %s\n", path, strerror(errno));
875 return ERR_PTR(err);
876 }
877
878 err = -LIBBPF_ERRNO__FORMAT;
879
880 elf = elf_begin(fd, ELF_C_READ, NULL);
881 if (!elf) {
882 pr_warn("failed to open %s as ELF file\n", path);
883 goto done;
884 }
885 if (!gelf_getehdr(elf, &ehdr)) {
886 pr_warn("failed to get EHDR from %s\n", path);
887 goto done;
888 }
889 if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
890 pr_warn("failed to get e_shstrndx from %s\n", path);
891 goto done;
892 }
893
894 while ((scn = elf_nextscn(elf, scn)) != NULL) {
895 GElf_Shdr sh;
896 char *name;
897
898 idx++;
899 if (gelf_getshdr(scn, &sh) != &sh) {
900 pr_warn("failed to get section(%d) header from %s\n",
901 idx, path);
902 goto done;
903 }
904 name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
905 if (!name) {
906 pr_warn("failed to get section(%d) name from %s\n",
907 idx, path);
908 goto done;
909 }
910 if (strcmp(name, BTF_ELF_SEC) == 0) {
911 btf_data = elf_getdata(scn, 0);
912 if (!btf_data) {
913 pr_warn("failed to get section(%d, %s) data from %s\n",
914 idx, name, path);
915 goto done;
916 }
917 continue;
918 } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
919 btf_ext_data = elf_getdata(scn, 0);
920 if (!btf_ext_data) {
921 pr_warn("failed to get section(%d, %s) data from %s\n",
922 idx, name, path);
923 goto done;
924 }
925 continue;
926 }
927 }
928
929 err = 0;
930
931 if (!btf_data) {
932 err = -ENOENT;
933 goto done;
934 }
935 btf = btf_new(btf_data->d_buf, btf_data->d_size, base_btf);
936 if (IS_ERR(btf))
937 goto done;
938
939 switch (gelf_getclass(elf)) {
940 case ELFCLASS32:
941 btf__set_pointer_size(btf, 4);
942 break;
943 case ELFCLASS64:
944 btf__set_pointer_size(btf, 8);
945 break;
946 default:
947 pr_warn("failed to get ELF class (bitness) for %s\n", path);
948 break;
949 }
950
951 if (btf_ext && btf_ext_data) {
952 *btf_ext = btf_ext__new(btf_ext_data->d_buf,
953 btf_ext_data->d_size);
954 if (IS_ERR(*btf_ext))
955 goto done;
956 } else if (btf_ext) {
957 *btf_ext = NULL;
958 }
959done:
960 if (elf)
961 elf_end(elf);
962 close(fd);
963
964 if (err)
965 return ERR_PTR(err);
966
967
968
969
970 if (IS_ERR(btf))
971 return btf;
972 if (btf_ext && IS_ERR(*btf_ext)) {
973 btf__free(btf);
974 err = PTR_ERR(*btf_ext);
975 return ERR_PTR(err);
976 }
977 return btf;
978}
979
980struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
981{
982 return btf_parse_elf(path, NULL, btf_ext);
983}
984
985struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf)
986{
987 return btf_parse_elf(path, base_btf, NULL);
988}
989
990static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)
991{
992 struct btf *btf = NULL;
993 void *data = NULL;
994 FILE *f = NULL;
995 __u16 magic;
996 int err = 0;
997 long sz;
998
999 f = fopen(path, "rb");
1000 if (!f) {
1001 err = -errno;
1002 goto err_out;
1003 }
1004
1005
1006 if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
1007 err = -EIO;
1008 goto err_out;
1009 }
1010 if (magic == __bswap_16(BTF_MAGIC)) {
1011
1012 pr_warn("non-native BTF endianness is not supported\n");
1013 err = -LIBBPF_ERRNO__ENDIAN;
1014 goto err_out;
1015 }
1016 if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) {
1017
1018 err = -EPROTO;
1019 goto err_out;
1020 }
1021
1022
1023 if (fseek(f, 0, SEEK_END)) {
1024 err = -errno;
1025 goto err_out;
1026 }
1027 sz = ftell(f);
1028 if (sz < 0) {
1029 err = -errno;
1030 goto err_out;
1031 }
1032
1033 if (fseek(f, 0, SEEK_SET)) {
1034 err = -errno;
1035 goto err_out;
1036 }
1037
1038
1039 data = malloc(sz);
1040 if (!data) {
1041 err = -ENOMEM;
1042 goto err_out;
1043 }
1044 if (fread(data, 1, sz, f) < sz) {
1045 err = -EIO;
1046 goto err_out;
1047 }
1048
1049
1050 btf = btf_new(data, sz, base_btf);
1051
1052err_out:
1053 free(data);
1054 if (f)
1055 fclose(f);
1056 return err ? ERR_PTR(err) : btf;
1057}
1058
1059struct btf *btf__parse_raw(const char *path)
1060{
1061 return btf_parse_raw(path, NULL);
1062}
1063
1064struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf)
1065{
1066 return btf_parse_raw(path, base_btf);
1067}
1068
1069static struct btf *btf_parse(const char *path, struct btf *base_btf, struct btf_ext **btf_ext)
1070{
1071 struct btf *btf;
1072
1073 if (btf_ext)
1074 *btf_ext = NULL;
1075
1076 btf = btf_parse_raw(path, base_btf);
1077 if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
1078 return btf;
1079
1080 return btf_parse_elf(path, base_btf, btf_ext);
1081}
1082
1083struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
1084{
1085 return btf_parse(path, NULL, btf_ext);
1086}
1087
1088struct btf *btf__parse_split(const char *path, struct btf *base_btf)
1089{
1090 return btf_parse(path, base_btf, NULL);
1091}
1092
1093static int compare_vsi_off(const void *_a, const void *_b)
1094{
1095 const struct btf_var_secinfo *a = _a;
1096 const struct btf_var_secinfo *b = _b;
1097
1098 return a->offset - b->offset;
1099}
1100
1101static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
1102 struct btf_type *t)
1103{
1104 __u32 size = 0, off = 0, i, vars = btf_vlen(t);
1105 const char *name = btf__name_by_offset(btf, t->name_off);
1106 const struct btf_type *t_var;
1107 struct btf_var_secinfo *vsi;
1108 const struct btf_var *var;
1109 int ret;
1110
1111 if (!name) {
1112 pr_debug("No name found in string section for DATASEC kind.\n");
1113 return -ENOENT;
1114 }
1115
1116
1117
1118
1119 if (t->size)
1120 goto sort_vars;
1121
1122 ret = bpf_object__section_size(obj, name, &size);
1123 if (ret || !size || (t->size && t->size != size)) {
1124 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
1125 return -ENOENT;
1126 }
1127
1128 t->size = size;
1129
1130 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
1131 t_var = btf__type_by_id(btf, vsi->type);
1132 var = btf_var(t_var);
1133
1134 if (!btf_is_var(t_var)) {
1135 pr_debug("Non-VAR type seen in section %s\n", name);
1136 return -EINVAL;
1137 }
1138
1139 if (var->linkage == BTF_VAR_STATIC)
1140 continue;
1141
1142 name = btf__name_by_offset(btf, t_var->name_off);
1143 if (!name) {
1144 pr_debug("No name found in string section for VAR kind\n");
1145 return -ENOENT;
1146 }
1147
1148 ret = bpf_object__variable_offset(obj, name, &off);
1149 if (ret) {
1150 pr_debug("No offset found in symbol table for VAR %s\n",
1151 name);
1152 return -ENOENT;
1153 }
1154
1155 vsi->offset = off;
1156 }
1157
1158sort_vars:
1159 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
1160 return 0;
1161}
1162
1163int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
1164{
1165 int err = 0;
1166 __u32 i;
1167
1168 for (i = 1; i <= btf->nr_types; i++) {
1169 struct btf_type *t = btf_type_by_id(btf, i);
1170
1171
1172
1173
1174
1175
1176 if (btf_is_datasec(t)) {
1177 err = btf_fixup_datasec(obj, btf, t);
1178 if (err)
1179 break;
1180 }
1181 }
1182
1183 return err;
1184}
1185
1186static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
1187
1188int btf__load(struct btf *btf)
1189{
1190 __u32 log_buf_size = 0, raw_size;
1191 char *log_buf = NULL;
1192 void *raw_data;
1193 int err = 0;
1194
1195 if (btf->fd >= 0)
1196 return -EEXIST;
1197
1198retry_load:
1199 if (log_buf_size) {
1200 log_buf = malloc(log_buf_size);
1201 if (!log_buf)
1202 return -ENOMEM;
1203
1204 *log_buf = 0;
1205 }
1206
1207 raw_data = btf_get_raw_data(btf, &raw_size, false);
1208 if (!raw_data) {
1209 err = -ENOMEM;
1210 goto done;
1211 }
1212
1213 btf->raw_size = raw_size;
1214 btf->raw_data = raw_data;
1215
1216 btf->fd = bpf_load_btf(raw_data, raw_size, log_buf, log_buf_size, false);
1217 if (btf->fd < 0) {
1218 if (!log_buf || errno == ENOSPC) {
1219 log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
1220 log_buf_size << 1);
1221 free(log_buf);
1222 goto retry_load;
1223 }
1224
1225 err = -errno;
1226 pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
1227 if (*log_buf)
1228 pr_warn("%s\n", log_buf);
1229 goto done;
1230 }
1231
1232done:
1233 free(log_buf);
1234 return err;
1235}
1236
1237int btf__fd(const struct btf *btf)
1238{
1239 return btf->fd;
1240}
1241
1242void btf__set_fd(struct btf *btf, int fd)
1243{
1244 btf->fd = fd;
1245}
1246
1247static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
1248{
1249 struct btf_header *hdr = btf->hdr;
1250 struct btf_type *t;
1251 void *data, *p;
1252 __u32 data_sz;
1253 int i;
1254
1255 data = swap_endian ? btf->raw_data_swapped : btf->raw_data;
1256 if (data) {
1257 *size = btf->raw_size;
1258 return data;
1259 }
1260
1261 data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
1262 data = calloc(1, data_sz);
1263 if (!data)
1264 return NULL;
1265 p = data;
1266
1267 memcpy(p, hdr, hdr->hdr_len);
1268 if (swap_endian)
1269 btf_bswap_hdr(p);
1270 p += hdr->hdr_len;
1271
1272 memcpy(p, btf->types_data, hdr->type_len);
1273 if (swap_endian) {
1274 for (i = 0; i < btf->nr_types; i++) {
1275 t = p + btf->type_offs[i];
1276
1277
1278
1279
1280 if (btf_bswap_type_rest(t))
1281 goto err_out;
1282 btf_bswap_type_base(t);
1283 }
1284 }
1285 p += hdr->type_len;
1286
1287 memcpy(p, btf->strs_data, hdr->str_len);
1288 p += hdr->str_len;
1289
1290 *size = data_sz;
1291 return data;
1292err_out:
1293 free(data);
1294 return NULL;
1295}
1296
1297const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
1298{
1299 struct btf *btf = (struct btf *)btf_ro;
1300 __u32 data_sz;
1301 void *data;
1302
1303 data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
1304 if (!data)
1305 return NULL;
1306
1307 btf->raw_size = data_sz;
1308 if (btf->swapped_endian)
1309 btf->raw_data_swapped = data;
1310 else
1311 btf->raw_data = data;
1312 *size = data_sz;
1313 return data;
1314}
1315
1316const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
1317{
1318 if (offset < btf->start_str_off)
1319 return btf__str_by_offset(btf->base_btf, offset);
1320 else if (offset - btf->start_str_off < btf->hdr->str_len)
1321 return btf->strs_data + (offset - btf->start_str_off);
1322 else
1323 return NULL;
1324}
1325
1326const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
1327{
1328 return btf__str_by_offset(btf, offset);
1329}
1330
1331struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
1332{
1333 struct bpf_btf_info btf_info;
1334 __u32 len = sizeof(btf_info);
1335 __u32 last_size;
1336 struct btf *btf;
1337 void *ptr;
1338 int err;
1339
1340
1341
1342
1343
1344 last_size = 4096;
1345 ptr = malloc(last_size);
1346 if (!ptr)
1347 return ERR_PTR(-ENOMEM);
1348
1349 memset(&btf_info, 0, sizeof(btf_info));
1350 btf_info.btf = ptr_to_u64(ptr);
1351 btf_info.btf_size = last_size;
1352 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1353
1354 if (!err && btf_info.btf_size > last_size) {
1355 void *temp_ptr;
1356
1357 last_size = btf_info.btf_size;
1358 temp_ptr = realloc(ptr, last_size);
1359 if (!temp_ptr) {
1360 btf = ERR_PTR(-ENOMEM);
1361 goto exit_free;
1362 }
1363 ptr = temp_ptr;
1364
1365 len = sizeof(btf_info);
1366 memset(&btf_info, 0, sizeof(btf_info));
1367 btf_info.btf = ptr_to_u64(ptr);
1368 btf_info.btf_size = last_size;
1369
1370 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1371 }
1372
1373 if (err || btf_info.btf_size > last_size) {
1374 btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG);
1375 goto exit_free;
1376 }
1377
1378 btf = btf_new(ptr, btf_info.btf_size, base_btf);
1379
1380exit_free:
1381 free(ptr);
1382 return btf;
1383}
1384
1385int btf__get_from_id(__u32 id, struct btf **btf)
1386{
1387 struct btf *res;
1388 int btf_fd;
1389
1390 *btf = NULL;
1391 btf_fd = bpf_btf_get_fd_by_id(id);
1392 if (btf_fd < 0)
1393 return -errno;
1394
1395 res = btf_get_from_fd(btf_fd, NULL);
1396 close(btf_fd);
1397 if (IS_ERR(res))
1398 return PTR_ERR(res);
1399
1400 *btf = res;
1401 return 0;
1402}
1403
1404int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
1405 __u32 expected_key_size, __u32 expected_value_size,
1406 __u32 *key_type_id, __u32 *value_type_id)
1407{
1408 const struct btf_type *container_type;
1409 const struct btf_member *key, *value;
1410 const size_t max_name = 256;
1411 char container_name[max_name];
1412 __s64 key_size, value_size;
1413 __s32 container_id;
1414
1415 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
1416 max_name) {
1417 pr_warn("map:%s length of '____btf_map_%s' is too long\n",
1418 map_name, map_name);
1419 return -EINVAL;
1420 }
1421
1422 container_id = btf__find_by_name(btf, container_name);
1423 if (container_id < 0) {
1424 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1425 map_name, container_name);
1426 return container_id;
1427 }
1428
1429 container_type = btf__type_by_id(btf, container_id);
1430 if (!container_type) {
1431 pr_warn("map:%s cannot find BTF type for container_id:%u\n",
1432 map_name, container_id);
1433 return -EINVAL;
1434 }
1435
1436 if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
1437 pr_warn("map:%s container_name:%s is an invalid container struct\n",
1438 map_name, container_name);
1439 return -EINVAL;
1440 }
1441
1442 key = btf_members(container_type);
1443 value = key + 1;
1444
1445 key_size = btf__resolve_size(btf, key->type);
1446 if (key_size < 0) {
1447 pr_warn("map:%s invalid BTF key_type_size\n", map_name);
1448 return key_size;
1449 }
1450
1451 if (expected_key_size != key_size) {
1452 pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1453 map_name, (__u32)key_size, expected_key_size);
1454 return -EINVAL;
1455 }
1456
1457 value_size = btf__resolve_size(btf, value->type);
1458 if (value_size < 0) {
1459 pr_warn("map:%s invalid BTF value_type_size\n", map_name);
1460 return value_size;
1461 }
1462
1463 if (expected_value_size != value_size) {
1464 pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1465 map_name, (__u32)value_size, expected_value_size);
1466 return -EINVAL;
1467 }
1468
1469 *key_type_id = key->type;
1470 *value_type_id = value->type;
1471
1472 return 0;
1473}
1474
1475static size_t strs_hash_fn(const void *key, void *ctx)
1476{
1477 const struct btf *btf = ctx;
1478 const char *strs = *btf->strs_data_ptr;
1479 const char *str = strs + (long)key;
1480
1481 return str_hash(str);
1482}
1483
1484static bool strs_hash_equal_fn(const void *key1, const void *key2, void *ctx)
1485{
1486 const struct btf *btf = ctx;
1487 const char *strs = *btf->strs_data_ptr;
1488 const char *str1 = strs + (long)key1;
1489 const char *str2 = strs + (long)key2;
1490
1491 return strcmp(str1, str2) == 0;
1492}
1493
1494static void btf_invalidate_raw_data(struct btf *btf)
1495{
1496 if (btf->raw_data) {
1497 free(btf->raw_data);
1498 btf->raw_data = NULL;
1499 }
1500 if (btf->raw_data_swapped) {
1501 free(btf->raw_data_swapped);
1502 btf->raw_data_swapped = NULL;
1503 }
1504}
1505
1506
1507
1508
1509
1510static int btf_ensure_modifiable(struct btf *btf)
1511{
1512 void *hdr, *types, *strs, *strs_end, *s;
1513 struct hashmap *hash = NULL;
1514 long off;
1515 int err;
1516
1517 if (btf_is_modifiable(btf)) {
1518
1519 btf_invalidate_raw_data(btf);
1520 return 0;
1521 }
1522
1523
1524 hdr = malloc(btf->hdr->hdr_len);
1525 types = malloc(btf->hdr->type_len);
1526 strs = malloc(btf->hdr->str_len);
1527 if (!hdr || !types || !strs)
1528 goto err_out;
1529
1530 memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
1531 memcpy(types, btf->types_data, btf->hdr->type_len);
1532 memcpy(strs, btf->strs_data, btf->hdr->str_len);
1533
1534
1535 btf->strs_data_ptr = &btf->strs_data;
1536
1537
1538 hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, btf);
1539 if (IS_ERR(hash)) {
1540 err = PTR_ERR(hash);
1541 hash = NULL;
1542 goto err_out;
1543 }
1544
1545 strs_end = strs + btf->hdr->str_len;
1546 for (off = 0, s = strs; s < strs_end; off += strlen(s) + 1, s = strs + off) {
1547
1548
1549
1550 err = hashmap__add(hash, (void *)off, (void *)off);
1551 if (err == -EEXIST)
1552 continue;
1553 if (err)
1554 goto err_out;
1555 }
1556
1557
1558 btf->hdr = hdr;
1559 btf->types_data = types;
1560 btf->types_data_cap = btf->hdr->type_len;
1561 btf->strs_data = strs;
1562 btf->strs_data_cap = btf->hdr->str_len;
1563 btf->strs_hash = hash;
1564
1565
1566
1567 if (btf->hdr->str_len == 0)
1568 btf->strs_deduped = true;
1569 if (!btf->base_btf && btf->hdr->str_len == 1)
1570 btf->strs_deduped = true;
1571
1572
1573 btf_invalidate_raw_data(btf);
1574
1575 return 0;
1576
1577err_out:
1578 hashmap__free(hash);
1579 free(hdr);
1580 free(types);
1581 free(strs);
1582 return -ENOMEM;
1583}
1584
1585static void *btf_add_str_mem(struct btf *btf, size_t add_sz)
1586{
1587 return btf_add_mem(&btf->strs_data, &btf->strs_data_cap, 1,
1588 btf->hdr->str_len, BTF_MAX_STR_OFFSET, add_sz);
1589}
1590
1591
1592
1593
1594
1595
1596
1597int btf__find_str(struct btf *btf, const char *s)
1598{
1599 long old_off, new_off, len;
1600 void *p;
1601
1602 if (btf->base_btf) {
1603 int ret;
1604
1605 ret = btf__find_str(btf->base_btf, s);
1606 if (ret != -ENOENT)
1607 return ret;
1608 }
1609
1610
1611 if (btf_ensure_modifiable(btf))
1612 return -ENOMEM;
1613
1614
1615 len = strlen(s) + 1;
1616 p = btf_add_str_mem(btf, len);
1617 if (!p)
1618 return -ENOMEM;
1619
1620 new_off = btf->hdr->str_len;
1621 memcpy(p, s, len);
1622
1623 if (hashmap__find(btf->strs_hash, (void *)new_off, (void **)&old_off))
1624 return btf->start_str_off + old_off;
1625
1626 return -ENOENT;
1627}
1628
1629
1630
1631
1632
1633
1634int btf__add_str(struct btf *btf, const char *s)
1635{
1636 long old_off, new_off, len;
1637 void *p;
1638 int err;
1639
1640 if (btf->base_btf) {
1641 int ret;
1642
1643 ret = btf__find_str(btf->base_btf, s);
1644 if (ret != -ENOENT)
1645 return ret;
1646 }
1647
1648 if (btf_ensure_modifiable(btf))
1649 return -ENOMEM;
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659 len = strlen(s) + 1;
1660 p = btf_add_str_mem(btf, len);
1661 if (!p)
1662 return -ENOMEM;
1663
1664 new_off = btf->hdr->str_len;
1665 memcpy(p, s, len);
1666
1667
1668
1669
1670
1671 err = hashmap__insert(btf->strs_hash, (void *)new_off, (void *)new_off,
1672 HASHMAP_ADD, (const void **)&old_off, NULL);
1673 if (err == -EEXIST)
1674 return btf->start_str_off + old_off;
1675 if (err)
1676 return err;
1677
1678 btf->hdr->str_len += len;
1679 return btf->start_str_off + new_off;
1680}
1681
1682static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
1683{
1684 return btf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
1685 btf->hdr->type_len, UINT_MAX, add_sz);
1686}
1687
1688static __u32 btf_type_info(int kind, int vlen, int kflag)
1689{
1690 return (kflag << 31) | (kind << 24) | vlen;
1691}
1692
1693static void btf_type_inc_vlen(struct btf_type *t)
1694{
1695 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
1696}
1697
1698static int btf_commit_type(struct btf *btf, int data_sz)
1699{
1700 int err;
1701
1702 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1703 if (err)
1704 return err;
1705
1706 btf->hdr->type_len += data_sz;
1707 btf->hdr->str_off += data_sz;
1708 btf->nr_types++;
1709 return btf->start_id + btf->nr_types - 1;
1710}
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding)
1722{
1723 struct btf_type *t;
1724 int sz, name_off;
1725
1726
1727 if (!name || !name[0])
1728 return -EINVAL;
1729
1730 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16)
1731 return -EINVAL;
1732 if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL))
1733 return -EINVAL;
1734
1735
1736 if (btf_ensure_modifiable(btf))
1737 return -ENOMEM;
1738
1739 sz = sizeof(struct btf_type) + sizeof(int);
1740 t = btf_add_type_mem(btf, sz);
1741 if (!t)
1742 return -ENOMEM;
1743
1744
1745
1746
1747
1748 name_off = btf__add_str(btf, name);
1749 if (name_off < 0)
1750 return name_off;
1751
1752 t->name_off = name_off;
1753 t->info = btf_type_info(BTF_KIND_INT, 0, 0);
1754 t->size = byte_sz;
1755
1756 *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8);
1757
1758 return btf_commit_type(btf, sz);
1759}
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
1770{
1771 struct btf_type *t;
1772 int sz, name_off;
1773
1774
1775 if (!name || !name[0])
1776 return -EINVAL;
1777
1778
1779 if (byte_sz != 2 && byte_sz != 4 && byte_sz != 8 && byte_sz != 12 &&
1780 byte_sz != 16)
1781 return -EINVAL;
1782
1783 if (btf_ensure_modifiable(btf))
1784 return -ENOMEM;
1785
1786 sz = sizeof(struct btf_type);
1787 t = btf_add_type_mem(btf, sz);
1788 if (!t)
1789 return -ENOMEM;
1790
1791 name_off = btf__add_str(btf, name);
1792 if (name_off < 0)
1793 return name_off;
1794
1795 t->name_off = name_off;
1796 t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
1797 t->size = byte_sz;
1798
1799 return btf_commit_type(btf, sz);
1800}
1801
1802
1803
1804
1805
1806static int validate_type_id(int id)
1807{
1808 if (id < 0 || id > BTF_MAX_NR_TYPES)
1809 return -EINVAL;
1810 return 0;
1811}
1812
1813
1814static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id)
1815{
1816 struct btf_type *t;
1817 int sz, name_off = 0;
1818
1819 if (validate_type_id(ref_type_id))
1820 return -EINVAL;
1821
1822 if (btf_ensure_modifiable(btf))
1823 return -ENOMEM;
1824
1825 sz = sizeof(struct btf_type);
1826 t = btf_add_type_mem(btf, sz);
1827 if (!t)
1828 return -ENOMEM;
1829
1830 if (name && name[0]) {
1831 name_off = btf__add_str(btf, name);
1832 if (name_off < 0)
1833 return name_off;
1834 }
1835
1836 t->name_off = name_off;
1837 t->info = btf_type_info(kind, 0, 0);
1838 t->type = ref_type_id;
1839
1840 return btf_commit_type(btf, sz);
1841}
1842
1843
1844
1845
1846
1847
1848
1849
1850int btf__add_ptr(struct btf *btf, int ref_type_id)
1851{
1852 return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id);
1853}
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems)
1865{
1866 struct btf_type *t;
1867 struct btf_array *a;
1868 int sz;
1869
1870 if (validate_type_id(index_type_id) || validate_type_id(elem_type_id))
1871 return -EINVAL;
1872
1873 if (btf_ensure_modifiable(btf))
1874 return -ENOMEM;
1875
1876 sz = sizeof(struct btf_type) + sizeof(struct btf_array);
1877 t = btf_add_type_mem(btf, sz);
1878 if (!t)
1879 return -ENOMEM;
1880
1881 t->name_off = 0;
1882 t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0);
1883 t->size = 0;
1884
1885 a = btf_array(t);
1886 a->type = elem_type_id;
1887 a->index_type = index_type_id;
1888 a->nelems = nr_elems;
1889
1890 return btf_commit_type(btf, sz);
1891}
1892
1893
1894static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz)
1895{
1896 struct btf_type *t;
1897 int sz, name_off = 0;
1898
1899 if (btf_ensure_modifiable(btf))
1900 return -ENOMEM;
1901
1902 sz = sizeof(struct btf_type);
1903 t = btf_add_type_mem(btf, sz);
1904 if (!t)
1905 return -ENOMEM;
1906
1907 if (name && name[0]) {
1908 name_off = btf__add_str(btf, name);
1909 if (name_off < 0)
1910 return name_off;
1911 }
1912
1913
1914
1915
1916 t->name_off = name_off;
1917 t->info = btf_type_info(kind, 0, 0);
1918 t->size = bytes_sz;
1919
1920 return btf_commit_type(btf, sz);
1921}
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz)
1936{
1937 return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz);
1938}
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
1954{
1955 return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz);
1956}
1957
1958static struct btf_type *btf_last_type(struct btf *btf)
1959{
1960 return btf_type_by_id(btf, btf__get_nr_types(btf));
1961}
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973int btf__add_field(struct btf *btf, const char *name, int type_id,
1974 __u32 bit_offset, __u32 bit_size)
1975{
1976 struct btf_type *t;
1977 struct btf_member *m;
1978 bool is_bitfield;
1979 int sz, name_off = 0;
1980
1981
1982 if (btf->nr_types == 0)
1983 return -EINVAL;
1984 t = btf_last_type(btf);
1985 if (!btf_is_composite(t))
1986 return -EINVAL;
1987
1988 if (validate_type_id(type_id))
1989 return -EINVAL;
1990
1991 is_bitfield = bit_size || (bit_offset % 8 != 0);
1992 if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff))
1993 return -EINVAL;
1994
1995
1996 if (btf_is_union(t) && bit_offset)
1997 return -EINVAL;
1998
1999
2000 if (btf_ensure_modifiable(btf))
2001 return -ENOMEM;
2002
2003 sz = sizeof(struct btf_member);
2004 m = btf_add_type_mem(btf, sz);
2005 if (!m)
2006 return -ENOMEM;
2007
2008 if (name && name[0]) {
2009 name_off = btf__add_str(btf, name);
2010 if (name_off < 0)
2011 return name_off;
2012 }
2013
2014 m->name_off = name_off;
2015 m->type = type_id;
2016 m->offset = bit_offset | (bit_size << 24);
2017
2018
2019 t = btf_last_type(btf);
2020
2021 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
2022
2023 btf->hdr->type_len += sz;
2024 btf->hdr->str_off += sz;
2025 return 0;
2026}
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
2042{
2043 struct btf_type *t;
2044 int sz, name_off = 0;
2045
2046
2047 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
2048 return -EINVAL;
2049
2050 if (btf_ensure_modifiable(btf))
2051 return -ENOMEM;
2052
2053 sz = sizeof(struct btf_type);
2054 t = btf_add_type_mem(btf, sz);
2055 if (!t)
2056 return -ENOMEM;
2057
2058 if (name && name[0]) {
2059 name_off = btf__add_str(btf, name);
2060 if (name_off < 0)
2061 return name_off;
2062 }
2063
2064
2065 t->name_off = name_off;
2066 t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
2067 t->size = byte_sz;
2068
2069 return btf_commit_type(btf, sz);
2070}
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
2081{
2082 struct btf_type *t;
2083 struct btf_enum *v;
2084 int sz, name_off;
2085
2086
2087 if (btf->nr_types == 0)
2088 return -EINVAL;
2089 t = btf_last_type(btf);
2090 if (!btf_is_enum(t))
2091 return -EINVAL;
2092
2093
2094 if (!name || !name[0])
2095 return -EINVAL;
2096 if (value < INT_MIN || value > UINT_MAX)
2097 return -E2BIG;
2098
2099
2100 if (btf_ensure_modifiable(btf))
2101 return -ENOMEM;
2102
2103 sz = sizeof(struct btf_enum);
2104 v = btf_add_type_mem(btf, sz);
2105 if (!v)
2106 return -ENOMEM;
2107
2108 name_off = btf__add_str(btf, name);
2109 if (name_off < 0)
2110 return name_off;
2111
2112 v->name_off = name_off;
2113 v->val = value;
2114
2115
2116 t = btf_last_type(btf);
2117 btf_type_inc_vlen(t);
2118
2119 btf->hdr->type_len += sz;
2120 btf->hdr->str_off += sz;
2121 return 0;
2122}
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
2134{
2135 if (!name || !name[0])
2136 return -EINVAL;
2137
2138 switch (fwd_kind) {
2139 case BTF_FWD_STRUCT:
2140 case BTF_FWD_UNION: {
2141 struct btf_type *t;
2142 int id;
2143
2144 id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0);
2145 if (id <= 0)
2146 return id;
2147 t = btf_type_by_id(btf, id);
2148 t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION);
2149 return id;
2150 }
2151 case BTF_FWD_ENUM:
2152
2153
2154
2155 return btf__add_enum(btf, name, sizeof(int));
2156 default:
2157 return -EINVAL;
2158 }
2159}
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)
2170{
2171 if (!name || !name[0])
2172 return -EINVAL;
2173
2174 return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id);
2175}
2176
2177
2178
2179
2180
2181
2182
2183
2184int btf__add_volatile(struct btf *btf, int ref_type_id)
2185{
2186 return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id);
2187}
2188
2189
2190
2191
2192
2193
2194
2195
2196int btf__add_const(struct btf *btf, int ref_type_id)
2197{
2198 return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id);
2199}
2200
2201
2202
2203
2204
2205
2206
2207
2208int btf__add_restrict(struct btf *btf, int ref_type_id)
2209{
2210 return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
2211}
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221int btf__add_func(struct btf *btf, const char *name,
2222 enum btf_func_linkage linkage, int proto_type_id)
2223{
2224 int id;
2225
2226 if (!name || !name[0])
2227 return -EINVAL;
2228 if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL &&
2229 linkage != BTF_FUNC_EXTERN)
2230 return -EINVAL;
2231
2232 id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id);
2233 if (id > 0) {
2234 struct btf_type *t = btf_type_by_id(btf, id);
2235
2236 t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0);
2237 }
2238 return id;
2239}
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253int btf__add_func_proto(struct btf *btf, int ret_type_id)
2254{
2255 struct btf_type *t;
2256 int sz;
2257
2258 if (validate_type_id(ret_type_id))
2259 return -EINVAL;
2260
2261 if (btf_ensure_modifiable(btf))
2262 return -ENOMEM;
2263
2264 sz = sizeof(struct btf_type);
2265 t = btf_add_type_mem(btf, sz);
2266 if (!t)
2267 return -ENOMEM;
2268
2269
2270
2271
2272 t->name_off = 0;
2273 t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0);
2274 t->type = ret_type_id;
2275
2276 return btf_commit_type(btf, sz);
2277}
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287int btf__add_func_param(struct btf *btf, const char *name, int type_id)
2288{
2289 struct btf_type *t;
2290 struct btf_param *p;
2291 int sz, name_off = 0;
2292
2293 if (validate_type_id(type_id))
2294 return -EINVAL;
2295
2296
2297 if (btf->nr_types == 0)
2298 return -EINVAL;
2299 t = btf_last_type(btf);
2300 if (!btf_is_func_proto(t))
2301 return -EINVAL;
2302
2303
2304 if (btf_ensure_modifiable(btf))
2305 return -ENOMEM;
2306
2307 sz = sizeof(struct btf_param);
2308 p = btf_add_type_mem(btf, sz);
2309 if (!p)
2310 return -ENOMEM;
2311
2312 if (name && name[0]) {
2313 name_off = btf__add_str(btf, name);
2314 if (name_off < 0)
2315 return name_off;
2316 }
2317
2318 p->name_off = name_off;
2319 p->type = type_id;
2320
2321
2322 t = btf_last_type(btf);
2323 btf_type_inc_vlen(t);
2324
2325 btf->hdr->type_len += sz;
2326 btf->hdr->str_off += sz;
2327 return 0;
2328}
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id)
2341{
2342 struct btf_type *t;
2343 struct btf_var *v;
2344 int sz, name_off;
2345
2346
2347 if (!name || !name[0])
2348 return -EINVAL;
2349 if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2350 linkage != BTF_VAR_GLOBAL_EXTERN)
2351 return -EINVAL;
2352 if (validate_type_id(type_id))
2353 return -EINVAL;
2354
2355
2356 if (btf_ensure_modifiable(btf))
2357 return -ENOMEM;
2358
2359 sz = sizeof(struct btf_type) + sizeof(struct btf_var);
2360 t = btf_add_type_mem(btf, sz);
2361 if (!t)
2362 return -ENOMEM;
2363
2364 name_off = btf__add_str(btf, name);
2365 if (name_off < 0)
2366 return name_off;
2367
2368 t->name_off = name_off;
2369 t->info = btf_type_info(BTF_KIND_VAR, 0, 0);
2370 t->type = type_id;
2371
2372 v = btf_var(t);
2373 v->linkage = linkage;
2374
2375 return btf_commit_type(btf, sz);
2376}
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
2391{
2392 struct btf_type *t;
2393 int sz, name_off;
2394
2395
2396 if (!name || !name[0])
2397 return -EINVAL;
2398
2399 if (btf_ensure_modifiable(btf))
2400 return -ENOMEM;
2401
2402 sz = sizeof(struct btf_type);
2403 t = btf_add_type_mem(btf, sz);
2404 if (!t)
2405 return -ENOMEM;
2406
2407 name_off = btf__add_str(btf, name);
2408 if (name_off < 0)
2409 return name_off;
2410
2411
2412 t->name_off = name_off;
2413 t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0);
2414 t->size = byte_sz;
2415
2416 return btf_commit_type(btf, sz);
2417}
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz)
2430{
2431 struct btf_type *t;
2432 struct btf_var_secinfo *v;
2433 int sz;
2434
2435
2436 if (btf->nr_types == 0)
2437 return -EINVAL;
2438 t = btf_last_type(btf);
2439 if (!btf_is_datasec(t))
2440 return -EINVAL;
2441
2442 if (validate_type_id(var_type_id))
2443 return -EINVAL;
2444
2445
2446 if (btf_ensure_modifiable(btf))
2447 return -ENOMEM;
2448
2449 sz = sizeof(struct btf_var_secinfo);
2450 v = btf_add_type_mem(btf, sz);
2451 if (!v)
2452 return -ENOMEM;
2453
2454 v->type = var_type_id;
2455 v->offset = offset;
2456 v->size = byte_sz;
2457
2458
2459 t = btf_last_type(btf);
2460 btf_type_inc_vlen(t);
2461
2462 btf->hdr->type_len += sz;
2463 btf->hdr->str_off += sz;
2464 return 0;
2465}
2466
2467struct btf_ext_sec_setup_param {
2468 __u32 off;
2469 __u32 len;
2470 __u32 min_rec_size;
2471 struct btf_ext_info *ext_info;
2472 const char *desc;
2473};
2474
2475static int btf_ext_setup_info(struct btf_ext *btf_ext,
2476 struct btf_ext_sec_setup_param *ext_sec)
2477{
2478 const struct btf_ext_info_sec *sinfo;
2479 struct btf_ext_info *ext_info;
2480 __u32 info_left, record_size;
2481
2482 void *info;
2483
2484 if (ext_sec->len == 0)
2485 return 0;
2486
2487 if (ext_sec->off & 0x03) {
2488 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
2489 ext_sec->desc);
2490 return -EINVAL;
2491 }
2492
2493 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
2494 info_left = ext_sec->len;
2495
2496 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
2497 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
2498 ext_sec->desc, ext_sec->off, ext_sec->len);
2499 return -EINVAL;
2500 }
2501
2502
2503 if (info_left < sizeof(__u32)) {
2504 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
2505 return -EINVAL;
2506 }
2507
2508
2509 record_size = *(__u32 *)info;
2510 if (record_size < ext_sec->min_rec_size ||
2511 record_size & 0x03) {
2512 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
2513 ext_sec->desc, record_size);
2514 return -EINVAL;
2515 }
2516
2517 sinfo = info + sizeof(__u32);
2518 info_left -= sizeof(__u32);
2519
2520
2521 if (!info_left) {
2522 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
2523 return -EINVAL;
2524 }
2525
2526 while (info_left) {
2527 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
2528 __u64 total_record_size;
2529 __u32 num_records;
2530
2531 if (info_left < sec_hdrlen) {
2532 pr_debug("%s section header is not found in .BTF.ext\n",
2533 ext_sec->desc);
2534 return -EINVAL;
2535 }
2536
2537 num_records = sinfo->num_info;
2538 if (num_records == 0) {
2539 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2540 ext_sec->desc);
2541 return -EINVAL;
2542 }
2543
2544 total_record_size = sec_hdrlen +
2545 (__u64)num_records * record_size;
2546 if (info_left < total_record_size) {
2547 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2548 ext_sec->desc);
2549 return -EINVAL;
2550 }
2551
2552 info_left -= total_record_size;
2553 sinfo = (void *)sinfo + total_record_size;
2554 }
2555
2556 ext_info = ext_sec->ext_info;
2557 ext_info->len = ext_sec->len - sizeof(__u32);
2558 ext_info->rec_size = record_size;
2559 ext_info->info = info + sizeof(__u32);
2560
2561 return 0;
2562}
2563
2564static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
2565{
2566 struct btf_ext_sec_setup_param param = {
2567 .off = btf_ext->hdr->func_info_off,
2568 .len = btf_ext->hdr->func_info_len,
2569 .min_rec_size = sizeof(struct bpf_func_info_min),
2570 .ext_info = &btf_ext->func_info,
2571 .desc = "func_info"
2572 };
2573
2574 return btf_ext_setup_info(btf_ext, ¶m);
2575}
2576
2577static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
2578{
2579 struct btf_ext_sec_setup_param param = {
2580 .off = btf_ext->hdr->line_info_off,
2581 .len = btf_ext->hdr->line_info_len,
2582 .min_rec_size = sizeof(struct bpf_line_info_min),
2583 .ext_info = &btf_ext->line_info,
2584 .desc = "line_info",
2585 };
2586
2587 return btf_ext_setup_info(btf_ext, ¶m);
2588}
2589
2590static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
2591{
2592 struct btf_ext_sec_setup_param param = {
2593 .off = btf_ext->hdr->core_relo_off,
2594 .len = btf_ext->hdr->core_relo_len,
2595 .min_rec_size = sizeof(struct bpf_core_relo),
2596 .ext_info = &btf_ext->core_relo_info,
2597 .desc = "core_relo",
2598 };
2599
2600 return btf_ext_setup_info(btf_ext, ¶m);
2601}
2602
2603static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
2604{
2605 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
2606
2607 if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
2608 data_size < hdr->hdr_len) {
2609 pr_debug("BTF.ext header not found");
2610 return -EINVAL;
2611 }
2612
2613 if (hdr->magic == bswap_16(BTF_MAGIC)) {
2614 pr_warn("BTF.ext in non-native endianness is not supported\n");
2615 return -ENOTSUP;
2616 } else if (hdr->magic != BTF_MAGIC) {
2617 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
2618 return -EINVAL;
2619 }
2620
2621 if (hdr->version != BTF_VERSION) {
2622 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
2623 return -ENOTSUP;
2624 }
2625
2626 if (hdr->flags) {
2627 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
2628 return -ENOTSUP;
2629 }
2630
2631 if (data_size == hdr->hdr_len) {
2632 pr_debug("BTF.ext has no data\n");
2633 return -EINVAL;
2634 }
2635
2636 return 0;
2637}
2638
2639void btf_ext__free(struct btf_ext *btf_ext)
2640{
2641 if (IS_ERR_OR_NULL(btf_ext))
2642 return;
2643 free(btf_ext->data);
2644 free(btf_ext);
2645}
2646
2647struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
2648{
2649 struct btf_ext *btf_ext;
2650 int err;
2651
2652 err = btf_ext_parse_hdr(data, size);
2653 if (err)
2654 return ERR_PTR(err);
2655
2656 btf_ext = calloc(1, sizeof(struct btf_ext));
2657 if (!btf_ext)
2658 return ERR_PTR(-ENOMEM);
2659
2660 btf_ext->data_size = size;
2661 btf_ext->data = malloc(size);
2662 if (!btf_ext->data) {
2663 err = -ENOMEM;
2664 goto done;
2665 }
2666 memcpy(btf_ext->data, data, size);
2667
2668 if (btf_ext->hdr->hdr_len <
2669 offsetofend(struct btf_ext_header, line_info_len))
2670 goto done;
2671 err = btf_ext_setup_func_info(btf_ext);
2672 if (err)
2673 goto done;
2674
2675 err = btf_ext_setup_line_info(btf_ext);
2676 if (err)
2677 goto done;
2678
2679 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
2680 goto done;
2681 err = btf_ext_setup_core_relos(btf_ext);
2682 if (err)
2683 goto done;
2684
2685done:
2686 if (err) {
2687 btf_ext__free(btf_ext);
2688 return ERR_PTR(err);
2689 }
2690
2691 return btf_ext;
2692}
2693
2694const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
2695{
2696 *size = btf_ext->data_size;
2697 return btf_ext->data;
2698}
2699
2700static int btf_ext_reloc_info(const struct btf *btf,
2701 const struct btf_ext_info *ext_info,
2702 const char *sec_name, __u32 insns_cnt,
2703 void **info, __u32 *cnt)
2704{
2705 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
2706 __u32 i, record_size, existing_len, records_len;
2707 struct btf_ext_info_sec *sinfo;
2708 const char *info_sec_name;
2709 __u64 remain_len;
2710 void *data;
2711
2712 record_size = ext_info->rec_size;
2713 sinfo = ext_info->info;
2714 remain_len = ext_info->len;
2715 while (remain_len > 0) {
2716 records_len = sinfo->num_info * record_size;
2717 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
2718 if (strcmp(info_sec_name, sec_name)) {
2719 remain_len -= sec_hdrlen + records_len;
2720 sinfo = (void *)sinfo + sec_hdrlen + records_len;
2721 continue;
2722 }
2723
2724 existing_len = (*cnt) * record_size;
2725 data = realloc(*info, existing_len + records_len);
2726 if (!data)
2727 return -ENOMEM;
2728
2729 memcpy(data + existing_len, sinfo->data, records_len);
2730
2731
2732
2733 for (i = 0; i < sinfo->num_info; i++) {
2734 __u32 *insn_off;
2735
2736 insn_off = data + existing_len + (i * record_size);
2737 *insn_off = *insn_off / sizeof(struct bpf_insn) +
2738 insns_cnt;
2739 }
2740 *info = data;
2741 *cnt += sinfo->num_info;
2742 return 0;
2743 }
2744
2745 return -ENOENT;
2746}
2747
2748int btf_ext__reloc_func_info(const struct btf *btf,
2749 const struct btf_ext *btf_ext,
2750 const char *sec_name, __u32 insns_cnt,
2751 void **func_info, __u32 *cnt)
2752{
2753 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
2754 insns_cnt, func_info, cnt);
2755}
2756
2757int btf_ext__reloc_line_info(const struct btf *btf,
2758 const struct btf_ext *btf_ext,
2759 const char *sec_name, __u32 insns_cnt,
2760 void **line_info, __u32 *cnt)
2761{
2762 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
2763 insns_cnt, line_info, cnt);
2764}
2765
2766__u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
2767{
2768 return btf_ext->func_info.rec_size;
2769}
2770
2771__u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
2772{
2773 return btf_ext->line_info.rec_size;
2774}
2775
2776struct btf_dedup;
2777
2778static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
2779 const struct btf_dedup_opts *opts);
2780static void btf_dedup_free(struct btf_dedup *d);
2781static int btf_dedup_prep(struct btf_dedup *d);
2782static int btf_dedup_strings(struct btf_dedup *d);
2783static int btf_dedup_prim_types(struct btf_dedup *d);
2784static int btf_dedup_struct_types(struct btf_dedup *d);
2785static int btf_dedup_ref_types(struct btf_dedup *d);
2786static int btf_dedup_compact_types(struct btf_dedup *d);
2787static int btf_dedup_remap_types(struct btf_dedup *d);
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
2927 const struct btf_dedup_opts *opts)
2928{
2929 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
2930 int err;
2931
2932 if (IS_ERR(d)) {
2933 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
2934 return -EINVAL;
2935 }
2936
2937 if (btf_ensure_modifiable(btf))
2938 return -ENOMEM;
2939
2940 err = btf_dedup_prep(d);
2941 if (err) {
2942 pr_debug("btf_dedup_prep failed:%d\n", err);
2943 goto done;
2944 }
2945 err = btf_dedup_strings(d);
2946 if (err < 0) {
2947 pr_debug("btf_dedup_strings failed:%d\n", err);
2948 goto done;
2949 }
2950 err = btf_dedup_prim_types(d);
2951 if (err < 0) {
2952 pr_debug("btf_dedup_prim_types failed:%d\n", err);
2953 goto done;
2954 }
2955 err = btf_dedup_struct_types(d);
2956 if (err < 0) {
2957 pr_debug("btf_dedup_struct_types failed:%d\n", err);
2958 goto done;
2959 }
2960 err = btf_dedup_ref_types(d);
2961 if (err < 0) {
2962 pr_debug("btf_dedup_ref_types failed:%d\n", err);
2963 goto done;
2964 }
2965 err = btf_dedup_compact_types(d);
2966 if (err < 0) {
2967 pr_debug("btf_dedup_compact_types failed:%d\n", err);
2968 goto done;
2969 }
2970 err = btf_dedup_remap_types(d);
2971 if (err < 0) {
2972 pr_debug("btf_dedup_remap_types failed:%d\n", err);
2973 goto done;
2974 }
2975
2976done:
2977 btf_dedup_free(d);
2978 return err;
2979}
2980
2981#define BTF_UNPROCESSED_ID ((__u32)-1)
2982#define BTF_IN_PROGRESS_ID ((__u32)-2)
2983
2984struct btf_dedup {
2985
2986 struct btf *btf;
2987
2988
2989
2990
2991 struct btf_ext *btf_ext;
2992
2993
2994
2995
2996
2997
2998
2999 struct hashmap *dedup_table;
3000
3001 __u32 *map;
3002
3003 __u32 *hypot_map;
3004 __u32 *hypot_list;
3005 size_t hypot_cnt;
3006 size_t hypot_cap;
3007
3008
3009
3010
3011
3012
3013 bool hypot_adjust_canon;
3014
3015 struct btf_dedup_opts opts;
3016
3017 void *strs_data;
3018 size_t strs_cap;
3019 size_t strs_len;
3020 struct hashmap* strs_hash;
3021};
3022
3023static long hash_combine(long h, long value)
3024{
3025 return h * 31 + value;
3026}
3027
3028#define for_each_dedup_cand(d, node, hash) \
3029 hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
3030
3031static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
3032{
3033 return hashmap__append(d->dedup_table,
3034 (void *)hash, (void *)(long)type_id);
3035}
3036
3037static int btf_dedup_hypot_map_add(struct btf_dedup *d,
3038 __u32 from_id, __u32 to_id)
3039{
3040 if (d->hypot_cnt == d->hypot_cap) {
3041 __u32 *new_list;
3042
3043 d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
3044 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
3045 if (!new_list)
3046 return -ENOMEM;
3047 d->hypot_list = new_list;
3048 }
3049 d->hypot_list[d->hypot_cnt++] = from_id;
3050 d->hypot_map[from_id] = to_id;
3051 return 0;
3052}
3053
3054static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
3055{
3056 int i;
3057
3058 for (i = 0; i < d->hypot_cnt; i++)
3059 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
3060 d->hypot_cnt = 0;
3061 d->hypot_adjust_canon = false;
3062}
3063
3064static void btf_dedup_free(struct btf_dedup *d)
3065{
3066 hashmap__free(d->dedup_table);
3067 d->dedup_table = NULL;
3068
3069 free(d->map);
3070 d->map = NULL;
3071
3072 free(d->hypot_map);
3073 d->hypot_map = NULL;
3074
3075 free(d->hypot_list);
3076 d->hypot_list = NULL;
3077
3078 free(d);
3079}
3080
3081static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
3082{
3083 return (size_t)key;
3084}
3085
3086static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
3087{
3088 return 0;
3089}
3090
3091static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
3092{
3093 return k1 == k2;
3094}
3095
3096static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
3097 const struct btf_dedup_opts *opts)
3098{
3099 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
3100 hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
3101 int i, err = 0, type_cnt;
3102
3103 if (!d)
3104 return ERR_PTR(-ENOMEM);
3105
3106 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
3107
3108 if (opts && opts->dedup_table_size == 1)
3109 hash_fn = btf_dedup_collision_hash_fn;
3110
3111 d->btf = btf;
3112 d->btf_ext = btf_ext;
3113
3114 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
3115 if (IS_ERR(d->dedup_table)) {
3116 err = PTR_ERR(d->dedup_table);
3117 d->dedup_table = NULL;
3118 goto done;
3119 }
3120
3121 type_cnt = btf__get_nr_types(btf) + 1;
3122 d->map = malloc(sizeof(__u32) * type_cnt);
3123 if (!d->map) {
3124 err = -ENOMEM;
3125 goto done;
3126 }
3127
3128 d->map[0] = 0;
3129 for (i = 1; i < type_cnt; i++) {
3130 struct btf_type *t = btf_type_by_id(d->btf, i);
3131
3132
3133 if (btf_is_var(t) || btf_is_datasec(t))
3134 d->map[i] = i;
3135 else
3136 d->map[i] = BTF_UNPROCESSED_ID;
3137 }
3138
3139 d->hypot_map = malloc(sizeof(__u32) * type_cnt);
3140 if (!d->hypot_map) {
3141 err = -ENOMEM;
3142 goto done;
3143 }
3144 for (i = 0; i < type_cnt; i++)
3145 d->hypot_map[i] = BTF_UNPROCESSED_ID;
3146
3147done:
3148 if (err) {
3149 btf_dedup_free(d);
3150 return ERR_PTR(err);
3151 }
3152
3153 return d;
3154}
3155
3156typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
3157
3158
3159
3160
3161
3162static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
3163{
3164 void *line_data_cur, *line_data_end;
3165 int i, j, r, rec_size;
3166 struct btf_type *t;
3167
3168 for (i = 0; i < d->btf->nr_types; i++) {
3169 t = btf_type_by_id(d->btf, d->btf->start_id + i);
3170 r = fn(&t->name_off, ctx);
3171 if (r)
3172 return r;
3173
3174 switch (btf_kind(t)) {
3175 case BTF_KIND_STRUCT:
3176 case BTF_KIND_UNION: {
3177 struct btf_member *m = btf_members(t);
3178 __u16 vlen = btf_vlen(t);
3179
3180 for (j = 0; j < vlen; j++) {
3181 r = fn(&m->name_off, ctx);
3182 if (r)
3183 return r;
3184 m++;
3185 }
3186 break;
3187 }
3188 case BTF_KIND_ENUM: {
3189 struct btf_enum *m = btf_enum(t);
3190 __u16 vlen = btf_vlen(t);
3191
3192 for (j = 0; j < vlen; j++) {
3193 r = fn(&m->name_off, ctx);
3194 if (r)
3195 return r;
3196 m++;
3197 }
3198 break;
3199 }
3200 case BTF_KIND_FUNC_PROTO: {
3201 struct btf_param *m = btf_params(t);
3202 __u16 vlen = btf_vlen(t);
3203
3204 for (j = 0; j < vlen; j++) {
3205 r = fn(&m->name_off, ctx);
3206 if (r)
3207 return r;
3208 m++;
3209 }
3210 break;
3211 }
3212 default:
3213 break;
3214 }
3215 }
3216
3217 if (!d->btf_ext)
3218 return 0;
3219
3220 line_data_cur = d->btf_ext->line_info.info;
3221 line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
3222 rec_size = d->btf_ext->line_info.rec_size;
3223
3224 while (line_data_cur < line_data_end) {
3225 struct btf_ext_info_sec *sec = line_data_cur;
3226 struct bpf_line_info_min *line_info;
3227 __u32 num_info = sec->num_info;
3228
3229 r = fn(&sec->sec_name_off, ctx);
3230 if (r)
3231 return r;
3232
3233 line_data_cur += sizeof(struct btf_ext_info_sec);
3234 for (i = 0; i < num_info; i++) {
3235 line_info = line_data_cur;
3236 r = fn(&line_info->file_name_off, ctx);
3237 if (r)
3238 return r;
3239 r = fn(&line_info->line_off, ctx);
3240 if (r)
3241 return r;
3242 line_data_cur += rec_size;
3243 }
3244 }
3245
3246 return 0;
3247}
3248
3249static int strs_dedup_remap_str_off(__u32 *str_off_ptr, void *ctx)
3250{
3251 struct btf_dedup *d = ctx;
3252 __u32 str_off = *str_off_ptr;
3253 long old_off, new_off, len;
3254 const char *s;
3255 void *p;
3256 int err;
3257
3258
3259 if (str_off == 0 || str_off < d->btf->start_str_off)
3260 return 0;
3261
3262 s = btf__str_by_offset(d->btf, str_off);
3263 if (d->btf->base_btf) {
3264 err = btf__find_str(d->btf->base_btf, s);
3265 if (err >= 0) {
3266 *str_off_ptr = err;
3267 return 0;
3268 }
3269 if (err != -ENOENT)
3270 return err;
3271 }
3272
3273 len = strlen(s) + 1;
3274
3275 new_off = d->strs_len;
3276 p = btf_add_mem(&d->strs_data, &d->strs_cap, 1, new_off, BTF_MAX_STR_OFFSET, len);
3277 if (!p)
3278 return -ENOMEM;
3279
3280 memcpy(p, s, len);
3281
3282
3283
3284
3285
3286 err = hashmap__insert(d->strs_hash, (void *)new_off, (void *)new_off,
3287 HASHMAP_ADD, (const void **)&old_off, NULL);
3288 if (err == -EEXIST) {
3289 *str_off_ptr = d->btf->start_str_off + old_off;
3290 } else if (err) {
3291 return err;
3292 } else {
3293 *str_off_ptr = d->btf->start_str_off + new_off;
3294 d->strs_len += len;
3295 }
3296 return 0;
3297}
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310static int btf_dedup_strings(struct btf_dedup *d)
3311{
3312 char *s;
3313 int err;
3314
3315 if (d->btf->strs_deduped)
3316 return 0;
3317
3318
3319
3320
3321
3322 d->btf->strs_data_ptr = &d->strs_data;
3323
3324 d->strs_hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, d->btf);
3325 if (IS_ERR(d->strs_hash)) {
3326 err = PTR_ERR(d->strs_hash);
3327 d->strs_hash = NULL;
3328 goto err_out;
3329 }
3330
3331 if (!d->btf->base_btf) {
3332 s = btf_add_mem(&d->strs_data, &d->strs_cap, 1, d->strs_len, BTF_MAX_STR_OFFSET, 1);
3333 if (!s)
3334 return -ENOMEM;
3335
3336 s[0] = 0;
3337 d->strs_len = 1;
3338
3339
3340
3341
3342 err = hashmap__insert(d->strs_hash, (void *)0, (void *)0,
3343 HASHMAP_ADD, NULL, NULL);
3344 if (err)
3345 goto err_out;
3346 }
3347
3348
3349 err = btf_for_each_str_off(d, strs_dedup_remap_str_off, d);
3350 if (err)
3351 goto err_out;
3352
3353
3354 free(d->btf->strs_data);
3355 hashmap__free(d->btf->strs_hash);
3356 d->btf->strs_data = d->strs_data;
3357 d->btf->strs_data_cap = d->strs_cap;
3358 d->btf->hdr->str_len = d->strs_len;
3359 d->btf->strs_hash = d->strs_hash;
3360
3361 d->btf->strs_data_ptr = &d->btf->strs_data;
3362
3363 d->strs_data = d->strs_hash = NULL;
3364 d->strs_len = d->strs_cap = 0;
3365 d->btf->strs_deduped = true;
3366 return 0;
3367
3368err_out:
3369 free(d->strs_data);
3370 hashmap__free(d->strs_hash);
3371 d->strs_data = d->strs_hash = NULL;
3372 d->strs_len = d->strs_cap = 0;
3373
3374
3375 d->btf->strs_data_ptr = &d->strs_data;
3376
3377 return err;
3378}
3379
3380static long btf_hash_common(struct btf_type *t)
3381{
3382 long h;
3383
3384 h = hash_combine(0, t->name_off);
3385 h = hash_combine(h, t->info);
3386 h = hash_combine(h, t->size);
3387 return h;
3388}
3389
3390static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
3391{
3392 return t1->name_off == t2->name_off &&
3393 t1->info == t2->info &&
3394 t1->size == t2->size;
3395}
3396
3397
3398static long btf_hash_int(struct btf_type *t)
3399{
3400 __u32 info = *(__u32 *)(t + 1);
3401 long h;
3402
3403 h = btf_hash_common(t);
3404 h = hash_combine(h, info);
3405 return h;
3406}
3407
3408
3409static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
3410{
3411 __u32 info1, info2;
3412
3413 if (!btf_equal_common(t1, t2))
3414 return false;
3415 info1 = *(__u32 *)(t1 + 1);
3416 info2 = *(__u32 *)(t2 + 1);
3417 return info1 == info2;
3418}
3419
3420
3421static long btf_hash_enum(struct btf_type *t)
3422{
3423 long h;
3424
3425
3426 h = hash_combine(0, t->name_off);
3427 h = hash_combine(h, t->info & ~0xffff);
3428 h = hash_combine(h, t->size);
3429 return h;
3430}
3431
3432
3433static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
3434{
3435 const struct btf_enum *m1, *m2;
3436 __u16 vlen;
3437 int i;
3438
3439 if (!btf_equal_common(t1, t2))
3440 return false;
3441
3442 vlen = btf_vlen(t1);
3443 m1 = btf_enum(t1);
3444 m2 = btf_enum(t2);
3445 for (i = 0; i < vlen; i++) {
3446 if (m1->name_off != m2->name_off || m1->val != m2->val)
3447 return false;
3448 m1++;
3449 m2++;
3450 }
3451 return true;
3452}
3453
3454static inline bool btf_is_enum_fwd(struct btf_type *t)
3455{
3456 return btf_is_enum(t) && btf_vlen(t) == 0;
3457}
3458
3459static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3460{
3461 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3462 return btf_equal_enum(t1, t2);
3463
3464 return t1->name_off == t2->name_off &&
3465 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3466 t1->size == t2->size;
3467}
3468
3469
3470
3471
3472
3473
3474static long btf_hash_struct(struct btf_type *t)
3475{
3476 const struct btf_member *member = btf_members(t);
3477 __u32 vlen = btf_vlen(t);
3478 long h = btf_hash_common(t);
3479 int i;
3480
3481 for (i = 0; i < vlen; i++) {
3482 h = hash_combine(h, member->name_off);
3483 h = hash_combine(h, member->offset);
3484
3485 member++;
3486 }
3487 return h;
3488}
3489
3490
3491
3492
3493
3494
3495static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
3496{
3497 const struct btf_member *m1, *m2;
3498 __u16 vlen;
3499 int i;
3500
3501 if (!btf_equal_common(t1, t2))
3502 return false;
3503
3504 vlen = btf_vlen(t1);
3505 m1 = btf_members(t1);
3506 m2 = btf_members(t2);
3507 for (i = 0; i < vlen; i++) {
3508 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
3509 return false;
3510 m1++;
3511 m2++;
3512 }
3513 return true;
3514}
3515
3516
3517
3518
3519
3520
3521static long btf_hash_array(struct btf_type *t)
3522{
3523 const struct btf_array *info = btf_array(t);
3524 long h = btf_hash_common(t);
3525
3526 h = hash_combine(h, info->type);
3527 h = hash_combine(h, info->index_type);
3528 h = hash_combine(h, info->nelems);
3529 return h;
3530}
3531
3532
3533
3534
3535
3536
3537
3538
3539static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
3540{
3541 const struct btf_array *info1, *info2;
3542
3543 if (!btf_equal_common(t1, t2))
3544 return false;
3545
3546 info1 = btf_array(t1);
3547 info2 = btf_array(t2);
3548 return info1->type == info2->type &&
3549 info1->index_type == info2->index_type &&
3550 info1->nelems == info2->nelems;
3551}
3552
3553
3554
3555
3556
3557
3558static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
3559{
3560 if (!btf_equal_common(t1, t2))
3561 return false;
3562
3563 return btf_array(t1)->nelems == btf_array(t2)->nelems;
3564}
3565
3566
3567
3568
3569
3570
3571static long btf_hash_fnproto(struct btf_type *t)
3572{
3573 const struct btf_param *member = btf_params(t);
3574 __u16 vlen = btf_vlen(t);
3575 long h = btf_hash_common(t);
3576 int i;
3577
3578 for (i = 0; i < vlen; i++) {
3579 h = hash_combine(h, member->name_off);
3580 h = hash_combine(h, member->type);
3581 member++;
3582 }
3583 return h;
3584}
3585
3586
3587
3588
3589
3590
3591
3592
3593static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
3594{
3595 const struct btf_param *m1, *m2;
3596 __u16 vlen;
3597 int i;
3598
3599 if (!btf_equal_common(t1, t2))
3600 return false;
3601
3602 vlen = btf_vlen(t1);
3603 m1 = btf_params(t1);
3604 m2 = btf_params(t2);
3605 for (i = 0; i < vlen; i++) {
3606 if (m1->name_off != m2->name_off || m1->type != m2->type)
3607 return false;
3608 m1++;
3609 m2++;
3610 }
3611 return true;
3612}
3613
3614
3615
3616
3617
3618
3619static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
3620{
3621 const struct btf_param *m1, *m2;
3622 __u16 vlen;
3623 int i;
3624
3625
3626 if (t1->name_off != t2->name_off || t1->info != t2->info)
3627 return false;
3628
3629 vlen = btf_vlen(t1);
3630 m1 = btf_params(t1);
3631 m2 = btf_params(t2);
3632 for (i = 0; i < vlen; i++) {
3633 if (m1->name_off != m2->name_off)
3634 return false;
3635 m1++;
3636 m2++;
3637 }
3638 return true;
3639}
3640
3641
3642
3643
3644
3645static int btf_dedup_prep(struct btf_dedup *d)
3646{
3647 struct btf_type *t;
3648 int type_id;
3649 long h;
3650
3651 if (!d->btf->base_btf)
3652 return 0;
3653
3654 for (type_id = 1; type_id < d->btf->start_id; type_id++) {
3655 t = btf_type_by_id(d->btf, type_id);
3656
3657
3658 d->map[type_id] = type_id;
3659
3660 switch (btf_kind(t)) {
3661 case BTF_KIND_VAR:
3662 case BTF_KIND_DATASEC:
3663
3664 continue;
3665 case BTF_KIND_CONST:
3666 case BTF_KIND_VOLATILE:
3667 case BTF_KIND_RESTRICT:
3668 case BTF_KIND_PTR:
3669 case BTF_KIND_FWD:
3670 case BTF_KIND_TYPEDEF:
3671 case BTF_KIND_FUNC:
3672 case BTF_KIND_FLOAT:
3673 h = btf_hash_common(t);
3674 break;
3675 case BTF_KIND_INT:
3676 h = btf_hash_int(t);
3677 break;
3678 case BTF_KIND_ENUM:
3679 h = btf_hash_enum(t);
3680 break;
3681 case BTF_KIND_STRUCT:
3682 case BTF_KIND_UNION:
3683 h = btf_hash_struct(t);
3684 break;
3685 case BTF_KIND_ARRAY:
3686 h = btf_hash_array(t);
3687 break;
3688 case BTF_KIND_FUNC_PROTO:
3689 h = btf_hash_fnproto(t);
3690 break;
3691 default:
3692 pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id);
3693 return -EINVAL;
3694 }
3695 if (btf_dedup_table_add(d, h, type_id))
3696 return -ENOMEM;
3697 }
3698
3699 return 0;
3700}
3701
3702
3703
3704
3705
3706
3707
3708static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
3709{
3710 struct btf_type *t = btf_type_by_id(d->btf, type_id);
3711 struct hashmap_entry *hash_entry;
3712 struct btf_type *cand;
3713
3714 __u32 new_id = type_id;
3715 __u32 cand_id;
3716 long h;
3717
3718 switch (btf_kind(t)) {
3719 case BTF_KIND_CONST:
3720 case BTF_KIND_VOLATILE:
3721 case BTF_KIND_RESTRICT:
3722 case BTF_KIND_PTR:
3723 case BTF_KIND_TYPEDEF:
3724 case BTF_KIND_ARRAY:
3725 case BTF_KIND_STRUCT:
3726 case BTF_KIND_UNION:
3727 case BTF_KIND_FUNC:
3728 case BTF_KIND_FUNC_PROTO:
3729 case BTF_KIND_VAR:
3730 case BTF_KIND_DATASEC:
3731 return 0;
3732
3733 case BTF_KIND_INT:
3734 h = btf_hash_int(t);
3735 for_each_dedup_cand(d, hash_entry, h) {
3736 cand_id = (__u32)(long)hash_entry->value;
3737 cand = btf_type_by_id(d->btf, cand_id);
3738 if (btf_equal_int(t, cand)) {
3739 new_id = cand_id;
3740 break;
3741 }
3742 }
3743 break;
3744
3745 case BTF_KIND_ENUM:
3746 h = btf_hash_enum(t);
3747 for_each_dedup_cand(d, hash_entry, h) {
3748 cand_id = (__u32)(long)hash_entry->value;
3749 cand = btf_type_by_id(d->btf, cand_id);
3750 if (btf_equal_enum(t, cand)) {
3751 new_id = cand_id;
3752 break;
3753 }
3754 if (d->opts.dont_resolve_fwds)
3755 continue;
3756 if (btf_compat_enum(t, cand)) {
3757 if (btf_is_enum_fwd(t)) {
3758
3759 new_id = cand_id;
3760 break;
3761 }
3762
3763 d->map[cand_id] = type_id;
3764 }
3765 }
3766 break;
3767
3768 case BTF_KIND_FWD:
3769 case BTF_KIND_FLOAT:
3770 h = btf_hash_common(t);
3771 for_each_dedup_cand(d, hash_entry, h) {
3772 cand_id = (__u32)(long)hash_entry->value;
3773 cand = btf_type_by_id(d->btf, cand_id);
3774 if (btf_equal_common(t, cand)) {
3775 new_id = cand_id;
3776 break;
3777 }
3778 }
3779 break;
3780
3781 default:
3782 return -EINVAL;
3783 }
3784
3785 d->map[type_id] = new_id;
3786 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
3787 return -ENOMEM;
3788
3789 return 0;
3790}
3791
3792static int btf_dedup_prim_types(struct btf_dedup *d)
3793{
3794 int i, err;
3795
3796 for (i = 0; i < d->btf->nr_types; i++) {
3797 err = btf_dedup_prim_type(d, d->btf->start_id + i);
3798 if (err)
3799 return err;
3800 }
3801 return 0;
3802}
3803
3804
3805
3806
3807static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
3808{
3809 return d->map[type_id] <= BTF_MAX_NR_TYPES;
3810}
3811
3812
3813
3814
3815
3816
3817static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
3818{
3819 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3820 type_id = d->map[type_id];
3821 return type_id;
3822}
3823
3824
3825
3826
3827
3828static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
3829{
3830 __u32 orig_type_id = type_id;
3831
3832 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3833 return type_id;
3834
3835 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3836 type_id = d->map[type_id];
3837
3838 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3839 return type_id;
3840
3841 return orig_type_id;
3842}
3843
3844
3845static inline __u16 btf_fwd_kind(struct btf_type *t)
3846{
3847 return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
3848}
3849
3850
3851static int btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
3852{
3853 struct btf_type *t1, *t2;
3854
3855 t1 = btf_type_by_id(d->btf, id1);
3856 t2 = btf_type_by_id(d->btf, id2);
3857 if (!btf_is_array(t1) || !btf_is_array(t2))
3858 return 0;
3859
3860 return btf_equal_array(t1, t2);
3861}
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
3957 __u32 canon_id)
3958{
3959 struct btf_type *cand_type;
3960 struct btf_type *canon_type;
3961 __u32 hypot_type_id;
3962 __u16 cand_kind;
3963 __u16 canon_kind;
3964 int i, eq;
3965
3966
3967 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
3968 return 1;
3969
3970 canon_id = resolve_fwd_id(d, canon_id);
3971
3972 hypot_type_id = d->hypot_map[canon_id];
3973 if (hypot_type_id <= BTF_MAX_NR_TYPES) {
3974
3975
3976
3977
3978
3979
3980
3981
3982 return hypot_type_id == cand_id ||
3983 btf_dedup_identical_arrays(d, hypot_type_id, cand_id);
3984 }
3985
3986 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
3987 return -ENOMEM;
3988
3989 cand_type = btf_type_by_id(d->btf, cand_id);
3990 canon_type = btf_type_by_id(d->btf, canon_id);
3991 cand_kind = btf_kind(cand_type);
3992 canon_kind = btf_kind(canon_type);
3993
3994 if (cand_type->name_off != canon_type->name_off)
3995 return 0;
3996
3997
3998 if (!d->opts.dont_resolve_fwds
3999 && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
4000 && cand_kind != canon_kind) {
4001 __u16 real_kind;
4002 __u16 fwd_kind;
4003
4004 if (cand_kind == BTF_KIND_FWD) {
4005 real_kind = canon_kind;
4006 fwd_kind = btf_fwd_kind(cand_type);
4007 } else {
4008 real_kind = cand_kind;
4009 fwd_kind = btf_fwd_kind(canon_type);
4010
4011 if (fwd_kind == real_kind && canon_id < d->btf->start_id)
4012 d->hypot_adjust_canon = true;
4013 }
4014 return fwd_kind == real_kind;
4015 }
4016
4017 if (cand_kind != canon_kind)
4018 return 0;
4019
4020 switch (cand_kind) {
4021 case BTF_KIND_INT:
4022 return btf_equal_int(cand_type, canon_type);
4023
4024 case BTF_KIND_ENUM:
4025 if (d->opts.dont_resolve_fwds)
4026 return btf_equal_enum(cand_type, canon_type);
4027 else
4028 return btf_compat_enum(cand_type, canon_type);
4029
4030 case BTF_KIND_FWD:
4031 case BTF_KIND_FLOAT:
4032 return btf_equal_common(cand_type, canon_type);
4033
4034 case BTF_KIND_CONST:
4035 case BTF_KIND_VOLATILE:
4036 case BTF_KIND_RESTRICT:
4037 case BTF_KIND_PTR:
4038 case BTF_KIND_TYPEDEF:
4039 case BTF_KIND_FUNC:
4040 if (cand_type->info != canon_type->info)
4041 return 0;
4042 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4043
4044 case BTF_KIND_ARRAY: {
4045 const struct btf_array *cand_arr, *canon_arr;
4046
4047 if (!btf_compat_array(cand_type, canon_type))
4048 return 0;
4049 cand_arr = btf_array(cand_type);
4050 canon_arr = btf_array(canon_type);
4051 eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type);
4052 if (eq <= 0)
4053 return eq;
4054 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
4055 }
4056
4057 case BTF_KIND_STRUCT:
4058 case BTF_KIND_UNION: {
4059 const struct btf_member *cand_m, *canon_m;
4060 __u16 vlen;
4061
4062 if (!btf_shallow_equal_struct(cand_type, canon_type))
4063 return 0;
4064 vlen = btf_vlen(cand_type);
4065 cand_m = btf_members(cand_type);
4066 canon_m = btf_members(canon_type);
4067 for (i = 0; i < vlen; i++) {
4068 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
4069 if (eq <= 0)
4070 return eq;
4071 cand_m++;
4072 canon_m++;
4073 }
4074
4075 return 1;
4076 }
4077
4078 case BTF_KIND_FUNC_PROTO: {
4079 const struct btf_param *cand_p, *canon_p;
4080 __u16 vlen;
4081
4082 if (!btf_compat_fnproto(cand_type, canon_type))
4083 return 0;
4084 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4085 if (eq <= 0)
4086 return eq;
4087 vlen = btf_vlen(cand_type);
4088 cand_p = btf_params(cand_type);
4089 canon_p = btf_params(canon_type);
4090 for (i = 0; i < vlen; i++) {
4091 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
4092 if (eq <= 0)
4093 return eq;
4094 cand_p++;
4095 canon_p++;
4096 }
4097 return 1;
4098 }
4099
4100 default:
4101 return -EINVAL;
4102 }
4103 return 0;
4104}
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
4133{
4134 __u32 canon_type_id, targ_type_id;
4135 __u16 t_kind, c_kind;
4136 __u32 t_id, c_id;
4137 int i;
4138
4139 for (i = 0; i < d->hypot_cnt; i++) {
4140 canon_type_id = d->hypot_list[i];
4141 targ_type_id = d->hypot_map[canon_type_id];
4142 t_id = resolve_type_id(d, targ_type_id);
4143 c_id = resolve_type_id(d, canon_type_id);
4144 t_kind = btf_kind(btf__type_by_id(d->btf, t_id));
4145 c_kind = btf_kind(btf__type_by_id(d->btf, c_id));
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
4167 d->map[c_id] = t_id;
4168
4169
4170
4171
4172
4173
4174 if (d->hypot_adjust_canon)
4175 continue;
4176
4177 if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
4178 d->map[t_id] = c_id;
4179
4180 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
4181 c_kind != BTF_KIND_FWD &&
4182 is_type_mapped(d, c_id) &&
4183 !is_type_mapped(d, t_id)) {
4184
4185
4186
4187
4188
4189
4190 d->map[t_id] = c_id;
4191 }
4192 }
4193}
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
4218{
4219 struct btf_type *cand_type, *t;
4220 struct hashmap_entry *hash_entry;
4221
4222 __u32 new_id = type_id;
4223 __u16 kind;
4224 long h;
4225
4226
4227 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4228 return 0;
4229
4230 t = btf_type_by_id(d->btf, type_id);
4231 kind = btf_kind(t);
4232
4233 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4234 return 0;
4235
4236 h = btf_hash_struct(t);
4237 for_each_dedup_cand(d, hash_entry, h) {
4238 __u32 cand_id = (__u32)(long)hash_entry->value;
4239 int eq;
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251 cand_type = btf_type_by_id(d->btf, cand_id);
4252 if (!btf_shallow_equal_struct(t, cand_type))
4253 continue;
4254
4255 btf_dedup_clear_hypot_map(d);
4256 eq = btf_dedup_is_equiv(d, type_id, cand_id);
4257 if (eq < 0)
4258 return eq;
4259 if (!eq)
4260 continue;
4261 btf_dedup_merge_hypot_map(d);
4262 if (d->hypot_adjust_canon)
4263 continue;
4264 new_id = cand_id;
4265 break;
4266 }
4267
4268 d->map[type_id] = new_id;
4269 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4270 return -ENOMEM;
4271
4272 return 0;
4273}
4274
4275static int btf_dedup_struct_types(struct btf_dedup *d)
4276{
4277 int i, err;
4278
4279 for (i = 0; i < d->btf->nr_types; i++) {
4280 err = btf_dedup_struct_type(d, d->btf->start_id + i);
4281 if (err)
4282 return err;
4283 }
4284 return 0;
4285}
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
4312{
4313 struct hashmap_entry *hash_entry;
4314 __u32 new_id = type_id, cand_id;
4315 struct btf_type *t, *cand;
4316
4317 int ref_type_id;
4318 long h;
4319
4320 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
4321 return -ELOOP;
4322 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4323 return resolve_type_id(d, type_id);
4324
4325 t = btf_type_by_id(d->btf, type_id);
4326 d->map[type_id] = BTF_IN_PROGRESS_ID;
4327
4328 switch (btf_kind(t)) {
4329 case BTF_KIND_CONST:
4330 case BTF_KIND_VOLATILE:
4331 case BTF_KIND_RESTRICT:
4332 case BTF_KIND_PTR:
4333 case BTF_KIND_TYPEDEF:
4334 case BTF_KIND_FUNC:
4335 ref_type_id = btf_dedup_ref_type(d, t->type);
4336 if (ref_type_id < 0)
4337 return ref_type_id;
4338 t->type = ref_type_id;
4339
4340 h = btf_hash_common(t);
4341 for_each_dedup_cand(d, hash_entry, h) {
4342 cand_id = (__u32)(long)hash_entry->value;
4343 cand = btf_type_by_id(d->btf, cand_id);
4344 if (btf_equal_common(t, cand)) {
4345 new_id = cand_id;
4346 break;
4347 }
4348 }
4349 break;
4350
4351 case BTF_KIND_ARRAY: {
4352 struct btf_array *info = btf_array(t);
4353
4354 ref_type_id = btf_dedup_ref_type(d, info->type);
4355 if (ref_type_id < 0)
4356 return ref_type_id;
4357 info->type = ref_type_id;
4358
4359 ref_type_id = btf_dedup_ref_type(d, info->index_type);
4360 if (ref_type_id < 0)
4361 return ref_type_id;
4362 info->index_type = ref_type_id;
4363
4364 h = btf_hash_array(t);
4365 for_each_dedup_cand(d, hash_entry, h) {
4366 cand_id = (__u32)(long)hash_entry->value;
4367 cand = btf_type_by_id(d->btf, cand_id);
4368 if (btf_equal_array(t, cand)) {
4369 new_id = cand_id;
4370 break;
4371 }
4372 }
4373 break;
4374 }
4375
4376 case BTF_KIND_FUNC_PROTO: {
4377 struct btf_param *param;
4378 __u16 vlen;
4379 int i;
4380
4381 ref_type_id = btf_dedup_ref_type(d, t->type);
4382 if (ref_type_id < 0)
4383 return ref_type_id;
4384 t->type = ref_type_id;
4385
4386 vlen = btf_vlen(t);
4387 param = btf_params(t);
4388 for (i = 0; i < vlen; i++) {
4389 ref_type_id = btf_dedup_ref_type(d, param->type);
4390 if (ref_type_id < 0)
4391 return ref_type_id;
4392 param->type = ref_type_id;
4393 param++;
4394 }
4395
4396 h = btf_hash_fnproto(t);
4397 for_each_dedup_cand(d, hash_entry, h) {
4398 cand_id = (__u32)(long)hash_entry->value;
4399 cand = btf_type_by_id(d->btf, cand_id);
4400 if (btf_equal_fnproto(t, cand)) {
4401 new_id = cand_id;
4402 break;
4403 }
4404 }
4405 break;
4406 }
4407
4408 default:
4409 return -EINVAL;
4410 }
4411
4412 d->map[type_id] = new_id;
4413 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4414 return -ENOMEM;
4415
4416 return new_id;
4417}
4418
4419static int btf_dedup_ref_types(struct btf_dedup *d)
4420{
4421 int i, err;
4422
4423 for (i = 0; i < d->btf->nr_types; i++) {
4424 err = btf_dedup_ref_type(d, d->btf->start_id + i);
4425 if (err < 0)
4426 return err;
4427 }
4428
4429 hashmap__free(d->dedup_table);
4430 d->dedup_table = NULL;
4431 return 0;
4432}
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445static int btf_dedup_compact_types(struct btf_dedup *d)
4446{
4447 __u32 *new_offs;
4448 __u32 next_type_id = d->btf->start_id;
4449 const struct btf_type *t;
4450 void *p;
4451 int i, id, len;
4452
4453
4454 d->hypot_map[0] = 0;
4455
4456 for (id = 1; id < d->btf->start_id; id++)
4457 d->hypot_map[id] = id;
4458 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++)
4459 d->hypot_map[id] = BTF_UNPROCESSED_ID;
4460
4461 p = d->btf->types_data;
4462
4463 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) {
4464 if (d->map[id] != id)
4465 continue;
4466
4467 t = btf__type_by_id(d->btf, id);
4468 len = btf_type_size(t);
4469 if (len < 0)
4470 return len;
4471
4472 memmove(p, t, len);
4473 d->hypot_map[id] = next_type_id;
4474 d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data;
4475 p += len;
4476 next_type_id++;
4477 }
4478
4479
4480 d->btf->nr_types = next_type_id - d->btf->start_id;
4481 d->btf->type_offs_cap = d->btf->nr_types;
4482 d->btf->hdr->type_len = p - d->btf->types_data;
4483 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
4484 sizeof(*new_offs));
4485 if (d->btf->type_offs_cap && !new_offs)
4486 return -ENOMEM;
4487 d->btf->type_offs = new_offs;
4488 d->btf->hdr->str_off = d->btf->hdr->type_len;
4489 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
4490 return 0;
4491}
4492
4493
4494
4495
4496
4497
4498
4499static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
4500{
4501 __u32 resolved_type_id, new_type_id;
4502
4503 resolved_type_id = resolve_type_id(d, type_id);
4504 new_type_id = d->hypot_map[resolved_type_id];
4505 if (new_type_id > BTF_MAX_NR_TYPES)
4506 return -EINVAL;
4507 return new_type_id;
4508}
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
4521{
4522 struct btf_type *t = btf_type_by_id(d->btf, type_id);
4523 int i, r;
4524
4525 switch (btf_kind(t)) {
4526 case BTF_KIND_INT:
4527 case BTF_KIND_ENUM:
4528 case BTF_KIND_FLOAT:
4529 break;
4530
4531 case BTF_KIND_FWD:
4532 case BTF_KIND_CONST:
4533 case BTF_KIND_VOLATILE:
4534 case BTF_KIND_RESTRICT:
4535 case BTF_KIND_PTR:
4536 case BTF_KIND_TYPEDEF:
4537 case BTF_KIND_FUNC:
4538 case BTF_KIND_VAR:
4539 r = btf_dedup_remap_type_id(d, t->type);
4540 if (r < 0)
4541 return r;
4542 t->type = r;
4543 break;
4544
4545 case BTF_KIND_ARRAY: {
4546 struct btf_array *arr_info = btf_array(t);
4547
4548 r = btf_dedup_remap_type_id(d, arr_info->type);
4549 if (r < 0)
4550 return r;
4551 arr_info->type = r;
4552 r = btf_dedup_remap_type_id(d, arr_info->index_type);
4553 if (r < 0)
4554 return r;
4555 arr_info->index_type = r;
4556 break;
4557 }
4558
4559 case BTF_KIND_STRUCT:
4560 case BTF_KIND_UNION: {
4561 struct btf_member *member = btf_members(t);
4562 __u16 vlen = btf_vlen(t);
4563
4564 for (i = 0; i < vlen; i++) {
4565 r = btf_dedup_remap_type_id(d, member->type);
4566 if (r < 0)
4567 return r;
4568 member->type = r;
4569 member++;
4570 }
4571 break;
4572 }
4573
4574 case BTF_KIND_FUNC_PROTO: {
4575 struct btf_param *param = btf_params(t);
4576 __u16 vlen = btf_vlen(t);
4577
4578 r = btf_dedup_remap_type_id(d, t->type);
4579 if (r < 0)
4580 return r;
4581 t->type = r;
4582
4583 for (i = 0; i < vlen; i++) {
4584 r = btf_dedup_remap_type_id(d, param->type);
4585 if (r < 0)
4586 return r;
4587 param->type = r;
4588 param++;
4589 }
4590 break;
4591 }
4592
4593 case BTF_KIND_DATASEC: {
4594 struct btf_var_secinfo *var = btf_var_secinfos(t);
4595 __u16 vlen = btf_vlen(t);
4596
4597 for (i = 0; i < vlen; i++) {
4598 r = btf_dedup_remap_type_id(d, var->type);
4599 if (r < 0)
4600 return r;
4601 var->type = r;
4602 var++;
4603 }
4604 break;
4605 }
4606
4607 default:
4608 return -EINVAL;
4609 }
4610
4611 return 0;
4612}
4613
4614static int btf_dedup_remap_types(struct btf_dedup *d)
4615{
4616 int i, r;
4617
4618 for (i = 0; i < d->btf->nr_types; i++) {
4619 r = btf_dedup_remap_type(d, d->btf->start_id + i);
4620 if (r < 0)
4621 return r;
4622 }
4623 return 0;
4624}
4625
4626
4627
4628
4629
4630struct btf *libbpf_find_kernel_btf(void)
4631{
4632 struct {
4633 const char *path_fmt;
4634 bool raw_btf;
4635 } locations[] = {
4636
4637 { "/sys/kernel/btf/vmlinux", true },
4638
4639 { "/boot/vmlinux-%1$s" },
4640 { "/lib/modules/%1$s/vmlinux-%1$s" },
4641 { "/lib/modules/%1$s/build/vmlinux" },
4642 { "/usr/lib/modules/%1$s/kernel/vmlinux" },
4643 { "/usr/lib/debug/boot/vmlinux-%1$s" },
4644 { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
4645 { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
4646 };
4647 char path[PATH_MAX + 1];
4648 struct utsname buf;
4649 struct btf *btf;
4650 int i;
4651
4652 uname(&buf);
4653
4654 for (i = 0; i < ARRAY_SIZE(locations); i++) {
4655 snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
4656
4657 if (access(path, R_OK))
4658 continue;
4659
4660 if (locations[i].raw_btf)
4661 btf = btf__parse_raw(path);
4662 else
4663 btf = btf__parse_elf(path, NULL);
4664
4665 pr_debug("loading kernel BTF '%s': %ld\n",
4666 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
4667 if (IS_ERR(btf))
4668 continue;
4669
4670 return btf;
4671 }
4672
4673 pr_warn("failed to find valid kernel BTF\n");
4674 return ERR_PTR(-ESRCH);
4675}
4676