1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
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
19 #ifdef HAVE_LIBELF
20 #include <gelf.h>
21 #endif
22
23 #include "btf.h"
24 #include "bpf.h"
25 #include "libbpf.h"
26 #include "libbpf_internal.h"
27 #include "hashmap.h"
28 #include "strset.h"
29
30 #define BTF_MAX_NR_TYPES 0x7fffffffU
31 #define BTF_MAX_STR_OFFSET 0x7fffffffU
32
33 static struct btf_type btf_void;
34
35 struct btf {
36 /* raw BTF data in native endianness */
37 void *raw_data;
38 /* raw BTF data in non-native endianness */
39 void *raw_data_swapped;
40 __u32 raw_size;
41 /* whether target endianness differs from the native one */
42 bool swapped_endian;
43
44 /*
45 * When BTF is loaded from an ELF or raw memory it is stored
46 * in a contiguous memory block. The hdr, type_data, and, strs_data
47 * point inside that memory region to their respective parts of BTF
48 * representation:
49 *
50 * +--------------------------------+
51 * | Header | Types | Strings |
52 * +--------------------------------+
53 * ^ ^ ^
54 * | | |
55 * hdr | |
56 * types_data-+ |
57 * strs_data------------+
58 *
59 * If BTF data is later modified, e.g., due to types added or
60 * removed, BTF deduplication performed, etc, this contiguous
61 * representation is broken up into three independently allocated
62 * memory regions to be able to modify them independently.
63 * raw_data is nulled out at that point, but can be later allocated
64 * and cached again if user calls btf__raw_data(), at which point
65 * raw_data will contain a contiguous copy of header, types, and
66 * strings:
67 *
68 * +----------+ +---------+ +-----------+
69 * | Header | | Types | | Strings |
70 * +----------+ +---------+ +-----------+
71 * ^ ^ ^
72 * | | |
73 * hdr | |
74 * types_data----+ |
75 * strset__data(strs_set)-----+
76 *
77 * +----------+---------+-----------+
78 * | Header | Types | Strings |
79 * raw_data----->+----------+---------+-----------+
80 */
81 struct btf_header *hdr;
82
83 void *types_data;
84 size_t types_data_cap; /* used size stored in hdr->type_len */
85
86 /* type ID to `struct btf_type *` lookup index
87 * type_offs[0] corresponds to the first non-VOID type:
88 * - for base BTF it's type [1];
89 * - for split BTF it's the first non-base BTF type.
90 */
91 __u32 *type_offs;
92 size_t type_offs_cap;
93 /* number of types in this BTF instance:
94 * - doesn't include special [0] void type;
95 * - for split BTF counts number of types added on top of base BTF.
96 */
97 __u32 nr_types;
98 /* if not NULL, points to the base BTF on top of which the current
99 * split BTF is based
100 */
101 struct btf *base_btf;
102 /* BTF type ID of the first type in this BTF instance:
103 * - for base BTF it's equal to 1;
104 * - for split BTF it's equal to biggest type ID of base BTF plus 1.
105 */
106 int start_id;
107 /* logical string offset of this BTF instance:
108 * - for base BTF it's equal to 0;
109 * - for split BTF it's equal to total size of base BTF's string section size.
110 */
111 int start_str_off;
112
113 /* only one of strs_data or strs_set can be non-NULL, depending on
114 * whether BTF is in a modifiable state (strs_set is used) or not
115 * (strs_data points inside raw_data)
116 */
117 void *strs_data;
118 /* a set of unique strings */
119 struct strset *strs_set;
120 /* whether strings are already deduplicated */
121 bool strs_deduped;
122
123 /* BTF object FD, if loaded into kernel */
124 int fd;
125
126 /* Pointer size (in bytes) for a target architecture of this BTF */
127 int ptr_sz;
128 };
129
ptr_to_u64(const void * ptr)130 static inline __u64 ptr_to_u64(const void *ptr)
131 {
132 return (__u64) (unsigned long) ptr;
133 }
134
135 /* Ensure given dynamically allocated memory region pointed to by *data* with
136 * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
137 * memory to accommodate *add_cnt* new elements, assuming *cur_cnt* elements
138 * are already used. At most *max_cnt* elements can be ever allocated.
139 * If necessary, memory is reallocated and all existing data is copied over,
140 * new pointer to the memory region is stored at *data, new memory region
141 * capacity (in number of elements) is stored in *cap.
142 * On success, memory pointer to the beginning of unused memory is returned.
143 * On error, NULL is returned.
144 */
libbpf_add_mem(void ** data,size_t * cap_cnt,size_t elem_sz,size_t cur_cnt,size_t max_cnt,size_t add_cnt)145 void *libbpf_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 /* requested more than the set limit */
155 if (cur_cnt + add_cnt > max_cnt)
156 return NULL;
157
158 new_cnt = *cap_cnt;
159 new_cnt += new_cnt / 4; /* expand by 25% */
160 if (new_cnt < 16) /* but at least 16 elements */
161 new_cnt = 16;
162 if (new_cnt > max_cnt) /* but not exceeding a set limit */
163 new_cnt = max_cnt;
164 if (new_cnt < cur_cnt + add_cnt) /* also ensure we have enough memory */
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 /* zero out newly allocated portion of memory */
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 /* Ensure given dynamically allocated memory region has enough allocated space
180 * to accommodate *need_cnt* elements of size *elem_sz* bytes each
181 */
libbpf_ensure_mem(void ** data,size_t * cap_cnt,size_t elem_sz,size_t need_cnt)182 int libbpf_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 = libbpf_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
btf_add_type_offs_mem(struct btf * btf,size_t add_cnt)196 static void *btf_add_type_offs_mem(struct btf *btf, size_t add_cnt)
197 {
198 return libbpf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32),
199 btf->nr_types, BTF_MAX_NR_TYPES, add_cnt);
200 }
201
btf_add_type_idx_entry(struct btf * btf,__u32 type_off)202 static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
203 {
204 __u32 *p;
205
206 p = btf_add_type_offs_mem(btf, 1);
207 if (!p)
208 return -ENOMEM;
209
210 *p = type_off;
211 return 0;
212 }
213
btf_bswap_hdr(struct btf_header * h)214 static void btf_bswap_hdr(struct btf_header *h)
215 {
216 h->magic = bswap_16(h->magic);
217 h->hdr_len = bswap_32(h->hdr_len);
218 h->type_off = bswap_32(h->type_off);
219 h->type_len = bswap_32(h->type_len);
220 h->str_off = bswap_32(h->str_off);
221 h->str_len = bswap_32(h->str_len);
222 }
223
btf_parse_hdr(struct btf * btf)224 static int btf_parse_hdr(struct btf *btf)
225 {
226 struct btf_header *hdr = btf->hdr;
227 __u32 meta_left;
228
229 if (btf->raw_size < sizeof(struct btf_header)) {
230 pr_debug("BTF header not found\n");
231 return -EINVAL;
232 }
233
234 if (hdr->magic == bswap_16(BTF_MAGIC)) {
235 btf->swapped_endian = true;
236 if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
237 pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
238 bswap_32(hdr->hdr_len));
239 return -ENOTSUP;
240 }
241 btf_bswap_hdr(hdr);
242 } else if (hdr->magic != BTF_MAGIC) {
243 pr_debug("Invalid BTF magic: %x\n", hdr->magic);
244 return -EINVAL;
245 }
246
247 if (btf->raw_size < hdr->hdr_len) {
248 pr_debug("BTF header len %u larger than data size %u\n",
249 hdr->hdr_len, btf->raw_size);
250 return -EINVAL;
251 }
252
253 meta_left = btf->raw_size - hdr->hdr_len;
254 if (meta_left < (long long)hdr->str_off + hdr->str_len) {
255 pr_debug("Invalid BTF total size: %u\n", btf->raw_size);
256 return -EINVAL;
257 }
258
259 if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) {
260 pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
261 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
262 return -EINVAL;
263 }
264
265 if (hdr->type_off % 4) {
266 pr_debug("BTF type section is not aligned to 4 bytes\n");
267 return -EINVAL;
268 }
269
270 return 0;
271 }
272
btf_parse_str_sec(struct btf * btf)273 static int btf_parse_str_sec(struct btf *btf)
274 {
275 const struct btf_header *hdr = btf->hdr;
276 const char *start = btf->strs_data;
277 const char *end = start + btf->hdr->str_len;
278
279 if (btf->base_btf && hdr->str_len == 0)
280 return 0;
281 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) {
282 pr_debug("Invalid BTF string section\n");
283 return -EINVAL;
284 }
285 if (!btf->base_btf && start[0]) {
286 pr_debug("Invalid BTF string section\n");
287 return -EINVAL;
288 }
289 return 0;
290 }
291
btf_type_size(const struct btf_type * t)292 static int btf_type_size(const struct btf_type *t)
293 {
294 const int base_size = sizeof(struct btf_type);
295 __u16 vlen = btf_vlen(t);
296
297 switch (btf_kind(t)) {
298 case BTF_KIND_FWD:
299 case BTF_KIND_CONST:
300 case BTF_KIND_VOLATILE:
301 case BTF_KIND_RESTRICT:
302 case BTF_KIND_PTR:
303 case BTF_KIND_TYPEDEF:
304 case BTF_KIND_FUNC:
305 case BTF_KIND_FLOAT:
306 case BTF_KIND_TYPE_TAG:
307 return base_size;
308 case BTF_KIND_INT:
309 return base_size + sizeof(__u32);
310 case BTF_KIND_ENUM:
311 return base_size + vlen * sizeof(struct btf_enum);
312 case BTF_KIND_ENUM64:
313 return base_size + vlen * sizeof(struct btf_enum64);
314 case BTF_KIND_ARRAY:
315 return base_size + sizeof(struct btf_array);
316 case BTF_KIND_STRUCT:
317 case BTF_KIND_UNION:
318 return base_size + vlen * sizeof(struct btf_member);
319 case BTF_KIND_FUNC_PROTO:
320 return base_size + vlen * sizeof(struct btf_param);
321 case BTF_KIND_VAR:
322 return base_size + sizeof(struct btf_var);
323 case BTF_KIND_DATASEC:
324 return base_size + vlen * sizeof(struct btf_var_secinfo);
325 case BTF_KIND_DECL_TAG:
326 return base_size + sizeof(struct btf_decl_tag);
327 default:
328 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
329 return -EINVAL;
330 }
331 }
332
btf_bswap_type_base(struct btf_type * t)333 static void btf_bswap_type_base(struct btf_type *t)
334 {
335 t->name_off = bswap_32(t->name_off);
336 t->info = bswap_32(t->info);
337 t->type = bswap_32(t->type);
338 }
339
btf_bswap_type_rest(struct btf_type * t)340 static int btf_bswap_type_rest(struct btf_type *t)
341 {
342 struct btf_var_secinfo *v;
343 struct btf_enum64 *e64;
344 struct btf_member *m;
345 struct btf_array *a;
346 struct btf_param *p;
347 struct btf_enum *e;
348 __u16 vlen = btf_vlen(t);
349 int i;
350
351 switch (btf_kind(t)) {
352 case BTF_KIND_FWD:
353 case BTF_KIND_CONST:
354 case BTF_KIND_VOLATILE:
355 case BTF_KIND_RESTRICT:
356 case BTF_KIND_PTR:
357 case BTF_KIND_TYPEDEF:
358 case BTF_KIND_FUNC:
359 case BTF_KIND_FLOAT:
360 case BTF_KIND_TYPE_TAG:
361 return 0;
362 case BTF_KIND_INT:
363 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
364 return 0;
365 case BTF_KIND_ENUM:
366 for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
367 e->name_off = bswap_32(e->name_off);
368 e->val = bswap_32(e->val);
369 }
370 return 0;
371 case BTF_KIND_ENUM64:
372 for (i = 0, e64 = btf_enum64(t); i < vlen; i++, e64++) {
373 e64->name_off = bswap_32(e64->name_off);
374 e64->val_lo32 = bswap_32(e64->val_lo32);
375 e64->val_hi32 = bswap_32(e64->val_hi32);
376 }
377 return 0;
378 case BTF_KIND_ARRAY:
379 a = btf_array(t);
380 a->type = bswap_32(a->type);
381 a->index_type = bswap_32(a->index_type);
382 a->nelems = bswap_32(a->nelems);
383 return 0;
384 case BTF_KIND_STRUCT:
385 case BTF_KIND_UNION:
386 for (i = 0, m = btf_members(t); i < vlen; i++, m++) {
387 m->name_off = bswap_32(m->name_off);
388 m->type = bswap_32(m->type);
389 m->offset = bswap_32(m->offset);
390 }
391 return 0;
392 case BTF_KIND_FUNC_PROTO:
393 for (i = 0, p = btf_params(t); i < vlen; i++, p++) {
394 p->name_off = bswap_32(p->name_off);
395 p->type = bswap_32(p->type);
396 }
397 return 0;
398 case BTF_KIND_VAR:
399 btf_var(t)->linkage = bswap_32(btf_var(t)->linkage);
400 return 0;
401 case BTF_KIND_DATASEC:
402 for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) {
403 v->type = bswap_32(v->type);
404 v->offset = bswap_32(v->offset);
405 v->size = bswap_32(v->size);
406 }
407 return 0;
408 case BTF_KIND_DECL_TAG:
409 btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx);
410 return 0;
411 default:
412 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
413 return -EINVAL;
414 }
415 }
416
btf_parse_type_sec(struct btf * btf)417 static int btf_parse_type_sec(struct btf *btf)
418 {
419 struct btf_header *hdr = btf->hdr;
420 void *next_type = btf->types_data;
421 void *end_type = next_type + hdr->type_len;
422 int err, type_size;
423
424 while (next_type + sizeof(struct btf_type) <= end_type) {
425 if (btf->swapped_endian)
426 btf_bswap_type_base(next_type);
427
428 type_size = btf_type_size(next_type);
429 if (type_size < 0)
430 return type_size;
431 if (next_type + type_size > end_type) {
432 pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types);
433 return -EINVAL;
434 }
435
436 if (btf->swapped_endian && btf_bswap_type_rest(next_type))
437 return -EINVAL;
438
439 err = btf_add_type_idx_entry(btf, next_type - btf->types_data);
440 if (err)
441 return err;
442
443 next_type += type_size;
444 btf->nr_types++;
445 }
446
447 if (next_type != end_type) {
448 pr_warn("BTF types data is malformed\n");
449 return -EINVAL;
450 }
451
452 return 0;
453 }
454
btf_validate_str(const struct btf * btf,__u32 str_off,const char * what,__u32 type_id)455 static int btf_validate_str(const struct btf *btf, __u32 str_off, const char *what, __u32 type_id)
456 {
457 const char *s;
458
459 s = btf__str_by_offset(btf, str_off);
460 if (!s) {
461 pr_warn("btf: type [%u]: invalid %s (string offset %u)\n", type_id, what, str_off);
462 return -EINVAL;
463 }
464
465 return 0;
466 }
467
btf_validate_id(const struct btf * btf,__u32 id,__u32 ctx_id)468 static int btf_validate_id(const struct btf *btf, __u32 id, __u32 ctx_id)
469 {
470 const struct btf_type *t;
471
472 t = btf__type_by_id(btf, id);
473 if (!t) {
474 pr_warn("btf: type [%u]: invalid referenced type ID %u\n", ctx_id, id);
475 return -EINVAL;
476 }
477
478 return 0;
479 }
480
btf_validate_type(const struct btf * btf,const struct btf_type * t,__u32 id)481 static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __u32 id)
482 {
483 __u32 kind = btf_kind(t);
484 int err, i, n;
485
486 err = btf_validate_str(btf, t->name_off, "type name", id);
487 if (err)
488 return err;
489
490 switch (kind) {
491 case BTF_KIND_UNKN:
492 case BTF_KIND_INT:
493 case BTF_KIND_FWD:
494 case BTF_KIND_FLOAT:
495 break;
496 case BTF_KIND_PTR:
497 case BTF_KIND_TYPEDEF:
498 case BTF_KIND_VOLATILE:
499 case BTF_KIND_CONST:
500 case BTF_KIND_RESTRICT:
501 case BTF_KIND_VAR:
502 case BTF_KIND_DECL_TAG:
503 case BTF_KIND_TYPE_TAG:
504 err = btf_validate_id(btf, t->type, id);
505 if (err)
506 return err;
507 break;
508 case BTF_KIND_ARRAY: {
509 const struct btf_array *a = btf_array(t);
510
511 err = btf_validate_id(btf, a->type, id);
512 err = err ?: btf_validate_id(btf, a->index_type, id);
513 if (err)
514 return err;
515 break;
516 }
517 case BTF_KIND_STRUCT:
518 case BTF_KIND_UNION: {
519 const struct btf_member *m = btf_members(t);
520
521 n = btf_vlen(t);
522 for (i = 0; i < n; i++, m++) {
523 err = btf_validate_str(btf, m->name_off, "field name", id);
524 err = err ?: btf_validate_id(btf, m->type, id);
525 if (err)
526 return err;
527 }
528 break;
529 }
530 case BTF_KIND_ENUM: {
531 const struct btf_enum *m = btf_enum(t);
532
533 n = btf_vlen(t);
534 for (i = 0; i < n; i++, m++) {
535 err = btf_validate_str(btf, m->name_off, "enum name", id);
536 if (err)
537 return err;
538 }
539 break;
540 }
541 case BTF_KIND_ENUM64: {
542 const struct btf_enum64 *m = btf_enum64(t);
543
544 n = btf_vlen(t);
545 for (i = 0; i < n; i++, m++) {
546 err = btf_validate_str(btf, m->name_off, "enum name", id);
547 if (err)
548 return err;
549 }
550 break;
551 }
552 case BTF_KIND_FUNC: {
553 const struct btf_type *ft;
554
555 err = btf_validate_id(btf, t->type, id);
556 if (err)
557 return err;
558 ft = btf__type_by_id(btf, t->type);
559 if (btf_kind(ft) != BTF_KIND_FUNC_PROTO) {
560 pr_warn("btf: type [%u]: referenced type [%u] is not FUNC_PROTO\n", id, t->type);
561 return -EINVAL;
562 }
563 break;
564 }
565 case BTF_KIND_FUNC_PROTO: {
566 const struct btf_param *m = btf_params(t);
567
568 n = btf_vlen(t);
569 for (i = 0; i < n; i++, m++) {
570 err = btf_validate_str(btf, m->name_off, "param name", id);
571 err = err ?: btf_validate_id(btf, m->type, id);
572 if (err)
573 return err;
574 }
575 break;
576 }
577 case BTF_KIND_DATASEC: {
578 const struct btf_var_secinfo *m = btf_var_secinfos(t);
579
580 n = btf_vlen(t);
581 for (i = 0; i < n; i++, m++) {
582 err = btf_validate_id(btf, m->type, id);
583 if (err)
584 return err;
585 }
586 break;
587 }
588 default:
589 pr_warn("btf: type [%u]: unrecognized kind %u\n", id, kind);
590 return -EINVAL;
591 }
592 return 0;
593 }
594
595 /* Validate basic sanity of BTF. It's intentionally less thorough than
596 * kernel's validation and validates only properties of BTF that libbpf relies
597 * on to be correct (e.g., valid type IDs, valid string offsets, etc)
598 */
btf_sanity_check(const struct btf * btf)599 static int btf_sanity_check(const struct btf *btf)
600 {
601 const struct btf_type *t;
602 __u32 i, n = btf__type_cnt(btf);
603 int err;
604
605 for (i = 1; i < n; i++) {
606 t = btf_type_by_id(btf, i);
607 err = btf_validate_type(btf, t, i);
608 if (err)
609 return err;
610 }
611 return 0;
612 }
613
btf__type_cnt(const struct btf * btf)614 __u32 btf__type_cnt(const struct btf *btf)
615 {
616 return btf->start_id + btf->nr_types;
617 }
618
btf__base_btf(const struct btf * btf)619 const struct btf *btf__base_btf(const struct btf *btf)
620 {
621 return btf->base_btf;
622 }
623
624 /* internal helper returning non-const pointer to a type */
btf_type_by_id(const struct btf * btf,__u32 type_id)625 struct btf_type *btf_type_by_id(const struct btf *btf, __u32 type_id)
626 {
627 if (type_id == 0)
628 return &btf_void;
629 if (type_id < btf->start_id)
630 return btf_type_by_id(btf->base_btf, type_id);
631 return btf->types_data + btf->type_offs[type_id - btf->start_id];
632 }
633
btf__type_by_id(const struct btf * btf,__u32 type_id)634 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
635 {
636 if (type_id >= btf->start_id + btf->nr_types)
637 return errno = EINVAL, NULL;
638 return btf_type_by_id((struct btf *)btf, type_id);
639 }
640
determine_ptr_size(const struct btf * btf)641 static int determine_ptr_size(const struct btf *btf)
642 {
643 static const char * const long_aliases[] = {
644 "long",
645 "long int",
646 "int long",
647 "unsigned long",
648 "long unsigned",
649 "unsigned long int",
650 "unsigned int long",
651 "long unsigned int",
652 "long int unsigned",
653 "int unsigned long",
654 "int long unsigned",
655 };
656 const struct btf_type *t;
657 const char *name;
658 int i, j, n;
659
660 if (btf->base_btf && btf->base_btf->ptr_sz > 0)
661 return btf->base_btf->ptr_sz;
662
663 n = btf__type_cnt(btf);
664 for (i = 1; i < n; i++) {
665 t = btf__type_by_id(btf, i);
666 if (!btf_is_int(t))
667 continue;
668
669 if (t->size != 4 && t->size != 8)
670 continue;
671
672 name = btf__name_by_offset(btf, t->name_off);
673 if (!name)
674 continue;
675
676 for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
677 if (strcmp(name, long_aliases[j]) == 0)
678 return t->size;
679 }
680 }
681
682 return -1;
683 }
684
btf_ptr_sz(const struct btf * btf)685 static size_t btf_ptr_sz(const struct btf *btf)
686 {
687 if (!btf->ptr_sz)
688 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
689 return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz;
690 }
691
692 /* Return pointer size this BTF instance assumes. The size is heuristically
693 * determined by looking for 'long' or 'unsigned long' integer type and
694 * recording its size in bytes. If BTF type information doesn't have any such
695 * type, this function returns 0. In the latter case, native architecture's
696 * pointer size is assumed, so will be either 4 or 8, depending on
697 * architecture that libbpf was compiled for. It's possible to override
698 * guessed value by using btf__set_pointer_size() API.
699 */
btf__pointer_size(const struct btf * btf)700 size_t btf__pointer_size(const struct btf *btf)
701 {
702 if (!btf->ptr_sz)
703 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
704
705 if (btf->ptr_sz < 0)
706 /* not enough BTF type info to guess */
707 return 0;
708
709 return btf->ptr_sz;
710 }
711
712 /* Override or set pointer size in bytes. Only values of 4 and 8 are
713 * supported.
714 */
btf__set_pointer_size(struct btf * btf,size_t ptr_sz)715 int btf__set_pointer_size(struct btf *btf, size_t ptr_sz)
716 {
717 if (ptr_sz != 4 && ptr_sz != 8)
718 return libbpf_err(-EINVAL);
719 btf->ptr_sz = ptr_sz;
720 return 0;
721 }
722
is_host_big_endian(void)723 static bool is_host_big_endian(void)
724 {
725 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
726 return false;
727 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
728 return true;
729 #else
730 # error "Unrecognized __BYTE_ORDER__"
731 #endif
732 }
733
btf__endianness(const struct btf * btf)734 enum btf_endianness btf__endianness(const struct btf *btf)
735 {
736 if (is_host_big_endian())
737 return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN;
738 else
739 return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
740 }
741
btf__set_endianness(struct btf * btf,enum btf_endianness endian)742 int btf__set_endianness(struct btf *btf, enum btf_endianness endian)
743 {
744 if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN)
745 return libbpf_err(-EINVAL);
746
747 btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN);
748 if (!btf->swapped_endian) {
749 free(btf->raw_data_swapped);
750 btf->raw_data_swapped = NULL;
751 }
752 return 0;
753 }
754
btf_type_is_void(const struct btf_type * t)755 static bool btf_type_is_void(const struct btf_type *t)
756 {
757 return t == &btf_void || btf_is_fwd(t);
758 }
759
btf_type_is_void_or_null(const struct btf_type * t)760 static bool btf_type_is_void_or_null(const struct btf_type *t)
761 {
762 return !t || btf_type_is_void(t);
763 }
764
765 #define MAX_RESOLVE_DEPTH 32
766
btf__resolve_size(const struct btf * btf,__u32 type_id)767 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
768 {
769 const struct btf_array *array;
770 const struct btf_type *t;
771 __u32 nelems = 1;
772 __s64 size = -1;
773 int i;
774
775 t = btf__type_by_id(btf, type_id);
776 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); i++) {
777 switch (btf_kind(t)) {
778 case BTF_KIND_INT:
779 case BTF_KIND_STRUCT:
780 case BTF_KIND_UNION:
781 case BTF_KIND_ENUM:
782 case BTF_KIND_ENUM64:
783 case BTF_KIND_DATASEC:
784 case BTF_KIND_FLOAT:
785 size = t->size;
786 goto done;
787 case BTF_KIND_PTR:
788 size = btf_ptr_sz(btf);
789 goto done;
790 case BTF_KIND_TYPEDEF:
791 case BTF_KIND_VOLATILE:
792 case BTF_KIND_CONST:
793 case BTF_KIND_RESTRICT:
794 case BTF_KIND_VAR:
795 case BTF_KIND_DECL_TAG:
796 case BTF_KIND_TYPE_TAG:
797 type_id = t->type;
798 break;
799 case BTF_KIND_ARRAY:
800 array = btf_array(t);
801 if (nelems && array->nelems > UINT32_MAX / nelems)
802 return libbpf_err(-E2BIG);
803 nelems *= array->nelems;
804 type_id = array->type;
805 break;
806 default:
807 return libbpf_err(-EINVAL);
808 }
809
810 t = btf__type_by_id(btf, type_id);
811 }
812
813 done:
814 if (size < 0)
815 return libbpf_err(-EINVAL);
816 if (nelems && size > UINT32_MAX / nelems)
817 return libbpf_err(-E2BIG);
818
819 return nelems * size;
820 }
821
btf__align_of(const struct btf * btf,__u32 id)822 int btf__align_of(const struct btf *btf, __u32 id)
823 {
824 const struct btf_type *t = btf__type_by_id(btf, id);
825 __u16 kind = btf_kind(t);
826
827 switch (kind) {
828 case BTF_KIND_INT:
829 case BTF_KIND_ENUM:
830 case BTF_KIND_ENUM64:
831 case BTF_KIND_FLOAT:
832 return min(btf_ptr_sz(btf), (size_t)t->size);
833 case BTF_KIND_PTR:
834 return btf_ptr_sz(btf);
835 case BTF_KIND_TYPEDEF:
836 case BTF_KIND_VOLATILE:
837 case BTF_KIND_CONST:
838 case BTF_KIND_RESTRICT:
839 case BTF_KIND_TYPE_TAG:
840 return btf__align_of(btf, t->type);
841 case BTF_KIND_ARRAY:
842 return btf__align_of(btf, btf_array(t)->type);
843 case BTF_KIND_STRUCT:
844 case BTF_KIND_UNION: {
845 const struct btf_member *m = btf_members(t);
846 __u16 vlen = btf_vlen(t);
847 int i, max_align = 1, align;
848
849 for (i = 0; i < vlen; i++, m++) {
850 align = btf__align_of(btf, m->type);
851 if (align <= 0)
852 return libbpf_err(align);
853 max_align = max(max_align, align);
854
855 /* if field offset isn't aligned according to field
856 * type's alignment, then struct must be packed
857 */
858 if (btf_member_bitfield_size(t, i) == 0 &&
859 (m->offset % (8 * align)) != 0)
860 return 1;
861 }
862
863 /* if struct/union size isn't a multiple of its alignment,
864 * then struct must be packed
865 */
866 if ((t->size % max_align) != 0)
867 return 1;
868
869 return max_align;
870 }
871 default:
872 pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
873 return errno = EINVAL, 0;
874 }
875 }
876
btf__resolve_type(const struct btf * btf,__u32 type_id)877 int btf__resolve_type(const struct btf *btf, __u32 type_id)
878 {
879 const struct btf_type *t;
880 int depth = 0;
881
882 t = btf__type_by_id(btf, type_id);
883 while (depth < MAX_RESOLVE_DEPTH &&
884 !btf_type_is_void_or_null(t) &&
885 (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
886 type_id = t->type;
887 t = btf__type_by_id(btf, type_id);
888 depth++;
889 }
890
891 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
892 return libbpf_err(-EINVAL);
893
894 return type_id;
895 }
896
btf__find_by_name(const struct btf * btf,const char * type_name)897 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
898 {
899 __u32 i, nr_types = btf__type_cnt(btf);
900
901 if (!strcmp(type_name, "void"))
902 return 0;
903
904 for (i = 1; i < nr_types; i++) {
905 const struct btf_type *t = btf__type_by_id(btf, i);
906 const char *name = btf__name_by_offset(btf, t->name_off);
907
908 if (name && !strcmp(type_name, name))
909 return i;
910 }
911
912 return libbpf_err(-ENOENT);
913 }
914
btf_find_by_name_kind(const struct btf * btf,int start_id,const char * type_name,__u32 kind)915 static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,
916 const char *type_name, __u32 kind)
917 {
918 __u32 i, nr_types = btf__type_cnt(btf);
919
920 if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
921 return 0;
922
923 for (i = start_id; i < nr_types; i++) {
924 const struct btf_type *t = btf__type_by_id(btf, i);
925 const char *name;
926
927 if (btf_kind(t) != kind)
928 continue;
929 name = btf__name_by_offset(btf, t->name_off);
930 if (name && !strcmp(type_name, name))
931 return i;
932 }
933
934 return libbpf_err(-ENOENT);
935 }
936
btf__find_by_name_kind_own(const struct btf * btf,const char * type_name,__u32 kind)937 __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name,
938 __u32 kind)
939 {
940 return btf_find_by_name_kind(btf, btf->start_id, type_name, kind);
941 }
942
btf__find_by_name_kind(const struct btf * btf,const char * type_name,__u32 kind)943 __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
944 __u32 kind)
945 {
946 return btf_find_by_name_kind(btf, 1, type_name, kind);
947 }
948
btf_is_modifiable(const struct btf * btf)949 static bool btf_is_modifiable(const struct btf *btf)
950 {
951 return (void *)btf->hdr != btf->raw_data;
952 }
953
btf__free(struct btf * btf)954 void btf__free(struct btf *btf)
955 {
956 if (IS_ERR_OR_NULL(btf))
957 return;
958
959 if (btf->fd >= 0)
960 close(btf->fd);
961
962 if (btf_is_modifiable(btf)) {
963 /* if BTF was modified after loading, it will have a split
964 * in-memory representation for header, types, and strings
965 * sections, so we need to free all of them individually. It
966 * might still have a cached contiguous raw data present,
967 * which will be unconditionally freed below.
968 */
969 free(btf->hdr);
970 free(btf->types_data);
971 strset__free(btf->strs_set);
972 }
973 free(btf->raw_data);
974 free(btf->raw_data_swapped);
975 free(btf->type_offs);
976 free(btf);
977 }
978
btf_new_empty(struct btf * base_btf)979 static struct btf *btf_new_empty(struct btf *base_btf)
980 {
981 struct btf *btf;
982
983 btf = calloc(1, sizeof(*btf));
984 if (!btf)
985 return ERR_PTR(-ENOMEM);
986
987 btf->nr_types = 0;
988 btf->start_id = 1;
989 btf->start_str_off = 0;
990 btf->fd = -1;
991 btf->ptr_sz = sizeof(void *);
992 btf->swapped_endian = false;
993
994 if (base_btf) {
995 btf->base_btf = base_btf;
996 btf->start_id = btf__type_cnt(base_btf);
997 btf->start_str_off = base_btf->hdr->str_len;
998 btf->swapped_endian = base_btf->swapped_endian;
999 }
1000
1001 /* +1 for empty string at offset 0 */
1002 btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1);
1003 btf->raw_data = calloc(1, btf->raw_size);
1004 if (!btf->raw_data) {
1005 free(btf);
1006 return ERR_PTR(-ENOMEM);
1007 }
1008
1009 btf->hdr = btf->raw_data;
1010 btf->hdr->hdr_len = sizeof(struct btf_header);
1011 btf->hdr->magic = BTF_MAGIC;
1012 btf->hdr->version = BTF_VERSION;
1013
1014 btf->types_data = btf->raw_data + btf->hdr->hdr_len;
1015 btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
1016 btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */
1017
1018 return btf;
1019 }
1020
btf__new_empty(void)1021 struct btf *btf__new_empty(void)
1022 {
1023 return libbpf_ptr(btf_new_empty(NULL));
1024 }
1025
btf__new_empty_split(struct btf * base_btf)1026 struct btf *btf__new_empty_split(struct btf *base_btf)
1027 {
1028 return libbpf_ptr(btf_new_empty(base_btf));
1029 }
1030
btf_new(const void * data,__u32 size,struct btf * base_btf)1031 static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
1032 {
1033 struct btf *btf;
1034 int err;
1035
1036 btf = calloc(1, sizeof(struct btf));
1037 if (!btf)
1038 return ERR_PTR(-ENOMEM);
1039
1040 btf->nr_types = 0;
1041 btf->start_id = 1;
1042 btf->start_str_off = 0;
1043 btf->fd = -1;
1044
1045 if (base_btf) {
1046 btf->base_btf = base_btf;
1047 btf->start_id = btf__type_cnt(base_btf);
1048 btf->start_str_off = base_btf->hdr->str_len;
1049 }
1050
1051 btf->raw_data = malloc(size);
1052 if (!btf->raw_data) {
1053 err = -ENOMEM;
1054 goto done;
1055 }
1056 memcpy(btf->raw_data, data, size);
1057 btf->raw_size = size;
1058
1059 btf->hdr = btf->raw_data;
1060 err = btf_parse_hdr(btf);
1061 if (err)
1062 goto done;
1063
1064 btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
1065 btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
1066
1067 err = btf_parse_str_sec(btf);
1068 err = err ?: btf_parse_type_sec(btf);
1069 err = err ?: btf_sanity_check(btf);
1070 if (err)
1071 goto done;
1072
1073 done:
1074 if (err) {
1075 btf__free(btf);
1076 return ERR_PTR(err);
1077 }
1078
1079 return btf;
1080 }
1081
btf__new(const void * data,__u32 size)1082 struct btf *btf__new(const void *data, __u32 size)
1083 {
1084 return libbpf_ptr(btf_new(data, size, NULL));
1085 }
1086
1087 #ifdef HAVE_ELFIO
elf_sec_hdr_by_idx(pelfio_t elf,size_t idx,Elf64_Shdr * sheader)1088 static Elf64_Shdr *elf_sec_hdr_by_idx(pelfio_t elf, size_t idx, Elf64_Shdr *sheader)
1089 {
1090 psection_t psection = elfio_get_section_by_index(elf, idx);
1091
1092 sheader->sh_name = elfio_section_get_name_string_offset(psection);
1093 sheader->sh_type = elfio_section_get_type(psection);
1094 sheader->sh_flags = elfio_section_get_flags(psection);
1095 sheader->sh_addr = elfio_section_get_address(psection);
1096 sheader->sh_offset = elfio_section_get_offset(psection);
1097 sheader->sh_size = elfio_section_get_size(psection);
1098 sheader->sh_link = elfio_section_get_link(psection);
1099 sheader->sh_info = elfio_section_get_info(psection);
1100 sheader->sh_addralign = elfio_section_get_addr_align(psection);
1101 sheader->sh_entsize = elfio_section_get_entry_size(psection);
1102
1103 return sheader;
1104 }
1105 #endif
1106
btf_parse_elf(const char * path,struct btf * base_btf,struct btf_ext ** btf_ext)1107 static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
1108 struct btf_ext **btf_ext)
1109 {
1110 Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
1111 int err = 0, fd = -1, idx = 0;
1112 struct btf *btf = NULL;
1113 #ifdef HAVE_LIBELF
1114 Elf_Scn *scn = NULL;
1115 Elf *elf = NULL;
1116 GElf_Ehdr ehdr;
1117 #elif defined HAVE_ELFIO
1118 pelfio_t elf;
1119 Elf64_Ehdr ehdr;
1120 Elf_Data btf_data_temp, btf_ext_data_temp;
1121 #endif
1122 size_t shstrndx;
1123
1124 #ifdef HAVE_LIBELF
1125 if (elf_version(EV_CURRENT) == EV_NONE) {
1126 pr_warn("failed to init libelf for %s\n", path);
1127 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1128 }
1129 #endif
1130
1131 fd = open(path, O_RDONLY | O_CLOEXEC);
1132 if (fd < 0) {
1133 err = -errno;
1134 pr_warn("failed to open %s: %s\n", path, strerror(errno));
1135 return ERR_PTR(err);
1136 }
1137
1138 err = -LIBBPF_ERRNO__FORMAT;
1139
1140 #ifdef HAVE_LIBELF
1141 elf = elf_begin(fd, ELF_C_READ, NULL);
1142 #elif defined HAVE_ELFIO
1143 elf = elfio_new();
1144 if (!elfio_load(elf, path)) {
1145 printf( "Can't load ELF file\n" );
1146 goto done;
1147 }
1148 #endif
1149 if (!elf) {
1150 pr_warn("failed to open %s as ELF file\n", path);
1151 goto done;
1152 }
1153
1154 #ifdef HAVE_LIBELF
1155 if (!gelf_getehdr(elf, &ehdr)) {
1156 #elif defined HAVE_ELFIO
1157 ssize_t nread = read(fd, &ehdr, sizeof(Elf64_Ehdr));
1158 if(nread != sizeof(Elf64_Ehdr)) {
1159 #endif
1160 pr_warn("failed to get EHDR from %s\n", path);
1161 goto done;
1162 }
1163
1164 #ifdef HAVE_LIBELF
1165 if (elf_getshdrstrndx(elf, &shstrndx)) {
1166 #elif defined HAVE_ELFIO
1167 shstrndx = elfio_get_section_name_str_index(elf);
1168 if(shstrndx == 0) {
1169 #endif
1170 pr_warn("failed to get section names section index for %s\n",
1171 path);
1172 goto done;
1173 }
1174
1175 #ifdef HAVE_LIBELF
1176 if (!elf_rawdata(elf_getscn(elf, shstrndx), NULL)) {
1177 pr_warn("failed to get e_shstrndx from %s\n", path);
1178 goto done;
1179 }
1180 #endif
1181
1182 #if defined HAVE_LIBELF
1183 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1184 GElf_Shdr sh;
1185 #elif defined HAVE_ELFIO
1186 psection_t psection = elfio_get_section_by_name(elf, ".shstrtab");
1187 if (!psection )
1188 goto done;
1189
1190 pstring_t pstring = elfio_string_section_accessor_new(psection);
1191 if (!pstring )
1192 goto done;
1193
1194 int secno = elfio_get_sections_num(elf);
1195
1196 for ( int i = 0; i < secno; i++ ) {
1197 Elf64_Shdr sh;
1198 #endif
1199 char *name;
1200
1201 idx++;
1202 #if defined HAVE_LIBELF
1203 if (gelf_getshdr(scn, &sh) != &sh) {
1204 #elif defined HAVE_ELFIO
1205 if (!elf_sec_hdr_by_idx(elf, i, &sh)) {
1206 #endif
1207 pr_warn("failed to get section(%d) header from %s\n",
1208 idx, path);
1209 goto done;
1210 }
1211 #if defined HAVE_LIBELF
1212 name = elf_strptr(elf, shstrndx, sh.sh_name);
1213 #elif defined HAVE_ELFIO
1214 name = elfio_string_get_string(pstring, sh.sh_name);
1215 #endif
1216 if (!name) {
1217 pr_warn("failed to get section(%d) name from %s\n",
1218 idx, path);
1219 goto done;
1220 }
1221 if (strcmp(name, BTF_ELF_SEC) == 0) {
1222 #if defined HAVE_LIBELF
1223 btf_data = elf_getdata(scn, 0);
1224 #elif defined HAVE_ELFIO
1225 psection_t psection_index = elfio_get_section_by_index(elf, i);
1226 btf_data_temp.d_buf = (void*)elfio_section_get_data(psection_index);
1227 btf_data_temp.d_size = elfio_section_get_size(psection_index);
1228 btf_data = &btf_data_temp;
1229 #endif
1230 if (!btf_data) {
1231 pr_warn("failed to get section(%d, %s) data from %s\n",
1232 idx, name, path);
1233 goto done;
1234 }
1235 continue;
1236 } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
1237 #if defined HAVE_LIBELF
1238 btf_ext_data = elf_getdata(scn, 0);
1239 #elif defined HAVE_ELFIO
1240 psection_t psection_index = elfio_get_section_by_index(elf, i);
1241 btf_ext_data_temp.d_buf = (void*)elfio_section_get_data(psection_index);
1242 btf_ext_data_temp.d_size = elfio_section_get_size(psection_index);
1243 btf_ext_data = &btf_ext_data_temp;
1244 #endif
1245 if (!btf_ext_data) {
1246 pr_warn("failed to get section(%d, %s) data from %s\n",
1247 idx, name, path);
1248 goto done;
1249 }
1250 continue;
1251 }
1252 }
1253
1254 if (!btf_data) {
1255 pr_warn("failed to find '%s' ELF section in %s\n", BTF_ELF_SEC, path);
1256 err = -ENODATA;
1257 goto done;
1258 }
1259 btf = btf_new(btf_data->d_buf, btf_data->d_size, base_btf);
1260 err = libbpf_get_error(btf);
1261 if (err)
1262 goto done;
1263 #ifdef HAVE_LIBELF
1264 switch (gelf_getclass(elf)) {
1265 #elif defined HAVE_ELFIO
1266 switch (elfio_get_class(elf)) {
1267 #endif
1268
1269 case ELFCLASS32:
1270 btf__set_pointer_size(btf, 4);
1271 break;
1272 case ELFCLASS64:
1273 btf__set_pointer_size(btf, 8);
1274 break;
1275 default:
1276 pr_warn("failed to get ELF class (bitness) for %s\n", path);
1277 break;
1278 }
1279
1280 if (btf_ext && btf_ext_data) {
1281 *btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
1282 err = libbpf_get_error(*btf_ext);
1283 if (err)
1284 goto done;
1285 } else if (btf_ext) {
1286 *btf_ext = NULL;
1287 }
1288 done:
1289 if (elf)
1290 #ifdef HAVE_LIBELF
1291 elf_end(elf);
1292 #elif defined HAVE_ELFIO
1293 elfio_delete(elf);
1294 if(pstring)
1295 elfio_string_section_accessor_delete(pstring);
1296 #endif
1297 close(fd);
1298
1299 if (!err)
1300 return btf;
1301
1302 if (btf_ext)
1303 btf_ext__free(*btf_ext);
1304 btf__free(btf);
1305
1306 return ERR_PTR(err);
1307 }
1308
1309 struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
1310 {
1311 return libbpf_ptr(btf_parse_elf(path, NULL, btf_ext));
1312 }
1313
1314 struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf)
1315 {
1316 return libbpf_ptr(btf_parse_elf(path, base_btf, NULL));
1317 }
1318
1319 static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)
1320 {
1321 struct btf *btf = NULL;
1322 void *data = NULL;
1323 FILE *f = NULL;
1324 __u16 magic;
1325 int err = 0;
1326 long sz;
1327
1328 f = fopen(path, "rbe");
1329 if (!f) {
1330 err = -errno;
1331 goto err_out;
1332 }
1333
1334 /* check BTF magic */
1335 if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
1336 err = -EIO;
1337 goto err_out;
1338 }
1339 if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) {
1340 /* definitely not a raw BTF */
1341 err = -EPROTO;
1342 goto err_out;
1343 }
1344
1345 /* get file size */
1346 if (fseek(f, 0, SEEK_END)) {
1347 err = -errno;
1348 goto err_out;
1349 }
1350 sz = ftell(f);
1351 if (sz < 0) {
1352 err = -errno;
1353 goto err_out;
1354 }
1355 /* rewind to the start */
1356 if (fseek(f, 0, SEEK_SET)) {
1357 err = -errno;
1358 goto err_out;
1359 }
1360
1361 /* pre-alloc memory and read all of BTF data */
1362 data = malloc(sz);
1363 if (!data) {
1364 err = -ENOMEM;
1365 goto err_out;
1366 }
1367 if (fread(data, 1, sz, f) < sz) {
1368 err = -EIO;
1369 goto err_out;
1370 }
1371
1372 /* finally parse BTF data */
1373 btf = btf_new(data, sz, base_btf);
1374
1375 err_out:
1376 free(data);
1377 if (f)
1378 fclose(f);
1379 return err ? ERR_PTR(err) : btf;
1380 }
1381
1382 struct btf *btf__parse_raw(const char *path)
1383 {
1384 return libbpf_ptr(btf_parse_raw(path, NULL));
1385 }
1386
1387 struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf)
1388 {
1389 return libbpf_ptr(btf_parse_raw(path, base_btf));
1390 }
1391
1392 static struct btf *btf_parse(const char *path, struct btf *base_btf, struct btf_ext **btf_ext)
1393 {
1394 struct btf *btf;
1395 int err;
1396
1397 if (btf_ext)
1398 *btf_ext = NULL;
1399
1400 btf = btf_parse_raw(path, base_btf);
1401 err = libbpf_get_error(btf);
1402 if (!err)
1403 return btf;
1404 if (err != -EPROTO)
1405 return ERR_PTR(err);
1406 return btf_parse_elf(path, base_btf, btf_ext);
1407 }
1408
1409 struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
1410 {
1411 return libbpf_ptr(btf_parse(path, NULL, btf_ext));
1412 }
1413
1414 struct btf *btf__parse_split(const char *path, struct btf *base_btf)
1415 {
1416 return libbpf_ptr(btf_parse(path, base_btf, NULL));
1417 }
1418
1419 static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
1420
1421 int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 log_level)
1422 {
1423 LIBBPF_OPTS(bpf_btf_load_opts, opts);
1424 __u32 buf_sz = 0, raw_size;
1425 char *buf = NULL, *tmp;
1426 void *raw_data;
1427 int err = 0;
1428
1429 if (btf->fd >= 0)
1430 return libbpf_err(-EEXIST);
1431 if (log_sz && !log_buf)
1432 return libbpf_err(-EINVAL);
1433
1434 /* cache native raw data representation */
1435 raw_data = btf_get_raw_data(btf, &raw_size, false);
1436 if (!raw_data) {
1437 err = -ENOMEM;
1438 goto done;
1439 }
1440 btf->raw_size = raw_size;
1441 btf->raw_data = raw_data;
1442
1443 retry_load:
1444 /* if log_level is 0, we won't provide log_buf/log_size to the kernel,
1445 * initially. Only if BTF loading fails, we bump log_level to 1 and
1446 * retry, using either auto-allocated or custom log_buf. This way
1447 * non-NULL custom log_buf provides a buffer just in case, but hopes
1448 * for successful load and no need for log_buf.
1449 */
1450 if (log_level) {
1451 /* if caller didn't provide custom log_buf, we'll keep
1452 * allocating our own progressively bigger buffers for BTF
1453 * verification log
1454 */
1455 if (!log_buf) {
1456 buf_sz = max((__u32)BPF_LOG_BUF_SIZE, buf_sz * 2);
1457 tmp = realloc(buf, buf_sz);
1458 if (!tmp) {
1459 err = -ENOMEM;
1460 goto done;
1461 }
1462 buf = tmp;
1463 buf[0] = '\0';
1464 }
1465
1466 opts.log_buf = log_buf ? log_buf : buf;
1467 opts.log_size = log_buf ? log_sz : buf_sz;
1468 opts.log_level = log_level;
1469 }
1470
1471 btf->fd = bpf_btf_load(raw_data, raw_size, &opts);
1472 if (btf->fd < 0) {
1473 /* time to turn on verbose mode and try again */
1474 if (log_level == 0) {
1475 log_level = 1;
1476 goto retry_load;
1477 }
1478 /* only retry if caller didn't provide custom log_buf, but
1479 * make sure we can never overflow buf_sz
1480 */
1481 if (!log_buf && errno == ENOSPC && buf_sz <= UINT_MAX / 2)
1482 goto retry_load;
1483
1484 err = -errno;
1485 pr_warn("BTF loading error: %d\n", err);
1486 /* don't print out contents of custom log_buf */
1487 if (!log_buf && buf[0])
1488 pr_warn("-- BEGIN BTF LOAD LOG ---\n%s\n-- END BTF LOAD LOG --\n", buf);
1489 }
1490
1491 done:
1492 free(buf);
1493 return libbpf_err(err);
1494 }
1495
1496 int btf__load_into_kernel(struct btf *btf)
1497 {
1498 return btf_load_into_kernel(btf, NULL, 0, 0);
1499 }
1500
1501 int btf__fd(const struct btf *btf)
1502 {
1503 return btf->fd;
1504 }
1505
1506 void btf__set_fd(struct btf *btf, int fd)
1507 {
1508 btf->fd = fd;
1509 }
1510
1511 static const void *btf_strs_data(const struct btf *btf)
1512 {
1513 return btf->strs_data ? btf->strs_data : strset__data(btf->strs_set);
1514 }
1515
1516 static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
1517 {
1518 struct btf_header *hdr = btf->hdr;
1519 struct btf_type *t;
1520 void *data, *p;
1521 __u32 data_sz;
1522 int i;
1523
1524 data = swap_endian ? btf->raw_data_swapped : btf->raw_data;
1525 if (data) {
1526 *size = btf->raw_size;
1527 return data;
1528 }
1529
1530 data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
1531 data = calloc(1, data_sz);
1532 if (!data)
1533 return NULL;
1534 p = data;
1535
1536 memcpy(p, hdr, hdr->hdr_len);
1537 if (swap_endian)
1538 btf_bswap_hdr(p);
1539 p += hdr->hdr_len;
1540
1541 memcpy(p, btf->types_data, hdr->type_len);
1542 if (swap_endian) {
1543 for (i = 0; i < btf->nr_types; i++) {
1544 t = p + btf->type_offs[i];
1545 /* btf_bswap_type_rest() relies on native t->info, so
1546 * we swap base type info after we swapped all the
1547 * additional information
1548 */
1549 if (btf_bswap_type_rest(t))
1550 goto err_out;
1551 btf_bswap_type_base(t);
1552 }
1553 }
1554 p += hdr->type_len;
1555
1556 memcpy(p, btf_strs_data(btf), hdr->str_len);
1557 p += hdr->str_len;
1558
1559 *size = data_sz;
1560 return data;
1561 err_out:
1562 free(data);
1563 return NULL;
1564 }
1565
1566 const void *btf__raw_data(const struct btf *btf_ro, __u32 *size)
1567 {
1568 struct btf *btf = (struct btf *)btf_ro;
1569 __u32 data_sz;
1570 void *data;
1571
1572 data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
1573 if (!data)
1574 return errno = ENOMEM, NULL;
1575
1576 btf->raw_size = data_sz;
1577 if (btf->swapped_endian)
1578 btf->raw_data_swapped = data;
1579 else
1580 btf->raw_data = data;
1581 *size = data_sz;
1582 return data;
1583 }
1584
1585 __attribute__((alias("btf__raw_data")))
1586 const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
1587
1588 const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
1589 {
1590 if (offset < btf->start_str_off)
1591 return btf__str_by_offset(btf->base_btf, offset);
1592 else if (offset - btf->start_str_off < btf->hdr->str_len)
1593 return btf_strs_data(btf) + (offset - btf->start_str_off);
1594 else
1595 return errno = EINVAL, NULL;
1596 }
1597
1598 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
1599 {
1600 return btf__str_by_offset(btf, offset);
1601 }
1602
1603 struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
1604 {
1605 struct bpf_btf_info btf_info;
1606 __u32 len = sizeof(btf_info);
1607 __u32 last_size;
1608 struct btf *btf;
1609 void *ptr;
1610 int err;
1611
1612 /* we won't know btf_size until we call bpf_btf_get_info_by_fd(). so
1613 * let's start with a sane default - 4KiB here - and resize it only if
1614 * bpf_btf_get_info_by_fd() needs a bigger buffer.
1615 */
1616 last_size = 4096;
1617 ptr = malloc(last_size);
1618 if (!ptr)
1619 return ERR_PTR(-ENOMEM);
1620
1621 memset(&btf_info, 0, sizeof(btf_info));
1622 btf_info.btf = ptr_to_u64(ptr);
1623 btf_info.btf_size = last_size;
1624 err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len);
1625
1626 if (!err && btf_info.btf_size > last_size) {
1627 void *temp_ptr;
1628
1629 last_size = btf_info.btf_size;
1630 temp_ptr = realloc(ptr, last_size);
1631 if (!temp_ptr) {
1632 btf = ERR_PTR(-ENOMEM);
1633 goto exit_free;
1634 }
1635 ptr = temp_ptr;
1636
1637 len = sizeof(btf_info);
1638 memset(&btf_info, 0, sizeof(btf_info));
1639 btf_info.btf = ptr_to_u64(ptr);
1640 btf_info.btf_size = last_size;
1641
1642 err = bpf_btf_get_info_by_fd(btf_fd, &btf_info, &len);
1643 }
1644
1645 if (err || btf_info.btf_size > last_size) {
1646 btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG);
1647 goto exit_free;
1648 }
1649
1650 btf = btf_new(ptr, btf_info.btf_size, base_btf);
1651
1652 exit_free:
1653 free(ptr);
1654 return btf;
1655 }
1656
1657 struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
1658 {
1659 struct btf *btf;
1660 int btf_fd;
1661
1662 btf_fd = bpf_btf_get_fd_by_id(id);
1663 if (btf_fd < 0)
1664 return libbpf_err_ptr(-errno);
1665
1666 btf = btf_get_from_fd(btf_fd, base_btf);
1667 close(btf_fd);
1668
1669 return libbpf_ptr(btf);
1670 }
1671
1672 struct btf *btf__load_from_kernel_by_id(__u32 id)
1673 {
1674 return btf__load_from_kernel_by_id_split(id, NULL);
1675 }
1676
1677 static void btf_invalidate_raw_data(struct btf *btf)
1678 {
1679 if (btf->raw_data) {
1680 free(btf->raw_data);
1681 btf->raw_data = NULL;
1682 }
1683 if (btf->raw_data_swapped) {
1684 free(btf->raw_data_swapped);
1685 btf->raw_data_swapped = NULL;
1686 }
1687 }
1688
1689 /* Ensure BTF is ready to be modified (by splitting into a three memory
1690 * regions for header, types, and strings). Also invalidate cached
1691 * raw_data, if any.
1692 */
1693 static int btf_ensure_modifiable(struct btf *btf)
1694 {
1695 void *hdr, *types;
1696 struct strset *set = NULL;
1697 int err = -ENOMEM;
1698
1699 if (btf_is_modifiable(btf)) {
1700 /* any BTF modification invalidates raw_data */
1701 btf_invalidate_raw_data(btf);
1702 return 0;
1703 }
1704
1705 /* split raw data into three memory regions */
1706 hdr = malloc(btf->hdr->hdr_len);
1707 types = malloc(btf->hdr->type_len);
1708 if (!hdr || !types)
1709 goto err_out;
1710
1711 memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
1712 memcpy(types, btf->types_data, btf->hdr->type_len);
1713
1714 /* build lookup index for all strings */
1715 set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr->str_len);
1716 if (IS_ERR(set)) {
1717 err = PTR_ERR(set);
1718 goto err_out;
1719 }
1720
1721 /* only when everything was successful, update internal state */
1722 btf->hdr = hdr;
1723 btf->types_data = types;
1724 btf->types_data_cap = btf->hdr->type_len;
1725 btf->strs_data = NULL;
1726 btf->strs_set = set;
1727 /* if BTF was created from scratch, all strings are guaranteed to be
1728 * unique and deduplicated
1729 */
1730 if (btf->hdr->str_len == 0)
1731 btf->strs_deduped = true;
1732 if (!btf->base_btf && btf->hdr->str_len == 1)
1733 btf->strs_deduped = true;
1734
1735 /* invalidate raw_data representation */
1736 btf_invalidate_raw_data(btf);
1737
1738 return 0;
1739
1740 err_out:
1741 strset__free(set);
1742 free(hdr);
1743 free(types);
1744 return err;
1745 }
1746
1747 /* Find an offset in BTF string section that corresponds to a given string *s*.
1748 * Returns:
1749 * - >0 offset into string section, if string is found;
1750 * - -ENOENT, if string is not in the string section;
1751 * - <0, on any other error.
1752 */
1753 int btf__find_str(struct btf *btf, const char *s)
1754 {
1755 int off;
1756
1757 if (btf->base_btf) {
1758 off = btf__find_str(btf->base_btf, s);
1759 if (off != -ENOENT)
1760 return off;
1761 }
1762
1763 /* BTF needs to be in a modifiable state to build string lookup index */
1764 if (btf_ensure_modifiable(btf))
1765 return libbpf_err(-ENOMEM);
1766
1767 off = strset__find_str(btf->strs_set, s);
1768 if (off < 0)
1769 return libbpf_err(off);
1770
1771 return btf->start_str_off + off;
1772 }
1773
1774 /* Add a string s to the BTF string section.
1775 * Returns:
1776 * - > 0 offset into string section, on success;
1777 * - < 0, on error.
1778 */
1779 int btf__add_str(struct btf *btf, const char *s)
1780 {
1781 int off;
1782
1783 if (btf->base_btf) {
1784 off = btf__find_str(btf->base_btf, s);
1785 if (off != -ENOENT)
1786 return off;
1787 }
1788
1789 if (btf_ensure_modifiable(btf))
1790 return libbpf_err(-ENOMEM);
1791
1792 off = strset__add_str(btf->strs_set, s);
1793 if (off < 0)
1794 return libbpf_err(off);
1795
1796 btf->hdr->str_len = strset__data_size(btf->strs_set);
1797
1798 return btf->start_str_off + off;
1799 }
1800
1801 static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
1802 {
1803 return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
1804 btf->hdr->type_len, UINT_MAX, add_sz);
1805 }
1806
1807 static void btf_type_inc_vlen(struct btf_type *t)
1808 {
1809 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
1810 }
1811
1812 static int btf_commit_type(struct btf *btf, int data_sz)
1813 {
1814 int err;
1815
1816 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1817 if (err)
1818 return libbpf_err(err);
1819
1820 btf->hdr->type_len += data_sz;
1821 btf->hdr->str_off += data_sz;
1822 btf->nr_types++;
1823 return btf->start_id + btf->nr_types - 1;
1824 }
1825
1826 struct btf_pipe {
1827 const struct btf *src;
1828 struct btf *dst;
1829 struct hashmap *str_off_map; /* map string offsets from src to dst */
1830 };
1831
1832 static int btf_rewrite_str(__u32 *str_off, void *ctx)
1833 {
1834 struct btf_pipe *p = ctx;
1835 long mapped_off;
1836 int off, err;
1837
1838 if (!*str_off) /* nothing to do for empty strings */
1839 return 0;
1840
1841 if (p->str_off_map &&
1842 hashmap__find(p->str_off_map, *str_off, &mapped_off)) {
1843 *str_off = mapped_off;
1844 return 0;
1845 }
1846
1847 off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
1848 if (off < 0)
1849 return off;
1850
1851 /* Remember string mapping from src to dst. It avoids
1852 * performing expensive string comparisons.
1853 */
1854 if (p->str_off_map) {
1855 err = hashmap__append(p->str_off_map, *str_off, off);
1856 if (err)
1857 return err;
1858 }
1859
1860 *str_off = off;
1861 return 0;
1862 }
1863
1864 int btf__add_type(struct btf *btf, const struct btf *src_btf, const struct btf_type *src_type)
1865 {
1866 struct btf_pipe p = { .src = src_btf, .dst = btf };
1867 struct btf_type *t;
1868 int sz, err;
1869
1870 sz = btf_type_size(src_type);
1871 if (sz < 0)
1872 return libbpf_err(sz);
1873
1874 /* deconstruct BTF, if necessary, and invalidate raw_data */
1875 if (btf_ensure_modifiable(btf))
1876 return libbpf_err(-ENOMEM);
1877
1878 t = btf_add_type_mem(btf, sz);
1879 if (!t)
1880 return libbpf_err(-ENOMEM);
1881
1882 memcpy(t, src_type, sz);
1883
1884 err = btf_type_visit_str_offs(t, btf_rewrite_str, &p);
1885 if (err)
1886 return libbpf_err(err);
1887
1888 return btf_commit_type(btf, sz);
1889 }
1890
1891 static int btf_rewrite_type_ids(__u32 *type_id, void *ctx)
1892 {
1893 struct btf *btf = ctx;
1894
1895 if (!*type_id) /* nothing to do for VOID references */
1896 return 0;
1897
1898 /* we haven't updated btf's type count yet, so
1899 * btf->start_id + btf->nr_types - 1 is the type ID offset we should
1900 * add to all newly added BTF types
1901 */
1902 *type_id += btf->start_id + btf->nr_types - 1;
1903 return 0;
1904 }
1905
1906 static size_t btf_dedup_identity_hash_fn(long key, void *ctx);
1907 static bool btf_dedup_equal_fn(long k1, long k2, void *ctx);
1908
1909 int btf__add_btf(struct btf *btf, const struct btf *src_btf)
1910 {
1911 struct btf_pipe p = { .src = src_btf, .dst = btf };
1912 int data_sz, sz, cnt, i, err, old_strs_len;
1913 __u32 *off;
1914 void *t;
1915
1916 /* appending split BTF isn't supported yet */
1917 if (src_btf->base_btf)
1918 return libbpf_err(-ENOTSUP);
1919
1920 /* deconstruct BTF, if necessary, and invalidate raw_data */
1921 if (btf_ensure_modifiable(btf))
1922 return libbpf_err(-ENOMEM);
1923
1924 /* remember original strings section size if we have to roll back
1925 * partial strings section changes
1926 */
1927 old_strs_len = btf->hdr->str_len;
1928
1929 data_sz = src_btf->hdr->type_len;
1930 cnt = btf__type_cnt(src_btf) - 1;
1931
1932 /* pre-allocate enough memory for new types */
1933 t = btf_add_type_mem(btf, data_sz);
1934 if (!t)
1935 return libbpf_err(-ENOMEM);
1936
1937 /* pre-allocate enough memory for type offset index for new types */
1938 off = btf_add_type_offs_mem(btf, cnt);
1939 if (!off)
1940 return libbpf_err(-ENOMEM);
1941
1942 /* Map the string offsets from src_btf to the offsets from btf to improve performance */
1943 p.str_off_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL);
1944 if (IS_ERR(p.str_off_map))
1945 return libbpf_err(-ENOMEM);
1946
1947 /* bulk copy types data for all types from src_btf */
1948 memcpy(t, src_btf->types_data, data_sz);
1949
1950 for (i = 0; i < cnt; i++) {
1951 sz = btf_type_size(t);
1952 if (sz < 0) {
1953 /* unlikely, has to be corrupted src_btf */
1954 err = sz;
1955 goto err_out;
1956 }
1957
1958 /* fill out type ID to type offset mapping for lookups by type ID */
1959 *off = t - btf->types_data;
1960
1961 /* add, dedup, and remap strings referenced by this BTF type */
1962 err = btf_type_visit_str_offs(t, btf_rewrite_str, &p);
1963 if (err)
1964 goto err_out;
1965
1966 /* remap all type IDs referenced from this BTF type */
1967 err = btf_type_visit_type_ids(t, btf_rewrite_type_ids, btf);
1968 if (err)
1969 goto err_out;
1970
1971 /* go to next type data and type offset index entry */
1972 t += sz;
1973 off++;
1974 }
1975
1976 /* Up until now any of the copied type data was effectively invisible,
1977 * so if we exited early before this point due to error, BTF would be
1978 * effectively unmodified. There would be extra internal memory
1979 * pre-allocated, but it would not be available for querying. But now
1980 * that we've copied and rewritten all the data successfully, we can
1981 * update type count and various internal offsets and sizes to
1982 * "commit" the changes and made them visible to the outside world.
1983 */
1984 btf->hdr->type_len += data_sz;
1985 btf->hdr->str_off += data_sz;
1986 btf->nr_types += cnt;
1987
1988 hashmap__free(p.str_off_map);
1989
1990 /* return type ID of the first added BTF type */
1991 return btf->start_id + btf->nr_types - cnt;
1992 err_out:
1993 /* zero out preallocated memory as if it was just allocated with
1994 * libbpf_add_mem()
1995 */
1996 memset(btf->types_data + btf->hdr->type_len, 0, data_sz);
1997 memset(btf->strs_data + old_strs_len, 0, btf->hdr->str_len - old_strs_len);
1998
1999 /* and now restore original strings section size; types data size
2000 * wasn't modified, so doesn't need restoring, see big comment above
2001 */
2002 btf->hdr->str_len = old_strs_len;
2003
2004 hashmap__free(p.str_off_map);
2005
2006 return libbpf_err(err);
2007 }
2008
2009 /*
2010 * Append new BTF_KIND_INT type with:
2011 * - *name* - non-empty, non-NULL type name;
2012 * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
2013 * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
2014 * Returns:
2015 * - >0, type ID of newly added BTF type;
2016 * - <0, on error.
2017 */
2018 int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding)
2019 {
2020 struct btf_type *t;
2021 int sz, name_off;
2022
2023 /* non-empty name */
2024 if (!name || !name[0])
2025 return libbpf_err(-EINVAL);
2026 /* byte_sz must be power of 2 */
2027 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16)
2028 return libbpf_err(-EINVAL);
2029 if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL))
2030 return libbpf_err(-EINVAL);
2031
2032 /* deconstruct BTF, if necessary, and invalidate raw_data */
2033 if (btf_ensure_modifiable(btf))
2034 return libbpf_err(-ENOMEM);
2035
2036 sz = sizeof(struct btf_type) + sizeof(int);
2037 t = btf_add_type_mem(btf, sz);
2038 if (!t)
2039 return libbpf_err(-ENOMEM);
2040
2041 /* if something goes wrong later, we might end up with an extra string,
2042 * but that shouldn't be a problem, because BTF can't be constructed
2043 * completely anyway and will most probably be just discarded
2044 */
2045 name_off = btf__add_str(btf, name);
2046 if (name_off < 0)
2047 return name_off;
2048
2049 t->name_off = name_off;
2050 t->info = btf_type_info(BTF_KIND_INT, 0, 0);
2051 t->size = byte_sz;
2052 /* set INT info, we don't allow setting legacy bit offset/size */
2053 *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8);
2054
2055 return btf_commit_type(btf, sz);
2056 }
2057
2058 /*
2059 * Append new BTF_KIND_FLOAT type with:
2060 * - *name* - non-empty, non-NULL type name;
2061 * - *sz* - size of the type, in bytes;
2062 * Returns:
2063 * - >0, type ID of newly added BTF type;
2064 * - <0, on error.
2065 */
2066 int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
2067 {
2068 struct btf_type *t;
2069 int sz, name_off;
2070
2071 /* non-empty name */
2072 if (!name || !name[0])
2073 return libbpf_err(-EINVAL);
2074
2075 /* byte_sz must be one of the explicitly allowed values */
2076 if (byte_sz != 2 && byte_sz != 4 && byte_sz != 8 && byte_sz != 12 &&
2077 byte_sz != 16)
2078 return libbpf_err(-EINVAL);
2079
2080 if (btf_ensure_modifiable(btf))
2081 return libbpf_err(-ENOMEM);
2082
2083 sz = sizeof(struct btf_type);
2084 t = btf_add_type_mem(btf, sz);
2085 if (!t)
2086 return libbpf_err(-ENOMEM);
2087
2088 name_off = btf__add_str(btf, name);
2089 if (name_off < 0)
2090 return name_off;
2091
2092 t->name_off = name_off;
2093 t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
2094 t->size = byte_sz;
2095
2096 return btf_commit_type(btf, sz);
2097 }
2098
2099 /* it's completely legal to append BTF types with type IDs pointing forward to
2100 * types that haven't been appended yet, so we only make sure that id looks
2101 * sane, we can't guarantee that ID will always be valid
2102 */
2103 static int validate_type_id(int id)
2104 {
2105 if (id < 0 || id > BTF_MAX_NR_TYPES)
2106 return -EINVAL;
2107 return 0;
2108 }
2109
2110 /* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */
2111 static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id)
2112 {
2113 struct btf_type *t;
2114 int sz, name_off = 0;
2115
2116 if (validate_type_id(ref_type_id))
2117 return libbpf_err(-EINVAL);
2118
2119 if (btf_ensure_modifiable(btf))
2120 return libbpf_err(-ENOMEM);
2121
2122 sz = sizeof(struct btf_type);
2123 t = btf_add_type_mem(btf, sz);
2124 if (!t)
2125 return libbpf_err(-ENOMEM);
2126
2127 if (name && name[0]) {
2128 name_off = btf__add_str(btf, name);
2129 if (name_off < 0)
2130 return name_off;
2131 }
2132
2133 t->name_off = name_off;
2134 t->info = btf_type_info(kind, 0, 0);
2135 t->type = ref_type_id;
2136
2137 return btf_commit_type(btf, sz);
2138 }
2139
2140 /*
2141 * Append new BTF_KIND_PTR type with:
2142 * - *ref_type_id* - referenced type ID, it might not exist yet;
2143 * Returns:
2144 * - >0, type ID of newly added BTF type;
2145 * - <0, on error.
2146 */
2147 int btf__add_ptr(struct btf *btf, int ref_type_id)
2148 {
2149 return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id);
2150 }
2151
2152 /*
2153 * Append new BTF_KIND_ARRAY type with:
2154 * - *index_type_id* - type ID of the type describing array index;
2155 * - *elem_type_id* - type ID of the type describing array element;
2156 * - *nr_elems* - the size of the array;
2157 * Returns:
2158 * - >0, type ID of newly added BTF type;
2159 * - <0, on error.
2160 */
2161 int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems)
2162 {
2163 struct btf_type *t;
2164 struct btf_array *a;
2165 int sz;
2166
2167 if (validate_type_id(index_type_id) || validate_type_id(elem_type_id))
2168 return libbpf_err(-EINVAL);
2169
2170 if (btf_ensure_modifiable(btf))
2171 return libbpf_err(-ENOMEM);
2172
2173 sz = sizeof(struct btf_type) + sizeof(struct btf_array);
2174 t = btf_add_type_mem(btf, sz);
2175 if (!t)
2176 return libbpf_err(-ENOMEM);
2177
2178 t->name_off = 0;
2179 t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0);
2180 t->size = 0;
2181
2182 a = btf_array(t);
2183 a->type = elem_type_id;
2184 a->index_type = index_type_id;
2185 a->nelems = nr_elems;
2186
2187 return btf_commit_type(btf, sz);
2188 }
2189
2190 /* generic STRUCT/UNION append function */
2191 static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz)
2192 {
2193 struct btf_type *t;
2194 int sz, name_off = 0;
2195
2196 if (btf_ensure_modifiable(btf))
2197 return libbpf_err(-ENOMEM);
2198
2199 sz = sizeof(struct btf_type);
2200 t = btf_add_type_mem(btf, sz);
2201 if (!t)
2202 return libbpf_err(-ENOMEM);
2203
2204 if (name && name[0]) {
2205 name_off = btf__add_str(btf, name);
2206 if (name_off < 0)
2207 return name_off;
2208 }
2209
2210 /* start out with vlen=0 and no kflag; this will be adjusted when
2211 * adding each member
2212 */
2213 t->name_off = name_off;
2214 t->info = btf_type_info(kind, 0, 0);
2215 t->size = bytes_sz;
2216
2217 return btf_commit_type(btf, sz);
2218 }
2219
2220 /*
2221 * Append new BTF_KIND_STRUCT type with:
2222 * - *name* - name of the struct, can be NULL or empty for anonymous structs;
2223 * - *byte_sz* - size of the struct, in bytes;
2224 *
2225 * Struct initially has no fields in it. Fields can be added by
2226 * btf__add_field() right after btf__add_struct() succeeds.
2227 *
2228 * Returns:
2229 * - >0, type ID of newly added BTF type;
2230 * - <0, on error.
2231 */
2232 int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz)
2233 {
2234 return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz);
2235 }
2236
2237 /*
2238 * Append new BTF_KIND_UNION type with:
2239 * - *name* - name of the union, can be NULL or empty for anonymous union;
2240 * - *byte_sz* - size of the union, in bytes;
2241 *
2242 * Union initially has no fields in it. Fields can be added by
2243 * btf__add_field() right after btf__add_union() succeeds. All fields
2244 * should have *bit_offset* of 0.
2245 *
2246 * Returns:
2247 * - >0, type ID of newly added BTF type;
2248 * - <0, on error.
2249 */
2250 int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
2251 {
2252 return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz);
2253 }
2254
2255 static struct btf_type *btf_last_type(struct btf *btf)
2256 {
2257 return btf_type_by_id(btf, btf__type_cnt(btf) - 1);
2258 }
2259
2260 /*
2261 * Append new field for the current STRUCT/UNION type with:
2262 * - *name* - name of the field, can be NULL or empty for anonymous field;
2263 * - *type_id* - type ID for the type describing field type;
2264 * - *bit_offset* - bit offset of the start of the field within struct/union;
2265 * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
2266 * Returns:
2267 * - 0, on success;
2268 * - <0, on error.
2269 */
2270 int btf__add_field(struct btf *btf, const char *name, int type_id,
2271 __u32 bit_offset, __u32 bit_size)
2272 {
2273 struct btf_type *t;
2274 struct btf_member *m;
2275 bool is_bitfield;
2276 int sz, name_off = 0;
2277
2278 /* last type should be union/struct */
2279 if (btf->nr_types == 0)
2280 return libbpf_err(-EINVAL);
2281 t = btf_last_type(btf);
2282 if (!btf_is_composite(t))
2283 return libbpf_err(-EINVAL);
2284
2285 if (validate_type_id(type_id))
2286 return libbpf_err(-EINVAL);
2287 /* best-effort bit field offset/size enforcement */
2288 is_bitfield = bit_size || (bit_offset % 8 != 0);
2289 if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff))
2290 return libbpf_err(-EINVAL);
2291
2292 /* only offset 0 is allowed for unions */
2293 if (btf_is_union(t) && bit_offset)
2294 return libbpf_err(-EINVAL);
2295
2296 /* decompose and invalidate raw data */
2297 if (btf_ensure_modifiable(btf))
2298 return libbpf_err(-ENOMEM);
2299
2300 sz = sizeof(struct btf_member);
2301 m = btf_add_type_mem(btf, sz);
2302 if (!m)
2303 return libbpf_err(-ENOMEM);
2304
2305 if (name && name[0]) {
2306 name_off = btf__add_str(btf, name);
2307 if (name_off < 0)
2308 return name_off;
2309 }
2310
2311 m->name_off = name_off;
2312 m->type = type_id;
2313 m->offset = bit_offset | (bit_size << 24);
2314
2315 /* btf_add_type_mem can invalidate t pointer */
2316 t = btf_last_type(btf);
2317 /* update parent type's vlen and kflag */
2318 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
2319
2320 btf->hdr->type_len += sz;
2321 btf->hdr->str_off += sz;
2322 return 0;
2323 }
2324
2325 static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz,
2326 bool is_signed, __u8 kind)
2327 {
2328 struct btf_type *t;
2329 int sz, name_off = 0;
2330
2331 /* byte_sz must be power of 2 */
2332 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
2333 return libbpf_err(-EINVAL);
2334
2335 if (btf_ensure_modifiable(btf))
2336 return libbpf_err(-ENOMEM);
2337
2338 sz = sizeof(struct btf_type);
2339 t = btf_add_type_mem(btf, sz);
2340 if (!t)
2341 return libbpf_err(-ENOMEM);
2342
2343 if (name && name[0]) {
2344 name_off = btf__add_str(btf, name);
2345 if (name_off < 0)
2346 return name_off;
2347 }
2348
2349 /* start out with vlen=0; it will be adjusted when adding enum values */
2350 t->name_off = name_off;
2351 t->info = btf_type_info(kind, 0, is_signed);
2352 t->size = byte_sz;
2353
2354 return btf_commit_type(btf, sz);
2355 }
2356
2357 /*
2358 * Append new BTF_KIND_ENUM type with:
2359 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2360 * - *byte_sz* - size of the enum, in bytes.
2361 *
2362 * Enum initially has no enum values in it (and corresponds to enum forward
2363 * declaration). Enumerator values can be added by btf__add_enum_value()
2364 * immediately after btf__add_enum() succeeds.
2365 *
2366 * Returns:
2367 * - >0, type ID of newly added BTF type;
2368 * - <0, on error.
2369 */
2370 int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
2371 {
2372 /*
2373 * set the signedness to be unsigned, it will change to signed
2374 * if any later enumerator is negative.
2375 */
2376 return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM);
2377 }
2378
2379 /*
2380 * Append new enum value for the current ENUM type with:
2381 * - *name* - name of the enumerator value, can't be NULL or empty;
2382 * - *value* - integer value corresponding to enum value *name*;
2383 * Returns:
2384 * - 0, on success;
2385 * - <0, on error.
2386 */
2387 int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
2388 {
2389 struct btf_type *t;
2390 struct btf_enum *v;
2391 int sz, name_off;
2392
2393 /* last type should be BTF_KIND_ENUM */
2394 if (btf->nr_types == 0)
2395 return libbpf_err(-EINVAL);
2396 t = btf_last_type(btf);
2397 if (!btf_is_enum(t))
2398 return libbpf_err(-EINVAL);
2399
2400 /* non-empty name */
2401 if (!name || !name[0])
2402 return libbpf_err(-EINVAL);
2403 if (value < INT_MIN || value > UINT_MAX)
2404 return libbpf_err(-E2BIG);
2405
2406 /* decompose and invalidate raw data */
2407 if (btf_ensure_modifiable(btf))
2408 return libbpf_err(-ENOMEM);
2409
2410 sz = sizeof(struct btf_enum);
2411 v = btf_add_type_mem(btf, sz);
2412 if (!v)
2413 return libbpf_err(-ENOMEM);
2414
2415 name_off = btf__add_str(btf, name);
2416 if (name_off < 0)
2417 return name_off;
2418
2419 v->name_off = name_off;
2420 v->val = value;
2421
2422 /* update parent type's vlen */
2423 t = btf_last_type(btf);
2424 btf_type_inc_vlen(t);
2425
2426 /* if negative value, set signedness to signed */
2427 if (value < 0)
2428 t->info = btf_type_info(btf_kind(t), btf_vlen(t), true);
2429
2430 btf->hdr->type_len += sz;
2431 btf->hdr->str_off += sz;
2432 return 0;
2433 }
2434
2435 /*
2436 * Append new BTF_KIND_ENUM64 type with:
2437 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2438 * - *byte_sz* - size of the enum, in bytes.
2439 * - *is_signed* - whether the enum values are signed or not;
2440 *
2441 * Enum initially has no enum values in it (and corresponds to enum forward
2442 * declaration). Enumerator values can be added by btf__add_enum64_value()
2443 * immediately after btf__add_enum64() succeeds.
2444 *
2445 * Returns:
2446 * - >0, type ID of newly added BTF type;
2447 * - <0, on error.
2448 */
2449 int btf__add_enum64(struct btf *btf, const char *name, __u32 byte_sz,
2450 bool is_signed)
2451 {
2452 return btf_add_enum_common(btf, name, byte_sz, is_signed,
2453 BTF_KIND_ENUM64);
2454 }
2455
2456 /*
2457 * Append new enum value for the current ENUM64 type with:
2458 * - *name* - name of the enumerator value, can't be NULL or empty;
2459 * - *value* - integer value corresponding to enum value *name*;
2460 * Returns:
2461 * - 0, on success;
2462 * - <0, on error.
2463 */
2464 int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value)
2465 {
2466 struct btf_enum64 *v;
2467 struct btf_type *t;
2468 int sz, name_off;
2469
2470 /* last type should be BTF_KIND_ENUM64 */
2471 if (btf->nr_types == 0)
2472 return libbpf_err(-EINVAL);
2473 t = btf_last_type(btf);
2474 if (!btf_is_enum64(t))
2475 return libbpf_err(-EINVAL);
2476
2477 /* non-empty name */
2478 if (!name || !name[0])
2479 return libbpf_err(-EINVAL);
2480
2481 /* decompose and invalidate raw data */
2482 if (btf_ensure_modifiable(btf))
2483 return libbpf_err(-ENOMEM);
2484
2485 sz = sizeof(struct btf_enum64);
2486 v = btf_add_type_mem(btf, sz);
2487 if (!v)
2488 return libbpf_err(-ENOMEM);
2489
2490 name_off = btf__add_str(btf, name);
2491 if (name_off < 0)
2492 return name_off;
2493
2494 v->name_off = name_off;
2495 v->val_lo32 = (__u32)value;
2496 v->val_hi32 = value >> 32;
2497
2498 /* update parent type's vlen */
2499 t = btf_last_type(btf);
2500 btf_type_inc_vlen(t);
2501
2502 btf->hdr->type_len += sz;
2503 btf->hdr->str_off += sz;
2504 return 0;
2505 }
2506
2507 /*
2508 * Append new BTF_KIND_FWD type with:
2509 * - *name*, non-empty/non-NULL name;
2510 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2511 * BTF_FWD_UNION, or BTF_FWD_ENUM;
2512 * Returns:
2513 * - >0, type ID of newly added BTF type;
2514 * - <0, on error.
2515 */
2516 int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
2517 {
2518 if (!name || !name[0])
2519 return libbpf_err(-EINVAL);
2520
2521 switch (fwd_kind) {
2522 case BTF_FWD_STRUCT:
2523 case BTF_FWD_UNION: {
2524 struct btf_type *t;
2525 int id;
2526
2527 id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0);
2528 if (id <= 0)
2529 return id;
2530 t = btf_type_by_id(btf, id);
2531 t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION);
2532 return id;
2533 }
2534 case BTF_FWD_ENUM:
2535 /* enum forward in BTF currently is just an enum with no enum
2536 * values; we also assume a standard 4-byte size for it
2537 */
2538 return btf__add_enum(btf, name, sizeof(int));
2539 default:
2540 return libbpf_err(-EINVAL);
2541 }
2542 }
2543
2544 /*
2545 * Append new BTF_KING_TYPEDEF type with:
2546 * - *name*, non-empty/non-NULL name;
2547 * - *ref_type_id* - referenced type ID, it might not exist yet;
2548 * Returns:
2549 * - >0, type ID of newly added BTF type;
2550 * - <0, on error.
2551 */
2552 int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)
2553 {
2554 if (!name || !name[0])
2555 return libbpf_err(-EINVAL);
2556
2557 return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id);
2558 }
2559
2560 /*
2561 * Append new BTF_KIND_VOLATILE type with:
2562 * - *ref_type_id* - referenced type ID, it might not exist yet;
2563 * Returns:
2564 * - >0, type ID of newly added BTF type;
2565 * - <0, on error.
2566 */
2567 int btf__add_volatile(struct btf *btf, int ref_type_id)
2568 {
2569 return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id);
2570 }
2571
2572 /*
2573 * Append new BTF_KIND_CONST type with:
2574 * - *ref_type_id* - referenced type ID, it might not exist yet;
2575 * Returns:
2576 * - >0, type ID of newly added BTF type;
2577 * - <0, on error.
2578 */
2579 int btf__add_const(struct btf *btf, int ref_type_id)
2580 {
2581 return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id);
2582 }
2583
2584 /*
2585 * Append new BTF_KIND_RESTRICT type with:
2586 * - *ref_type_id* - referenced type ID, it might not exist yet;
2587 * Returns:
2588 * - >0, type ID of newly added BTF type;
2589 * - <0, on error.
2590 */
2591 int btf__add_restrict(struct btf *btf, int ref_type_id)
2592 {
2593 return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
2594 }
2595
2596 /*
2597 * Append new BTF_KIND_TYPE_TAG type with:
2598 * - *value*, non-empty/non-NULL tag value;
2599 * - *ref_type_id* - referenced type ID, it might not exist yet;
2600 * Returns:
2601 * - >0, type ID of newly added BTF type;
2602 * - <0, on error.
2603 */
2604 int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id)
2605 {
2606 if (!value || !value[0])
2607 return libbpf_err(-EINVAL);
2608
2609 return btf_add_ref_kind(btf, BTF_KIND_TYPE_TAG, value, ref_type_id);
2610 }
2611
2612 /*
2613 * Append new BTF_KIND_FUNC type with:
2614 * - *name*, non-empty/non-NULL name;
2615 * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
2616 * Returns:
2617 * - >0, type ID of newly added BTF type;
2618 * - <0, on error.
2619 */
2620 int btf__add_func(struct btf *btf, const char *name,
2621 enum btf_func_linkage linkage, int proto_type_id)
2622 {
2623 int id;
2624
2625 if (!name || !name[0])
2626 return libbpf_err(-EINVAL);
2627 if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL &&
2628 linkage != BTF_FUNC_EXTERN)
2629 return libbpf_err(-EINVAL);
2630
2631 id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id);
2632 if (id > 0) {
2633 struct btf_type *t = btf_type_by_id(btf, id);
2634
2635 t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0);
2636 }
2637 return libbpf_err(id);
2638 }
2639
2640 /*
2641 * Append new BTF_KIND_FUNC_PROTO with:
2642 * - *ret_type_id* - type ID for return result of a function.
2643 *
2644 * Function prototype initially has no arguments, but they can be added by
2645 * btf__add_func_param() one by one, immediately after
2646 * btf__add_func_proto() succeeded.
2647 *
2648 * Returns:
2649 * - >0, type ID of newly added BTF type;
2650 * - <0, on error.
2651 */
2652 int btf__add_func_proto(struct btf *btf, int ret_type_id)
2653 {
2654 struct btf_type *t;
2655 int sz;
2656
2657 if (validate_type_id(ret_type_id))
2658 return libbpf_err(-EINVAL);
2659
2660 if (btf_ensure_modifiable(btf))
2661 return libbpf_err(-ENOMEM);
2662
2663 sz = sizeof(struct btf_type);
2664 t = btf_add_type_mem(btf, sz);
2665 if (!t)
2666 return libbpf_err(-ENOMEM);
2667
2668 /* start out with vlen=0; this will be adjusted when adding enum
2669 * values, if necessary
2670 */
2671 t->name_off = 0;
2672 t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0);
2673 t->type = ret_type_id;
2674
2675 return btf_commit_type(btf, sz);
2676 }
2677
2678 /*
2679 * Append new function parameter for current FUNC_PROTO type with:
2680 * - *name* - parameter name, can be NULL or empty;
2681 * - *type_id* - type ID describing the type of the parameter.
2682 * Returns:
2683 * - 0, on success;
2684 * - <0, on error.
2685 */
2686 int btf__add_func_param(struct btf *btf, const char *name, int type_id)
2687 {
2688 struct btf_type *t;
2689 struct btf_param *p;
2690 int sz, name_off = 0;
2691
2692 if (validate_type_id(type_id))
2693 return libbpf_err(-EINVAL);
2694
2695 /* last type should be BTF_KIND_FUNC_PROTO */
2696 if (btf->nr_types == 0)
2697 return libbpf_err(-EINVAL);
2698 t = btf_last_type(btf);
2699 if (!btf_is_func_proto(t))
2700 return libbpf_err(-EINVAL);
2701
2702 /* decompose and invalidate raw data */
2703 if (btf_ensure_modifiable(btf))
2704 return libbpf_err(-ENOMEM);
2705
2706 sz = sizeof(struct btf_param);
2707 p = btf_add_type_mem(btf, sz);
2708 if (!p)
2709 return libbpf_err(-ENOMEM);
2710
2711 if (name && name[0]) {
2712 name_off = btf__add_str(btf, name);
2713 if (name_off < 0)
2714 return name_off;
2715 }
2716
2717 p->name_off = name_off;
2718 p->type = type_id;
2719
2720 /* update parent type's vlen */
2721 t = btf_last_type(btf);
2722 btf_type_inc_vlen(t);
2723
2724 btf->hdr->type_len += sz;
2725 btf->hdr->str_off += sz;
2726 return 0;
2727 }
2728
2729 /*
2730 * Append new BTF_KIND_VAR type with:
2731 * - *name* - non-empty/non-NULL name;
2732 * - *linkage* - variable linkage, one of BTF_VAR_STATIC,
2733 * BTF_VAR_GLOBAL_ALLOCATED, or BTF_VAR_GLOBAL_EXTERN;
2734 * - *type_id* - type ID of the type describing the type of the variable.
2735 * Returns:
2736 * - >0, type ID of newly added BTF type;
2737 * - <0, on error.
2738 */
2739 int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id)
2740 {
2741 struct btf_type *t;
2742 struct btf_var *v;
2743 int sz, name_off;
2744
2745 /* non-empty name */
2746 if (!name || !name[0])
2747 return libbpf_err(-EINVAL);
2748 if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2749 linkage != BTF_VAR_GLOBAL_EXTERN)
2750 return libbpf_err(-EINVAL);
2751 if (validate_type_id(type_id))
2752 return libbpf_err(-EINVAL);
2753
2754 /* deconstruct BTF, if necessary, and invalidate raw_data */
2755 if (btf_ensure_modifiable(btf))
2756 return libbpf_err(-ENOMEM);
2757
2758 sz = sizeof(struct btf_type) + sizeof(struct btf_var);
2759 t = btf_add_type_mem(btf, sz);
2760 if (!t)
2761 return libbpf_err(-ENOMEM);
2762
2763 name_off = btf__add_str(btf, name);
2764 if (name_off < 0)
2765 return name_off;
2766
2767 t->name_off = name_off;
2768 t->info = btf_type_info(BTF_KIND_VAR, 0, 0);
2769 t->type = type_id;
2770
2771 v = btf_var(t);
2772 v->linkage = linkage;
2773
2774 return btf_commit_type(btf, sz);
2775 }
2776
2777 /*
2778 * Append new BTF_KIND_DATASEC type with:
2779 * - *name* - non-empty/non-NULL name;
2780 * - *byte_sz* - data section size, in bytes.
2781 *
2782 * Data section is initially empty. Variables info can be added with
2783 * btf__add_datasec_var_info() calls, after btf__add_datasec() succeeds.
2784 *
2785 * Returns:
2786 * - >0, type ID of newly added BTF type;
2787 * - <0, on error.
2788 */
2789 int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
2790 {
2791 struct btf_type *t;
2792 int sz, name_off;
2793
2794 /* non-empty name */
2795 if (!name || !name[0])
2796 return libbpf_err(-EINVAL);
2797
2798 if (btf_ensure_modifiable(btf))
2799 return libbpf_err(-ENOMEM);
2800
2801 sz = sizeof(struct btf_type);
2802 t = btf_add_type_mem(btf, sz);
2803 if (!t)
2804 return libbpf_err(-ENOMEM);
2805
2806 name_off = btf__add_str(btf, name);
2807 if (name_off < 0)
2808 return name_off;
2809
2810 /* start with vlen=0, which will be update as var_secinfos are added */
2811 t->name_off = name_off;
2812 t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0);
2813 t->size = byte_sz;
2814
2815 return btf_commit_type(btf, sz);
2816 }
2817
2818 /*
2819 * Append new data section variable information entry for current DATASEC type:
2820 * - *var_type_id* - type ID, describing type of the variable;
2821 * - *offset* - variable offset within data section, in bytes;
2822 * - *byte_sz* - variable size, in bytes.
2823 *
2824 * Returns:
2825 * - 0, on success;
2826 * - <0, on error.
2827 */
2828 int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz)
2829 {
2830 struct btf_type *t;
2831 struct btf_var_secinfo *v;
2832 int sz;
2833
2834 /* last type should be BTF_KIND_DATASEC */
2835 if (btf->nr_types == 0)
2836 return libbpf_err(-EINVAL);
2837 t = btf_last_type(btf);
2838 if (!btf_is_datasec(t))
2839 return libbpf_err(-EINVAL);
2840
2841 if (validate_type_id(var_type_id))
2842 return libbpf_err(-EINVAL);
2843
2844 /* decompose and invalidate raw data */
2845 if (btf_ensure_modifiable(btf))
2846 return libbpf_err(-ENOMEM);
2847
2848 sz = sizeof(struct btf_var_secinfo);
2849 v = btf_add_type_mem(btf, sz);
2850 if (!v)
2851 return libbpf_err(-ENOMEM);
2852
2853 v->type = var_type_id;
2854 v->offset = offset;
2855 v->size = byte_sz;
2856
2857 /* update parent type's vlen */
2858 t = btf_last_type(btf);
2859 btf_type_inc_vlen(t);
2860
2861 btf->hdr->type_len += sz;
2862 btf->hdr->str_off += sz;
2863 return 0;
2864 }
2865
2866 /*
2867 * Append new BTF_KIND_DECL_TAG type with:
2868 * - *value* - non-empty/non-NULL string;
2869 * - *ref_type_id* - referenced type ID, it might not exist yet;
2870 * - *component_idx* - -1 for tagging reference type, otherwise struct/union
2871 * member or function argument index;
2872 * Returns:
2873 * - >0, type ID of newly added BTF type;
2874 * - <0, on error.
2875 */
2876 int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,
2877 int component_idx)
2878 {
2879 struct btf_type *t;
2880 int sz, value_off;
2881
2882 if (!value || !value[0] || component_idx < -1)
2883 return libbpf_err(-EINVAL);
2884
2885 if (validate_type_id(ref_type_id))
2886 return libbpf_err(-EINVAL);
2887
2888 if (btf_ensure_modifiable(btf))
2889 return libbpf_err(-ENOMEM);
2890
2891 sz = sizeof(struct btf_type) + sizeof(struct btf_decl_tag);
2892 t = btf_add_type_mem(btf, sz);
2893 if (!t)
2894 return libbpf_err(-ENOMEM);
2895
2896 value_off = btf__add_str(btf, value);
2897 if (value_off < 0)
2898 return value_off;
2899
2900 t->name_off = value_off;
2901 t->info = btf_type_info(BTF_KIND_DECL_TAG, 0, false);
2902 t->type = ref_type_id;
2903 btf_decl_tag(t)->component_idx = component_idx;
2904
2905 return btf_commit_type(btf, sz);
2906 }
2907
2908 struct btf_ext_sec_setup_param {
2909 __u32 off;
2910 __u32 len;
2911 __u32 min_rec_size;
2912 struct btf_ext_info *ext_info;
2913 const char *desc;
2914 };
2915
2916 static int btf_ext_setup_info(struct btf_ext *btf_ext,
2917 struct btf_ext_sec_setup_param *ext_sec)
2918 {
2919 const struct btf_ext_info_sec *sinfo;
2920 struct btf_ext_info *ext_info;
2921 __u32 info_left, record_size;
2922 size_t sec_cnt = 0;
2923 /* The start of the info sec (including the __u32 record_size). */
2924 void *info;
2925
2926 if (ext_sec->len == 0)
2927 return 0;
2928
2929 if (ext_sec->off & 0x03) {
2930 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
2931 ext_sec->desc);
2932 return -EINVAL;
2933 }
2934
2935 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
2936 info_left = ext_sec->len;
2937
2938 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
2939 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
2940 ext_sec->desc, ext_sec->off, ext_sec->len);
2941 return -EINVAL;
2942 }
2943
2944 /* At least a record size */
2945 if (info_left < sizeof(__u32)) {
2946 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
2947 return -EINVAL;
2948 }
2949
2950 /* The record size needs to meet the minimum standard */
2951 record_size = *(__u32 *)info;
2952 if (record_size < ext_sec->min_rec_size ||
2953 record_size & 0x03) {
2954 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
2955 ext_sec->desc, record_size);
2956 return -EINVAL;
2957 }
2958
2959 sinfo = info + sizeof(__u32);
2960 info_left -= sizeof(__u32);
2961
2962 /* If no records, return failure now so .BTF.ext won't be used. */
2963 if (!info_left) {
2964 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
2965 return -EINVAL;
2966 }
2967
2968 while (info_left) {
2969 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
2970 __u64 total_record_size;
2971 __u32 num_records;
2972
2973 if (info_left < sec_hdrlen) {
2974 pr_debug("%s section header is not found in .BTF.ext\n",
2975 ext_sec->desc);
2976 return -EINVAL;
2977 }
2978
2979 num_records = sinfo->num_info;
2980 if (num_records == 0) {
2981 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2982 ext_sec->desc);
2983 return -EINVAL;
2984 }
2985
2986 total_record_size = sec_hdrlen + (__u64)num_records * record_size;
2987 if (info_left < total_record_size) {
2988 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2989 ext_sec->desc);
2990 return -EINVAL;
2991 }
2992
2993 info_left -= total_record_size;
2994 sinfo = (void *)sinfo + total_record_size;
2995 sec_cnt++;
2996 }
2997
2998 ext_info = ext_sec->ext_info;
2999 ext_info->len = ext_sec->len - sizeof(__u32);
3000 ext_info->rec_size = record_size;
3001 ext_info->info = info + sizeof(__u32);
3002 ext_info->sec_cnt = sec_cnt;
3003
3004 return 0;
3005 }
3006
3007 static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
3008 {
3009 struct btf_ext_sec_setup_param param = {
3010 .off = btf_ext->hdr->func_info_off,
3011 .len = btf_ext->hdr->func_info_len,
3012 .min_rec_size = sizeof(struct bpf_func_info_min),
3013 .ext_info = &btf_ext->func_info,
3014 .desc = "func_info"
3015 };
3016
3017 return btf_ext_setup_info(btf_ext, ¶m);
3018 }
3019
3020 static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
3021 {
3022 struct btf_ext_sec_setup_param param = {
3023 .off = btf_ext->hdr->line_info_off,
3024 .len = btf_ext->hdr->line_info_len,
3025 .min_rec_size = sizeof(struct bpf_line_info_min),
3026 .ext_info = &btf_ext->line_info,
3027 .desc = "line_info",
3028 };
3029
3030 return btf_ext_setup_info(btf_ext, ¶m);
3031 }
3032
3033 static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
3034 {
3035 struct btf_ext_sec_setup_param param = {
3036 .off = btf_ext->hdr->core_relo_off,
3037 .len = btf_ext->hdr->core_relo_len,
3038 .min_rec_size = sizeof(struct bpf_core_relo),
3039 .ext_info = &btf_ext->core_relo_info,
3040 .desc = "core_relo",
3041 };
3042
3043 return btf_ext_setup_info(btf_ext, ¶m);
3044 }
3045
3046 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
3047 {
3048 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
3049
3050 if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
3051 data_size < hdr->hdr_len) {
3052 pr_debug("BTF.ext header not found");
3053 return -EINVAL;
3054 }
3055
3056 if (hdr->magic == bswap_16(BTF_MAGIC)) {
3057 pr_warn("BTF.ext in non-native endianness is not supported\n");
3058 return -ENOTSUP;
3059 } else if (hdr->magic != BTF_MAGIC) {
3060 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
3061 return -EINVAL;
3062 }
3063
3064 if (hdr->version != BTF_VERSION) {
3065 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
3066 return -ENOTSUP;
3067 }
3068
3069 if (hdr->flags) {
3070 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
3071 return -ENOTSUP;
3072 }
3073
3074 if (data_size == hdr->hdr_len) {
3075 pr_debug("BTF.ext has no data\n");
3076 return -EINVAL;
3077 }
3078
3079 return 0;
3080 }
3081
3082 void btf_ext__free(struct btf_ext *btf_ext)
3083 {
3084 if (IS_ERR_OR_NULL(btf_ext))
3085 return;
3086 free(btf_ext->func_info.sec_idxs);
3087 free(btf_ext->line_info.sec_idxs);
3088 free(btf_ext->core_relo_info.sec_idxs);
3089 free(btf_ext->data);
3090 free(btf_ext);
3091 }
3092
3093 struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
3094 {
3095 struct btf_ext *btf_ext;
3096 int err;
3097
3098 btf_ext = calloc(1, sizeof(struct btf_ext));
3099 if (!btf_ext)
3100 return libbpf_err_ptr(-ENOMEM);
3101
3102 btf_ext->data_size = size;
3103 btf_ext->data = malloc(size);
3104 if (!btf_ext->data) {
3105 err = -ENOMEM;
3106 goto done;
3107 }
3108 memcpy(btf_ext->data, data, size);
3109
3110 err = btf_ext_parse_hdr(btf_ext->data, size);
3111 if (err)
3112 goto done;
3113
3114 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) {
3115 err = -EINVAL;
3116 goto done;
3117 }
3118
3119 err = btf_ext_setup_func_info(btf_ext);
3120 if (err)
3121 goto done;
3122
3123 err = btf_ext_setup_line_info(btf_ext);
3124 if (err)
3125 goto done;
3126
3127 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
3128 goto done; /* skip core relos parsing */
3129
3130 err = btf_ext_setup_core_relos(btf_ext);
3131 if (err)
3132 goto done;
3133
3134 done:
3135 if (err) {
3136 btf_ext__free(btf_ext);
3137 return libbpf_err_ptr(err);
3138 }
3139
3140 return btf_ext;
3141 }
3142
3143 const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
3144 {
3145 *size = btf_ext->data_size;
3146 return btf_ext->data;
3147 }
3148
3149 struct btf_dedup;
3150
3151 static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts);
3152 static void btf_dedup_free(struct btf_dedup *d);
3153 static int btf_dedup_prep(struct btf_dedup *d);
3154 static int btf_dedup_strings(struct btf_dedup *d);
3155 static int btf_dedup_prim_types(struct btf_dedup *d);
3156 static int btf_dedup_struct_types(struct btf_dedup *d);
3157 static int btf_dedup_ref_types(struct btf_dedup *d);
3158 static int btf_dedup_resolve_fwds(struct btf_dedup *d);
3159 static int btf_dedup_compact_types(struct btf_dedup *d);
3160 static int btf_dedup_remap_types(struct btf_dedup *d);
3161
3162 /*
3163 * Deduplicate BTF types and strings.
3164 *
3165 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
3166 * section with all BTF type descriptors and string data. It overwrites that
3167 * memory in-place with deduplicated types and strings without any loss of
3168 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
3169 * is provided, all the strings referenced from .BTF.ext section are honored
3170 * and updated to point to the right offsets after deduplication.
3171 *
3172 * If function returns with error, type/string data might be garbled and should
3173 * be discarded.
3174 *
3175 * More verbose and detailed description of both problem btf_dedup is solving,
3176 * as well as solution could be found at:
3177 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
3178 *
3179 * Problem description and justification
3180 * =====================================
3181 *
3182 * BTF type information is typically emitted either as a result of conversion
3183 * from DWARF to BTF or directly by compiler. In both cases, each compilation
3184 * unit contains information about a subset of all the types that are used
3185 * in an application. These subsets are frequently overlapping and contain a lot
3186 * of duplicated information when later concatenated together into a single
3187 * binary. This algorithm ensures that each unique type is represented by single
3188 * BTF type descriptor, greatly reducing resulting size of BTF data.
3189 *
3190 * Compilation unit isolation and subsequent duplication of data is not the only
3191 * problem. The same type hierarchy (e.g., struct and all the type that struct
3192 * references) in different compilation units can be represented in BTF to
3193 * various degrees of completeness (or, rather, incompleteness) due to
3194 * struct/union forward declarations.
3195 *
3196 * Let's take a look at an example, that we'll use to better understand the
3197 * problem (and solution). Suppose we have two compilation units, each using
3198 * same `struct S`, but each of them having incomplete type information about
3199 * struct's fields:
3200 *
3201 * // CU #1:
3202 * struct S;
3203 * struct A {
3204 * int a;
3205 * struct A* self;
3206 * struct S* parent;
3207 * };
3208 * struct B;
3209 * struct S {
3210 * struct A* a_ptr;
3211 * struct B* b_ptr;
3212 * };
3213 *
3214 * // CU #2:
3215 * struct S;
3216 * struct A;
3217 * struct B {
3218 * int b;
3219 * struct B* self;
3220 * struct S* parent;
3221 * };
3222 * struct S {
3223 * struct A* a_ptr;
3224 * struct B* b_ptr;
3225 * };
3226 *
3227 * In case of CU #1, BTF data will know only that `struct B` exist (but no
3228 * more), but will know the complete type information about `struct A`. While
3229 * for CU #2, it will know full type information about `struct B`, but will
3230 * only know about forward declaration of `struct A` (in BTF terms, it will
3231 * have `BTF_KIND_FWD` type descriptor with name `B`).
3232 *
3233 * This compilation unit isolation means that it's possible that there is no
3234 * single CU with complete type information describing structs `S`, `A`, and
3235 * `B`. Also, we might get tons of duplicated and redundant type information.
3236 *
3237 * Additional complication we need to keep in mind comes from the fact that
3238 * types, in general, can form graphs containing cycles, not just DAGs.
3239 *
3240 * While algorithm does deduplication, it also merges and resolves type
3241 * information (unless disabled throught `struct btf_opts`), whenever possible.
3242 * E.g., in the example above with two compilation units having partial type
3243 * information for structs `A` and `B`, the output of algorithm will emit
3244 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
3245 * (as well as type information for `int` and pointers), as if they were defined
3246 * in a single compilation unit as:
3247 *
3248 * struct A {
3249 * int a;
3250 * struct A* self;
3251 * struct S* parent;
3252 * };
3253 * struct B {
3254 * int b;
3255 * struct B* self;
3256 * struct S* parent;
3257 * };
3258 * struct S {
3259 * struct A* a_ptr;
3260 * struct B* b_ptr;
3261 * };
3262 *
3263 * Algorithm summary
3264 * =================
3265 *
3266 * Algorithm completes its work in 7 separate passes:
3267 *
3268 * 1. Strings deduplication.
3269 * 2. Primitive types deduplication (int, enum, fwd).
3270 * 3. Struct/union types deduplication.
3271 * 4. Resolve unambiguous forward declarations.
3272 * 5. Reference types deduplication (pointers, typedefs, arrays, funcs, func
3273 * protos, and const/volatile/restrict modifiers).
3274 * 6. Types compaction.
3275 * 7. Types remapping.
3276 *
3277 * Algorithm determines canonical type descriptor, which is a single
3278 * representative type for each truly unique type. This canonical type is the
3279 * one that will go into final deduplicated BTF type information. For
3280 * struct/unions, it is also the type that algorithm will merge additional type
3281 * information into (while resolving FWDs), as it discovers it from data in
3282 * other CUs. Each input BTF type eventually gets either mapped to itself, if
3283 * that type is canonical, or to some other type, if that type is equivalent
3284 * and was chosen as canonical representative. This mapping is stored in
3285 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
3286 * FWD type got resolved to.
3287 *
3288 * To facilitate fast discovery of canonical types, we also maintain canonical
3289 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
3290 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
3291 * that match that signature. With sufficiently good choice of type signature
3292 * hashing function, we can limit number of canonical types for each unique type
3293 * signature to a very small number, allowing to find canonical type for any
3294 * duplicated type very quickly.
3295 *
3296 * Struct/union deduplication is the most critical part and algorithm for
3297 * deduplicating structs/unions is described in greater details in comments for
3298 * `btf_dedup_is_equiv` function.
3299 */
3300 int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts)
3301 {
3302 struct btf_dedup *d;
3303 int err;
3304
3305 if (!OPTS_VALID(opts, btf_dedup_opts))
3306 return libbpf_err(-EINVAL);
3307
3308 d = btf_dedup_new(btf, opts);
3309 if (IS_ERR(d)) {
3310 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
3311 return libbpf_err(-EINVAL);
3312 }
3313
3314 if (btf_ensure_modifiable(btf)) {
3315 err = -ENOMEM;
3316 goto done;
3317 }
3318
3319 err = btf_dedup_prep(d);
3320 if (err) {
3321 pr_debug("btf_dedup_prep failed:%d\n", err);
3322 goto done;
3323 }
3324 err = btf_dedup_strings(d);
3325 if (err < 0) {
3326 pr_debug("btf_dedup_strings failed:%d\n", err);
3327 goto done;
3328 }
3329 err = btf_dedup_prim_types(d);
3330 if (err < 0) {
3331 pr_debug("btf_dedup_prim_types failed:%d\n", err);
3332 goto done;
3333 }
3334 err = btf_dedup_struct_types(d);
3335 if (err < 0) {
3336 pr_debug("btf_dedup_struct_types failed:%d\n", err);
3337 goto done;
3338 }
3339 err = btf_dedup_resolve_fwds(d);
3340 if (err < 0) {
3341 pr_debug("btf_dedup_resolve_fwds failed:%d\n", err);
3342 goto done;
3343 }
3344 err = btf_dedup_ref_types(d);
3345 if (err < 0) {
3346 pr_debug("btf_dedup_ref_types failed:%d\n", err);
3347 goto done;
3348 }
3349 err = btf_dedup_compact_types(d);
3350 if (err < 0) {
3351 pr_debug("btf_dedup_compact_types failed:%d\n", err);
3352 goto done;
3353 }
3354 err = btf_dedup_remap_types(d);
3355 if (err < 0) {
3356 pr_debug("btf_dedup_remap_types failed:%d\n", err);
3357 goto done;
3358 }
3359
3360 done:
3361 btf_dedup_free(d);
3362 return libbpf_err(err);
3363 }
3364
3365 #define BTF_UNPROCESSED_ID ((__u32)-1)
3366 #define BTF_IN_PROGRESS_ID ((__u32)-2)
3367
3368 struct btf_dedup {
3369 /* .BTF section to be deduped in-place */
3370 struct btf *btf;
3371 /*
3372 * Optional .BTF.ext section. When provided, any strings referenced
3373 * from it will be taken into account when deduping strings
3374 */
3375 struct btf_ext *btf_ext;
3376 /*
3377 * This is a map from any type's signature hash to a list of possible
3378 * canonical representative type candidates. Hash collisions are
3379 * ignored, so even types of various kinds can share same list of
3380 * candidates, which is fine because we rely on subsequent
3381 * btf_xxx_equal() checks to authoritatively verify type equality.
3382 */
3383 struct hashmap *dedup_table;
3384 /* Canonical types map */
3385 __u32 *map;
3386 /* Hypothetical mapping, used during type graph equivalence checks */
3387 __u32 *hypot_map;
3388 __u32 *hypot_list;
3389 size_t hypot_cnt;
3390 size_t hypot_cap;
3391 /* Whether hypothetical mapping, if successful, would need to adjust
3392 * already canonicalized types (due to a new forward declaration to
3393 * concrete type resolution). In such case, during split BTF dedup
3394 * candidate type would still be considered as different, because base
3395 * BTF is considered to be immutable.
3396 */
3397 bool hypot_adjust_canon;
3398 /* Various option modifying behavior of algorithm */
3399 struct btf_dedup_opts opts;
3400 /* temporary strings deduplication state */
3401 struct strset *strs_set;
3402 };
3403
3404 static unsigned long hash_combine(unsigned long h, unsigned long value)
3405 {
3406 return h * 31 + value;
3407 }
3408
3409 #define for_each_dedup_cand(d, node, hash) \
3410 hashmap__for_each_key_entry(d->dedup_table, node, hash)
3411
3412 static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
3413 {
3414 return hashmap__append(d->dedup_table, hash, type_id);
3415 }
3416
3417 static int btf_dedup_hypot_map_add(struct btf_dedup *d,
3418 __u32 from_id, __u32 to_id)
3419 {
3420 if (d->hypot_cnt == d->hypot_cap) {
3421 __u32 *new_list;
3422
3423 d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
3424 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
3425 if (!new_list)
3426 return -ENOMEM;
3427 d->hypot_list = new_list;
3428 }
3429 d->hypot_list[d->hypot_cnt++] = from_id;
3430 d->hypot_map[from_id] = to_id;
3431 return 0;
3432 }
3433
3434 static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
3435 {
3436 int i;
3437
3438 for (i = 0; i < d->hypot_cnt; i++)
3439 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
3440 d->hypot_cnt = 0;
3441 d->hypot_adjust_canon = false;
3442 }
3443
3444 static void btf_dedup_free(struct btf_dedup *d)
3445 {
3446 hashmap__free(d->dedup_table);
3447 d->dedup_table = NULL;
3448
3449 free(d->map);
3450 d->map = NULL;
3451
3452 free(d->hypot_map);
3453 d->hypot_map = NULL;
3454
3455 free(d->hypot_list);
3456 d->hypot_list = NULL;
3457
3458 free(d);
3459 }
3460
3461 static size_t btf_dedup_identity_hash_fn(long key, void *ctx)
3462 {
3463 return key;
3464 }
3465
3466 static size_t btf_dedup_collision_hash_fn(long key, void *ctx)
3467 {
3468 return 0;
3469 }
3470
3471 static bool btf_dedup_equal_fn(long k1, long k2, void *ctx)
3472 {
3473 return k1 == k2;
3474 }
3475
3476 static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts)
3477 {
3478 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
3479 hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
3480 int i, err = 0, type_cnt;
3481
3482 if (!d)
3483 return ERR_PTR(-ENOMEM);
3484
3485 if (OPTS_GET(opts, force_collisions, false))
3486 hash_fn = btf_dedup_collision_hash_fn;
3487
3488 d->btf = btf;
3489 d->btf_ext = OPTS_GET(opts, btf_ext, NULL);
3490
3491 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
3492 if (IS_ERR(d->dedup_table)) {
3493 err = PTR_ERR(d->dedup_table);
3494 d->dedup_table = NULL;
3495 goto done;
3496 }
3497
3498 type_cnt = btf__type_cnt(btf);
3499 d->map = malloc(sizeof(__u32) * type_cnt);
3500 if (!d->map) {
3501 err = -ENOMEM;
3502 goto done;
3503 }
3504 /* special BTF "void" type is made canonical immediately */
3505 d->map[0] = 0;
3506 for (i = 1; i < type_cnt; i++) {
3507 struct btf_type *t = btf_type_by_id(d->btf, i);
3508
3509 /* VAR and DATASEC are never deduped and are self-canonical */
3510 if (btf_is_var(t) || btf_is_datasec(t))
3511 d->map[i] = i;
3512 else
3513 d->map[i] = BTF_UNPROCESSED_ID;
3514 }
3515
3516 d->hypot_map = malloc(sizeof(__u32) * type_cnt);
3517 if (!d->hypot_map) {
3518 err = -ENOMEM;
3519 goto done;
3520 }
3521 for (i = 0; i < type_cnt; i++)
3522 d->hypot_map[i] = BTF_UNPROCESSED_ID;
3523
3524 done:
3525 if (err) {
3526 btf_dedup_free(d);
3527 return ERR_PTR(err);
3528 }
3529
3530 return d;
3531 }
3532
3533 /*
3534 * Iterate over all possible places in .BTF and .BTF.ext that can reference
3535 * string and pass pointer to it to a provided callback `fn`.
3536 */
3537 static int btf_for_each_str_off(struct btf_dedup *d, str_off_visit_fn fn, void *ctx)
3538 {
3539 int i, r;
3540
3541 for (i = 0; i < d->btf->nr_types; i++) {
3542 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i);
3543
3544 r = btf_type_visit_str_offs(t, fn, ctx);
3545 if (r)
3546 return r;
3547 }
3548
3549 if (!d->btf_ext)
3550 return 0;
3551
3552 r = btf_ext_visit_str_offs(d->btf_ext, fn, ctx);
3553 if (r)
3554 return r;
3555
3556 return 0;
3557 }
3558
3559 static int strs_dedup_remap_str_off(__u32 *str_off_ptr, void *ctx)
3560 {
3561 struct btf_dedup *d = ctx;
3562 __u32 str_off = *str_off_ptr;
3563 const char *s;
3564 int off, err;
3565
3566 /* don't touch empty string or string in main BTF */
3567 if (str_off == 0 || str_off < d->btf->start_str_off)
3568 return 0;
3569
3570 s = btf__str_by_offset(d->btf, str_off);
3571 if (d->btf->base_btf) {
3572 err = btf__find_str(d->btf->base_btf, s);
3573 if (err >= 0) {
3574 *str_off_ptr = err;
3575 return 0;
3576 }
3577 if (err != -ENOENT)
3578 return err;
3579 }
3580
3581 off = strset__add_str(d->strs_set, s);
3582 if (off < 0)
3583 return off;
3584
3585 *str_off_ptr = d->btf->start_str_off + off;
3586 return 0;
3587 }
3588
3589 /*
3590 * Dedup string and filter out those that are not referenced from either .BTF
3591 * or .BTF.ext (if provided) sections.
3592 *
3593 * This is done by building index of all strings in BTF's string section,
3594 * then iterating over all entities that can reference strings (e.g., type
3595 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
3596 * strings as used. After that all used strings are deduped and compacted into
3597 * sequential blob of memory and new offsets are calculated. Then all the string
3598 * references are iterated again and rewritten using new offsets.
3599 */
3600 static int btf_dedup_strings(struct btf_dedup *d)
3601 {
3602 int err;
3603
3604 if (d->btf->strs_deduped)
3605 return 0;
3606
3607 d->strs_set = strset__new(BTF_MAX_STR_OFFSET, NULL, 0);
3608 if (IS_ERR(d->strs_set)) {
3609 err = PTR_ERR(d->strs_set);
3610 goto err_out;
3611 }
3612
3613 if (!d->btf->base_btf) {
3614 /* insert empty string; we won't be looking it up during strings
3615 * dedup, but it's good to have it for generic BTF string lookups
3616 */
3617 err = strset__add_str(d->strs_set, "");
3618 if (err < 0)
3619 goto err_out;
3620 }
3621
3622 /* remap string offsets */
3623 err = btf_for_each_str_off(d, strs_dedup_remap_str_off, d);
3624 if (err)
3625 goto err_out;
3626
3627 /* replace BTF string data and hash with deduped ones */
3628 strset__free(d->btf->strs_set);
3629 d->btf->hdr->str_len = strset__data_size(d->strs_set);
3630 d->btf->strs_set = d->strs_set;
3631 d->strs_set = NULL;
3632 d->btf->strs_deduped = true;
3633 return 0;
3634
3635 err_out:
3636 strset__free(d->strs_set);
3637 d->strs_set = NULL;
3638
3639 return err;
3640 }
3641
3642 static long btf_hash_common(struct btf_type *t)
3643 {
3644 long h;
3645
3646 h = hash_combine(0, t->name_off);
3647 h = hash_combine(h, t->info);
3648 h = hash_combine(h, t->size);
3649 return h;
3650 }
3651
3652 static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
3653 {
3654 return t1->name_off == t2->name_off &&
3655 t1->info == t2->info &&
3656 t1->size == t2->size;
3657 }
3658
3659 /* Calculate type signature hash of INT or TAG. */
3660 static long btf_hash_int_decl_tag(struct btf_type *t)
3661 {
3662 __u32 info = *(__u32 *)(t + 1);
3663 long h;
3664
3665 h = btf_hash_common(t);
3666 h = hash_combine(h, info);
3667 return h;
3668 }
3669
3670 /* Check structural equality of two INTs or TAGs. */
3671 static bool btf_equal_int_tag(struct btf_type *t1, struct btf_type *t2)
3672 {
3673 __u32 info1, info2;
3674
3675 if (!btf_equal_common(t1, t2))
3676 return false;
3677 info1 = *(__u32 *)(t1 + 1);
3678 info2 = *(__u32 *)(t2 + 1);
3679 return info1 == info2;
3680 }
3681
3682 /* Calculate type signature hash of ENUM/ENUM64. */
3683 static long btf_hash_enum(struct btf_type *t)
3684 {
3685 long h;
3686
3687 /* don't hash vlen, enum members and size to support enum fwd resolving */
3688 h = hash_combine(0, t->name_off);
3689 return h;
3690 }
3691
3692 static bool btf_equal_enum_members(struct btf_type *t1, struct btf_type *t2)
3693 {
3694 const struct btf_enum *m1, *m2;
3695 __u16 vlen;
3696 int i;
3697
3698 vlen = btf_vlen(t1);
3699 m1 = btf_enum(t1);
3700 m2 = btf_enum(t2);
3701 for (i = 0; i < vlen; i++) {
3702 if (m1->name_off != m2->name_off || m1->val != m2->val)
3703 return false;
3704 m1++;
3705 m2++;
3706 }
3707 return true;
3708 }
3709
3710 static bool btf_equal_enum64_members(struct btf_type *t1, struct btf_type *t2)
3711 {
3712 const struct btf_enum64 *m1, *m2;
3713 __u16 vlen;
3714 int i;
3715
3716 vlen = btf_vlen(t1);
3717 m1 = btf_enum64(t1);
3718 m2 = btf_enum64(t2);
3719 for (i = 0; i < vlen; i++) {
3720 if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 ||
3721 m1->val_hi32 != m2->val_hi32)
3722 return false;
3723 m1++;
3724 m2++;
3725 }
3726 return true;
3727 }
3728
3729 /* Check structural equality of two ENUMs or ENUM64s. */
3730 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
3731 {
3732 if (!btf_equal_common(t1, t2))
3733 return false;
3734
3735 /* t1 & t2 kinds are identical because of btf_equal_common */
3736 if (btf_kind(t1) == BTF_KIND_ENUM)
3737 return btf_equal_enum_members(t1, t2);
3738 else
3739 return btf_equal_enum64_members(t1, t2);
3740 }
3741
3742 static inline bool btf_is_enum_fwd(struct btf_type *t)
3743 {
3744 return btf_is_any_enum(t) && btf_vlen(t) == 0;
3745 }
3746
3747 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3748 {
3749 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3750 return btf_equal_enum(t1, t2);
3751 /* At this point either t1 or t2 or both are forward declarations, thus:
3752 * - skip comparing vlen because it is zero for forward declarations;
3753 * - skip comparing size to allow enum forward declarations
3754 * to be compatible with enum64 full declarations;
3755 * - skip comparing kind for the same reason.
3756 */
3757 return t1->name_off == t2->name_off &&
3758 btf_is_any_enum(t1) && btf_is_any_enum(t2);
3759 }
3760
3761 /*
3762 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
3763 * as referenced type IDs equivalence is established separately during type
3764 * graph equivalence check algorithm.
3765 */
3766 static long btf_hash_struct(struct btf_type *t)
3767 {
3768 const struct btf_member *member = btf_members(t);
3769 __u32 vlen = btf_vlen(t);
3770 long h = btf_hash_common(t);
3771 int i;
3772
3773 for (i = 0; i < vlen; i++) {
3774 h = hash_combine(h, member->name_off);
3775 h = hash_combine(h, member->offset);
3776 /* no hashing of referenced type ID, it can be unresolved yet */
3777 member++;
3778 }
3779 return h;
3780 }
3781
3782 /*
3783 * Check structural compatibility of two STRUCTs/UNIONs, ignoring referenced
3784 * type IDs. This check is performed during type graph equivalence check and
3785 * referenced types equivalence is checked separately.
3786 */
3787 static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
3788 {
3789 const struct btf_member *m1, *m2;
3790 __u16 vlen;
3791 int i;
3792
3793 if (!btf_equal_common(t1, t2))
3794 return false;
3795
3796 vlen = btf_vlen(t1);
3797 m1 = btf_members(t1);
3798 m2 = btf_members(t2);
3799 for (i = 0; i < vlen; i++) {
3800 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
3801 return false;
3802 m1++;
3803 m2++;
3804 }
3805 return true;
3806 }
3807
3808 /*
3809 * Calculate type signature hash of ARRAY, including referenced type IDs,
3810 * under assumption that they were already resolved to canonical type IDs and
3811 * are not going to change.
3812 */
3813 static long btf_hash_array(struct btf_type *t)
3814 {
3815 const struct btf_array *info = btf_array(t);
3816 long h = btf_hash_common(t);
3817
3818 h = hash_combine(h, info->type);
3819 h = hash_combine(h, info->index_type);
3820 h = hash_combine(h, info->nelems);
3821 return h;
3822 }
3823
3824 /*
3825 * Check exact equality of two ARRAYs, taking into account referenced
3826 * type IDs, under assumption that they were already resolved to canonical
3827 * type IDs and are not going to change.
3828 * This function is called during reference types deduplication to compare
3829 * ARRAY to potential canonical representative.
3830 */
3831 static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
3832 {
3833 const struct btf_array *info1, *info2;
3834
3835 if (!btf_equal_common(t1, t2))
3836 return false;
3837
3838 info1 = btf_array(t1);
3839 info2 = btf_array(t2);
3840 return info1->type == info2->type &&
3841 info1->index_type == info2->index_type &&
3842 info1->nelems == info2->nelems;
3843 }
3844
3845 /*
3846 * Check structural compatibility of two ARRAYs, ignoring referenced type
3847 * IDs. This check is performed during type graph equivalence check and
3848 * referenced types equivalence is checked separately.
3849 */
3850 static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
3851 {
3852 if (!btf_equal_common(t1, t2))
3853 return false;
3854
3855 return btf_array(t1)->nelems == btf_array(t2)->nelems;
3856 }
3857
3858 /*
3859 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
3860 * under assumption that they were already resolved to canonical type IDs and
3861 * are not going to change.
3862 */
3863 static long btf_hash_fnproto(struct btf_type *t)
3864 {
3865 const struct btf_param *member = btf_params(t);
3866 __u16 vlen = btf_vlen(t);
3867 long h = btf_hash_common(t);
3868 int i;
3869
3870 for (i = 0; i < vlen; i++) {
3871 h = hash_combine(h, member->name_off);
3872 h = hash_combine(h, member->type);
3873 member++;
3874 }
3875 return h;
3876 }
3877
3878 /*
3879 * Check exact equality of two FUNC_PROTOs, taking into account referenced
3880 * type IDs, under assumption that they were already resolved to canonical
3881 * type IDs and are not going to change.
3882 * This function is called during reference types deduplication to compare
3883 * FUNC_PROTO to potential canonical representative.
3884 */
3885 static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
3886 {
3887 const struct btf_param *m1, *m2;
3888 __u16 vlen;
3889 int i;
3890
3891 if (!btf_equal_common(t1, t2))
3892 return false;
3893
3894 vlen = btf_vlen(t1);
3895 m1 = btf_params(t1);
3896 m2 = btf_params(t2);
3897 for (i = 0; i < vlen; i++) {
3898 if (m1->name_off != m2->name_off || m1->type != m2->type)
3899 return false;
3900 m1++;
3901 m2++;
3902 }
3903 return true;
3904 }
3905
3906 /*
3907 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
3908 * IDs. This check is performed during type graph equivalence check and
3909 * referenced types equivalence is checked separately.
3910 */
3911 static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
3912 {
3913 const struct btf_param *m1, *m2;
3914 __u16 vlen;
3915 int i;
3916
3917 /* skip return type ID */
3918 if (t1->name_off != t2->name_off || t1->info != t2->info)
3919 return false;
3920
3921 vlen = btf_vlen(t1);
3922 m1 = btf_params(t1);
3923 m2 = btf_params(t2);
3924 for (i = 0; i < vlen; i++) {
3925 if (m1->name_off != m2->name_off)
3926 return false;
3927 m1++;
3928 m2++;
3929 }
3930 return true;
3931 }
3932
3933 /* Prepare split BTF for deduplication by calculating hashes of base BTF's
3934 * types and initializing the rest of the state (canonical type mapping) for
3935 * the fixed base BTF part.
3936 */
3937 static int btf_dedup_prep(struct btf_dedup *d)
3938 {
3939 struct btf_type *t;
3940 int type_id;
3941 long h;
3942
3943 if (!d->btf->base_btf)
3944 return 0;
3945
3946 for (type_id = 1; type_id < d->btf->start_id; type_id++) {
3947 t = btf_type_by_id(d->btf, type_id);
3948
3949 /* all base BTF types are self-canonical by definition */
3950 d->map[type_id] = type_id;
3951
3952 switch (btf_kind(t)) {
3953 case BTF_KIND_VAR:
3954 case BTF_KIND_DATASEC:
3955 /* VAR and DATASEC are never hash/deduplicated */
3956 continue;
3957 case BTF_KIND_CONST:
3958 case BTF_KIND_VOLATILE:
3959 case BTF_KIND_RESTRICT:
3960 case BTF_KIND_PTR:
3961 case BTF_KIND_FWD:
3962 case BTF_KIND_TYPEDEF:
3963 case BTF_KIND_FUNC:
3964 case BTF_KIND_FLOAT:
3965 case BTF_KIND_TYPE_TAG:
3966 h = btf_hash_common(t);
3967 break;
3968 case BTF_KIND_INT:
3969 case BTF_KIND_DECL_TAG:
3970 h = btf_hash_int_decl_tag(t);
3971 break;
3972 case BTF_KIND_ENUM:
3973 case BTF_KIND_ENUM64:
3974 h = btf_hash_enum(t);
3975 break;
3976 case BTF_KIND_STRUCT:
3977 case BTF_KIND_UNION:
3978 h = btf_hash_struct(t);
3979 break;
3980 case BTF_KIND_ARRAY:
3981 h = btf_hash_array(t);
3982 break;
3983 case BTF_KIND_FUNC_PROTO:
3984 h = btf_hash_fnproto(t);
3985 break;
3986 default:
3987 pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id);
3988 return -EINVAL;
3989 }
3990 if (btf_dedup_table_add(d, h, type_id))
3991 return -ENOMEM;
3992 }
3993
3994 return 0;
3995 }
3996
3997 /*
3998 * Deduplicate primitive types, that can't reference other types, by calculating
3999 * their type signature hash and comparing them with any possible canonical
4000 * candidate. If no canonical candidate matches, type itself is marked as
4001 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
4002 */
4003 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
4004 {
4005 struct btf_type *t = btf_type_by_id(d->btf, type_id);
4006 struct hashmap_entry *hash_entry;
4007 struct btf_type *cand;
4008 /* if we don't find equivalent type, then we are canonical */
4009 __u32 new_id = type_id;
4010 __u32 cand_id;
4011 long h;
4012
4013 switch (btf_kind(t)) {
4014 case BTF_KIND_CONST:
4015 case BTF_KIND_VOLATILE:
4016 case BTF_KIND_RESTRICT:
4017 case BTF_KIND_PTR:
4018 case BTF_KIND_TYPEDEF:
4019 case BTF_KIND_ARRAY:
4020 case BTF_KIND_STRUCT:
4021 case BTF_KIND_UNION:
4022 case BTF_KIND_FUNC:
4023 case BTF_KIND_FUNC_PROTO:
4024 case BTF_KIND_VAR:
4025 case BTF_KIND_DATASEC:
4026 case BTF_KIND_DECL_TAG:
4027 case BTF_KIND_TYPE_TAG:
4028 return 0;
4029
4030 case BTF_KIND_INT:
4031 h = btf_hash_int_decl_tag(t);
4032 for_each_dedup_cand(d, hash_entry, h) {
4033 cand_id = hash_entry->value;
4034 cand = btf_type_by_id(d->btf, cand_id);
4035 if (btf_equal_int_tag(t, cand)) {
4036 new_id = cand_id;
4037 break;
4038 }
4039 }
4040 break;
4041
4042 case BTF_KIND_ENUM:
4043 case BTF_KIND_ENUM64:
4044 h = btf_hash_enum(t);
4045 for_each_dedup_cand(d, hash_entry, h) {
4046 cand_id = hash_entry->value;
4047 cand = btf_type_by_id(d->btf, cand_id);
4048 if (btf_equal_enum(t, cand)) {
4049 new_id = cand_id;
4050 break;
4051 }
4052 if (btf_compat_enum(t, cand)) {
4053 if (btf_is_enum_fwd(t)) {
4054 /* resolve fwd to full enum */
4055 new_id = cand_id;
4056 break;
4057 }
4058 /* resolve canonical enum fwd to full enum */
4059 d->map[cand_id] = type_id;
4060 }
4061 }
4062 break;
4063
4064 case BTF_KIND_FWD:
4065 case BTF_KIND_FLOAT:
4066 h = btf_hash_common(t);
4067 for_each_dedup_cand(d, hash_entry, h) {
4068 cand_id = hash_entry->value;
4069 cand = btf_type_by_id(d->btf, cand_id);
4070 if (btf_equal_common(t, cand)) {
4071 new_id = cand_id;
4072 break;
4073 }
4074 }
4075 break;
4076
4077 default:
4078 return -EINVAL;
4079 }
4080
4081 d->map[type_id] = new_id;
4082 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4083 return -ENOMEM;
4084
4085 return 0;
4086 }
4087
4088 static int btf_dedup_prim_types(struct btf_dedup *d)
4089 {
4090 int i, err;
4091
4092 for (i = 0; i < d->btf->nr_types; i++) {
4093 err = btf_dedup_prim_type(d, d->btf->start_id + i);
4094 if (err)
4095 return err;
4096 }
4097 return 0;
4098 }
4099
4100 /*
4101 * Check whether type is already mapped into canonical one (could be to itself).
4102 */
4103 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
4104 {
4105 return d->map[type_id] <= BTF_MAX_NR_TYPES;
4106 }
4107
4108 /*
4109 * Resolve type ID into its canonical type ID, if any; otherwise return original
4110 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
4111 * STRUCT/UNION link and resolve it into canonical type ID as well.
4112 */
4113 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
4114 {
4115 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
4116 type_id = d->map[type_id];
4117 return type_id;
4118 }
4119
4120 /*
4121 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
4122 * type ID.
4123 */
4124 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
4125 {
4126 __u32 orig_type_id = type_id;
4127
4128 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
4129 return type_id;
4130
4131 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
4132 type_id = d->map[type_id];
4133
4134 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
4135 return type_id;
4136
4137 return orig_type_id;
4138 }
4139
4140
4141 static inline __u16 btf_fwd_kind(struct btf_type *t)
4142 {
4143 return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
4144 }
4145
4146 /* Check if given two types are identical ARRAY definitions */
4147 static bool btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
4148 {
4149 struct btf_type *t1, *t2;
4150
4151 t1 = btf_type_by_id(d->btf, id1);
4152 t2 = btf_type_by_id(d->btf, id2);
4153 if (!btf_is_array(t1) || !btf_is_array(t2))
4154 return false;
4155
4156 return btf_equal_array(t1, t2);
4157 }
4158
4159 /* Check if given two types are identical STRUCT/UNION definitions */
4160 static bool btf_dedup_identical_structs(struct btf_dedup *d, __u32 id1, __u32 id2)
4161 {
4162 const struct btf_member *m1, *m2;
4163 struct btf_type *t1, *t2;
4164 int n, i;
4165
4166 t1 = btf_type_by_id(d->btf, id1);
4167 t2 = btf_type_by_id(d->btf, id2);
4168
4169 if (!btf_is_composite(t1) || btf_kind(t1) != btf_kind(t2))
4170 return false;
4171
4172 if (!btf_shallow_equal_struct(t1, t2))
4173 return false;
4174
4175 m1 = btf_members(t1);
4176 m2 = btf_members(t2);
4177 for (i = 0, n = btf_vlen(t1); i < n; i++, m1++, m2++) {
4178 if (m1->type != m2->type &&
4179 !btf_dedup_identical_arrays(d, m1->type, m2->type) &&
4180 !btf_dedup_identical_structs(d, m1->type, m2->type))
4181 return false;
4182 }
4183 return true;
4184 }
4185
4186 /*
4187 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
4188 * call it "candidate graph" in this description for brevity) to a type graph
4189 * formed by (potential) canonical struct/union ("canonical graph" for brevity
4190 * here, though keep in mind that not all types in canonical graph are
4191 * necessarily canonical representatives themselves, some of them might be
4192 * duplicates or its uniqueness might not have been established yet).
4193 * Returns:
4194 * - >0, if type graphs are equivalent;
4195 * - 0, if not equivalent;
4196 * - <0, on error.
4197 *
4198 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
4199 * equivalence of BTF types at each step. If at any point BTF types in candidate
4200 * and canonical graphs are not compatible structurally, whole graphs are
4201 * incompatible. If types are structurally equivalent (i.e., all information
4202 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
4203 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
4204 * If a type references other types, then those referenced types are checked
4205 * for equivalence recursively.
4206 *
4207 * During DFS traversal, if we find that for current `canon_id` type we
4208 * already have some mapping in hypothetical map, we check for two possible
4209 * situations:
4210 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
4211 * happen when type graphs have cycles. In this case we assume those two
4212 * types are equivalent.
4213 * - `canon_id` is mapped to different type. This is contradiction in our
4214 * hypothetical mapping, because same graph in canonical graph corresponds
4215 * to two different types in candidate graph, which for equivalent type
4216 * graphs shouldn't happen. This condition terminates equivalence check
4217 * with negative result.
4218 *
4219 * If type graphs traversal exhausts types to check and find no contradiction,
4220 * then type graphs are equivalent.
4221 *
4222 * When checking types for equivalence, there is one special case: FWD types.
4223 * If FWD type resolution is allowed and one of the types (either from canonical
4224 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
4225 * flag) and their names match, hypothetical mapping is updated to point from
4226 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
4227 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
4228 *
4229 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
4230 * if there are two exactly named (or anonymous) structs/unions that are
4231 * compatible structurally, one of which has FWD field, while other is concrete
4232 * STRUCT/UNION, but according to C sources they are different structs/unions
4233 * that are referencing different types with the same name. This is extremely
4234 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
4235 * this logic is causing problems.
4236 *
4237 * Doing FWD resolution means that both candidate and/or canonical graphs can
4238 * consists of portions of the graph that come from multiple compilation units.
4239 * This is due to the fact that types within single compilation unit are always
4240 * deduplicated and FWDs are already resolved, if referenced struct/union
4241 * definiton is available. So, if we had unresolved FWD and found corresponding
4242 * STRUCT/UNION, they will be from different compilation units. This
4243 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
4244 * type graph will likely have at least two different BTF types that describe
4245 * same type (e.g., most probably there will be two different BTF types for the
4246 * same 'int' primitive type) and could even have "overlapping" parts of type
4247 * graph that describe same subset of types.
4248 *
4249 * This in turn means that our assumption that each type in canonical graph
4250 * must correspond to exactly one type in candidate graph might not hold
4251 * anymore and will make it harder to detect contradictions using hypothetical
4252 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
4253 * resolution only in canonical graph. FWDs in candidate graphs are never
4254 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
4255 * that can occur:
4256 * - Both types in canonical and candidate graphs are FWDs. If they are
4257 * structurally equivalent, then they can either be both resolved to the
4258 * same STRUCT/UNION or not resolved at all. In both cases they are
4259 * equivalent and there is no need to resolve FWD on candidate side.
4260 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
4261 * so nothing to resolve as well, algorithm will check equivalence anyway.
4262 * - Type in canonical graph is FWD, while type in candidate is concrete
4263 * STRUCT/UNION. In this case candidate graph comes from single compilation
4264 * unit, so there is exactly one BTF type for each unique C type. After
4265 * resolving FWD into STRUCT/UNION, there might be more than one BTF type
4266 * in canonical graph mapping to single BTF type in candidate graph, but
4267 * because hypothetical mapping maps from canonical to candidate types, it's
4268 * alright, and we still maintain the property of having single `canon_id`
4269 * mapping to single `cand_id` (there could be two different `canon_id`
4270 * mapped to the same `cand_id`, but it's not contradictory).
4271 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
4272 * graph is FWD. In this case we are just going to check compatibility of
4273 * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
4274 * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
4275 * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
4276 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
4277 * canonical graph.
4278 */
4279 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
4280 __u32 canon_id)
4281 {
4282 struct btf_type *cand_type;
4283 struct btf_type *canon_type;
4284 __u32 hypot_type_id;
4285 __u16 cand_kind;
4286 __u16 canon_kind;
4287 int i, eq;
4288
4289 /* if both resolve to the same canonical, they must be equivalent */
4290 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
4291 return 1;
4292
4293 canon_id = resolve_fwd_id(d, canon_id);
4294
4295 hypot_type_id = d->hypot_map[canon_id];
4296 if (hypot_type_id <= BTF_MAX_NR_TYPES) {
4297 if (hypot_type_id == cand_id)
4298 return 1;
4299 /* In some cases compiler will generate different DWARF types
4300 * for *identical* array type definitions and use them for
4301 * different fields within the *same* struct. This breaks type
4302 * equivalence check, which makes an assumption that candidate
4303 * types sub-graph has a consistent and deduped-by-compiler
4304 * types within a single CU. So work around that by explicitly
4305 * allowing identical array types here.
4306 */
4307 if (btf_dedup_identical_arrays(d, hypot_type_id, cand_id))
4308 return 1;
4309 /* It turns out that similar situation can happen with
4310 * struct/union sometimes, sigh... Handle the case where
4311 * structs/unions are exactly the same, down to the referenced
4312 * type IDs. Anything more complicated (e.g., if referenced
4313 * types are different, but equivalent) is *way more*
4314 * complicated and requires a many-to-many equivalence mapping.
4315 */
4316 if (btf_dedup_identical_structs(d, hypot_type_id, cand_id))
4317 return 1;
4318 return 0;
4319 }
4320
4321 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
4322 return -ENOMEM;
4323
4324 cand_type = btf_type_by_id(d->btf, cand_id);
4325 canon_type = btf_type_by_id(d->btf, canon_id);
4326 cand_kind = btf_kind(cand_type);
4327 canon_kind = btf_kind(canon_type);
4328
4329 if (cand_type->name_off != canon_type->name_off)
4330 return 0;
4331
4332 /* FWD <--> STRUCT/UNION equivalence check, if enabled */
4333 if ((cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
4334 && cand_kind != canon_kind) {
4335 __u16 real_kind;
4336 __u16 fwd_kind;
4337
4338 if (cand_kind == BTF_KIND_FWD) {
4339 real_kind = canon_kind;
4340 fwd_kind = btf_fwd_kind(cand_type);
4341 } else {
4342 real_kind = cand_kind;
4343 fwd_kind = btf_fwd_kind(canon_type);
4344 /* we'd need to resolve base FWD to STRUCT/UNION */
4345 if (fwd_kind == real_kind && canon_id < d->btf->start_id)
4346 d->hypot_adjust_canon = true;
4347 }
4348 return fwd_kind == real_kind;
4349 }
4350
4351 if (cand_kind != canon_kind)
4352 return 0;
4353
4354 switch (cand_kind) {
4355 case BTF_KIND_INT:
4356 return btf_equal_int_tag(cand_type, canon_type);
4357
4358 case BTF_KIND_ENUM:
4359 case BTF_KIND_ENUM64:
4360 return btf_compat_enum(cand_type, canon_type);
4361
4362 case BTF_KIND_FWD:
4363 case BTF_KIND_FLOAT:
4364 return btf_equal_common(cand_type, canon_type);
4365
4366 case BTF_KIND_CONST:
4367 case BTF_KIND_VOLATILE:
4368 case BTF_KIND_RESTRICT:
4369 case BTF_KIND_PTR:
4370 case BTF_KIND_TYPEDEF:
4371 case BTF_KIND_FUNC:
4372 case BTF_KIND_TYPE_TAG:
4373 if (cand_type->info != canon_type->info)
4374 return 0;
4375 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4376
4377 case BTF_KIND_ARRAY: {
4378 const struct btf_array *cand_arr, *canon_arr;
4379
4380 if (!btf_compat_array(cand_type, canon_type))
4381 return 0;
4382 cand_arr = btf_array(cand_type);
4383 canon_arr = btf_array(canon_type);
4384 eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type);
4385 if (eq <= 0)
4386 return eq;
4387 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
4388 }
4389
4390 case BTF_KIND_STRUCT:
4391 case BTF_KIND_UNION: {
4392 const struct btf_member *cand_m, *canon_m;
4393 __u16 vlen;
4394
4395 if (!btf_shallow_equal_struct(cand_type, canon_type))
4396 return 0;
4397 vlen = btf_vlen(cand_type);
4398 cand_m = btf_members(cand_type);
4399 canon_m = btf_members(canon_type);
4400 for (i = 0; i < vlen; i++) {
4401 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
4402 if (eq <= 0)
4403 return eq;
4404 cand_m++;
4405 canon_m++;
4406 }
4407
4408 return 1;
4409 }
4410
4411 case BTF_KIND_FUNC_PROTO: {
4412 const struct btf_param *cand_p, *canon_p;
4413 __u16 vlen;
4414
4415 if (!btf_compat_fnproto(cand_type, canon_type))
4416 return 0;
4417 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4418 if (eq <= 0)
4419 return eq;
4420 vlen = btf_vlen(cand_type);
4421 cand_p = btf_params(cand_type);
4422 canon_p = btf_params(canon_type);
4423 for (i = 0; i < vlen; i++) {
4424 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
4425 if (eq <= 0)
4426 return eq;
4427 cand_p++;
4428 canon_p++;
4429 }
4430 return 1;
4431 }
4432
4433 default:
4434 return -EINVAL;
4435 }
4436 return 0;
4437 }
4438
4439 /*
4440 * Use hypothetical mapping, produced by successful type graph equivalence
4441 * check, to augment existing struct/union canonical mapping, where possible.
4442 *
4443 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
4444 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
4445 * it doesn't matter if FWD type was part of canonical graph or candidate one,
4446 * we are recording the mapping anyway. As opposed to carefulness required
4447 * for struct/union correspondence mapping (described below), for FWD resolution
4448 * it's not important, as by the time that FWD type (reference type) will be
4449 * deduplicated all structs/unions will be deduped already anyway.
4450 *
4451 * Recording STRUCT/UNION mapping is purely a performance optimization and is
4452 * not required for correctness. It needs to be done carefully to ensure that
4453 * struct/union from candidate's type graph is not mapped into corresponding
4454 * struct/union from canonical type graph that itself hasn't been resolved into
4455 * canonical representative. The only guarantee we have is that canonical
4456 * struct/union was determined as canonical and that won't change. But any
4457 * types referenced through that struct/union fields could have been not yet
4458 * resolved, so in case like that it's too early to establish any kind of
4459 * correspondence between structs/unions.
4460 *
4461 * No canonical correspondence is derived for primitive types (they are already
4462 * deduplicated completely already anyway) or reference types (they rely on
4463 * stability of struct/union canonical relationship for equivalence checks).
4464 */
4465 static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
4466 {
4467 __u32 canon_type_id, targ_type_id;
4468 __u16 t_kind, c_kind;
4469 __u32 t_id, c_id;
4470 int i;
4471
4472 for (i = 0; i < d->hypot_cnt; i++) {
4473 canon_type_id = d->hypot_list[i];
4474 targ_type_id = d->hypot_map[canon_type_id];
4475 t_id = resolve_type_id(d, targ_type_id);
4476 c_id = resolve_type_id(d, canon_type_id);
4477 t_kind = btf_kind(btf__type_by_id(d->btf, t_id));
4478 c_kind = btf_kind(btf__type_by_id(d->btf, c_id));
4479 /*
4480 * Resolve FWD into STRUCT/UNION.
4481 * It's ok to resolve FWD into STRUCT/UNION that's not yet
4482 * mapped to canonical representative (as opposed to
4483 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
4484 * eventually that struct is going to be mapped and all resolved
4485 * FWDs will automatically resolve to correct canonical
4486 * representative. This will happen before ref type deduping,
4487 * which critically depends on stability of these mapping. This
4488 * stability is not a requirement for STRUCT/UNION equivalence
4489 * checks, though.
4490 */
4491
4492 /* if it's the split BTF case, we still need to point base FWD
4493 * to STRUCT/UNION in a split BTF, because FWDs from split BTF
4494 * will be resolved against base FWD. If we don't point base
4495 * canonical FWD to the resolved STRUCT/UNION, then all the
4496 * FWDs in split BTF won't be correctly resolved to a proper
4497 * STRUCT/UNION.
4498 */
4499 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
4500 d->map[c_id] = t_id;
4501
4502 /* if graph equivalence determined that we'd need to adjust
4503 * base canonical types, then we need to only point base FWDs
4504 * to STRUCTs/UNIONs and do no more modifications. For all
4505 * other purposes the type graphs were not equivalent.
4506 */
4507 if (d->hypot_adjust_canon)
4508 continue;
4509
4510 if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
4511 d->map[t_id] = c_id;
4512
4513 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
4514 c_kind != BTF_KIND_FWD &&
4515 is_type_mapped(d, c_id) &&
4516 !is_type_mapped(d, t_id)) {
4517 /*
4518 * as a perf optimization, we can map struct/union
4519 * that's part of type graph we just verified for
4520 * equivalence. We can do that for struct/union that has
4521 * canonical representative only, though.
4522 */
4523 d->map[t_id] = c_id;
4524 }
4525 }
4526 }
4527
4528 /*
4529 * Deduplicate struct/union types.
4530 *
4531 * For each struct/union type its type signature hash is calculated, taking
4532 * into account type's name, size, number, order and names of fields, but
4533 * ignoring type ID's referenced from fields, because they might not be deduped
4534 * completely until after reference types deduplication phase. This type hash
4535 * is used to iterate over all potential canonical types, sharing same hash.
4536 * For each canonical candidate we check whether type graphs that they form
4537 * (through referenced types in fields and so on) are equivalent using algorithm
4538 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
4539 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
4540 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
4541 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
4542 * potentially map other structs/unions to their canonical representatives,
4543 * if such relationship hasn't yet been established. This speeds up algorithm
4544 * by eliminating some of the duplicate work.
4545 *
4546 * If no matching canonical representative was found, struct/union is marked
4547 * as canonical for itself and is added into btf_dedup->dedup_table hash map
4548 * for further look ups.
4549 */
4550 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
4551 {
4552 struct btf_type *cand_type, *t;
4553 struct hashmap_entry *hash_entry;
4554 /* if we don't find equivalent type, then we are canonical */
4555 __u32 new_id = type_id;
4556 __u16 kind;
4557 long h;
4558
4559 /* already deduped or is in process of deduping (loop detected) */
4560 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4561 return 0;
4562
4563 t = btf_type_by_id(d->btf, type_id);
4564 kind = btf_kind(t);
4565
4566 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4567 return 0;
4568
4569 h = btf_hash_struct(t);
4570 for_each_dedup_cand(d, hash_entry, h) {
4571 __u32 cand_id = hash_entry->value;
4572 int eq;
4573
4574 /*
4575 * Even though btf_dedup_is_equiv() checks for
4576 * btf_shallow_equal_struct() internally when checking two
4577 * structs (unions) for equivalence, we need to guard here
4578 * from picking matching FWD type as a dedup candidate.
4579 * This can happen due to hash collision. In such case just
4580 * relying on btf_dedup_is_equiv() would lead to potentially
4581 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
4582 * FWD and compatible STRUCT/UNION are considered equivalent.
4583 */
4584 cand_type = btf_type_by_id(d->btf, cand_id);
4585 if (!btf_shallow_equal_struct(t, cand_type))
4586 continue;
4587
4588 btf_dedup_clear_hypot_map(d);
4589 eq = btf_dedup_is_equiv(d, type_id, cand_id);
4590 if (eq < 0)
4591 return eq;
4592 if (!eq)
4593 continue;
4594 btf_dedup_merge_hypot_map(d);
4595 if (d->hypot_adjust_canon) /* not really equivalent */
4596 continue;
4597 new_id = cand_id;
4598 break;
4599 }
4600
4601 d->map[type_id] = new_id;
4602 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4603 return -ENOMEM;
4604
4605 return 0;
4606 }
4607
4608 static int btf_dedup_struct_types(struct btf_dedup *d)
4609 {
4610 int i, err;
4611
4612 for (i = 0; i < d->btf->nr_types; i++) {
4613 err = btf_dedup_struct_type(d, d->btf->start_id + i);
4614 if (err)
4615 return err;
4616 }
4617 return 0;
4618 }
4619
4620 /*
4621 * Deduplicate reference type.
4622 *
4623 * Once all primitive and struct/union types got deduplicated, we can easily
4624 * deduplicate all other (reference) BTF types. This is done in two steps:
4625 *
4626 * 1. Resolve all referenced type IDs into their canonical type IDs. This
4627 * resolution can be done either immediately for primitive or struct/union types
4628 * (because they were deduped in previous two phases) or recursively for
4629 * reference types. Recursion will always terminate at either primitive or
4630 * struct/union type, at which point we can "unwind" chain of reference types
4631 * one by one. There is no danger of encountering cycles because in C type
4632 * system the only way to form type cycle is through struct/union, so any chain
4633 * of reference types, even those taking part in a type cycle, will inevitably
4634 * reach struct/union at some point.
4635 *
4636 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
4637 * becomes "stable", in the sense that no further deduplication will cause
4638 * any changes to it. With that, it's now possible to calculate type's signature
4639 * hash (this time taking into account referenced type IDs) and loop over all
4640 * potential canonical representatives. If no match was found, current type
4641 * will become canonical representative of itself and will be added into
4642 * btf_dedup->dedup_table as another possible canonical representative.
4643 */
4644 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
4645 {
4646 struct hashmap_entry *hash_entry;
4647 __u32 new_id = type_id, cand_id;
4648 struct btf_type *t, *cand;
4649 /* if we don't find equivalent type, then we are representative type */
4650 int ref_type_id;
4651 long h;
4652
4653 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
4654 return -ELOOP;
4655 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4656 return resolve_type_id(d, type_id);
4657
4658 t = btf_type_by_id(d->btf, type_id);
4659 d->map[type_id] = BTF_IN_PROGRESS_ID;
4660
4661 switch (btf_kind(t)) {
4662 case BTF_KIND_CONST:
4663 case BTF_KIND_VOLATILE:
4664 case BTF_KIND_RESTRICT:
4665 case BTF_KIND_PTR:
4666 case BTF_KIND_TYPEDEF:
4667 case BTF_KIND_FUNC:
4668 case BTF_KIND_TYPE_TAG:
4669 ref_type_id = btf_dedup_ref_type(d, t->type);
4670 if (ref_type_id < 0)
4671 return ref_type_id;
4672 t->type = ref_type_id;
4673
4674 h = btf_hash_common(t);
4675 for_each_dedup_cand(d, hash_entry, h) {
4676 cand_id = hash_entry->value;
4677 cand = btf_type_by_id(d->btf, cand_id);
4678 if (btf_equal_common(t, cand)) {
4679 new_id = cand_id;
4680 break;
4681 }
4682 }
4683 break;
4684
4685 case BTF_KIND_DECL_TAG:
4686 ref_type_id = btf_dedup_ref_type(d, t->type);
4687 if (ref_type_id < 0)
4688 return ref_type_id;
4689 t->type = ref_type_id;
4690
4691 h = btf_hash_int_decl_tag(t);
4692 for_each_dedup_cand(d, hash_entry, h) {
4693 cand_id = hash_entry->value;
4694 cand = btf_type_by_id(d->btf, cand_id);
4695 if (btf_equal_int_tag(t, cand)) {
4696 new_id = cand_id;
4697 break;
4698 }
4699 }
4700 break;
4701
4702 case BTF_KIND_ARRAY: {
4703 struct btf_array *info = btf_array(t);
4704
4705 ref_type_id = btf_dedup_ref_type(d, info->type);
4706 if (ref_type_id < 0)
4707 return ref_type_id;
4708 info->type = ref_type_id;
4709
4710 ref_type_id = btf_dedup_ref_type(d, info->index_type);
4711 if (ref_type_id < 0)
4712 return ref_type_id;
4713 info->index_type = ref_type_id;
4714
4715 h = btf_hash_array(t);
4716 for_each_dedup_cand(d, hash_entry, h) {
4717 cand_id = hash_entry->value;
4718 cand = btf_type_by_id(d->btf, cand_id);
4719 if (btf_equal_array(t, cand)) {
4720 new_id = cand_id;
4721 break;
4722 }
4723 }
4724 break;
4725 }
4726
4727 case BTF_KIND_FUNC_PROTO: {
4728 struct btf_param *param;
4729 __u16 vlen;
4730 int i;
4731
4732 ref_type_id = btf_dedup_ref_type(d, t->type);
4733 if (ref_type_id < 0)
4734 return ref_type_id;
4735 t->type = ref_type_id;
4736
4737 vlen = btf_vlen(t);
4738 param = btf_params(t);
4739 for (i = 0; i < vlen; i++) {
4740 ref_type_id = btf_dedup_ref_type(d, param->type);
4741 if (ref_type_id < 0)
4742 return ref_type_id;
4743 param->type = ref_type_id;
4744 param++;
4745 }
4746
4747 h = btf_hash_fnproto(t);
4748 for_each_dedup_cand(d, hash_entry, h) {
4749 cand_id = hash_entry->value;
4750 cand = btf_type_by_id(d->btf, cand_id);
4751 if (btf_equal_fnproto(t, cand)) {
4752 new_id = cand_id;
4753 break;
4754 }
4755 }
4756 break;
4757 }
4758
4759 default:
4760 return -EINVAL;
4761 }
4762
4763 d->map[type_id] = new_id;
4764 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4765 return -ENOMEM;
4766
4767 return new_id;
4768 }
4769
4770 static int btf_dedup_ref_types(struct btf_dedup *d)
4771 {
4772 int i, err;
4773
4774 for (i = 0; i < d->btf->nr_types; i++) {
4775 err = btf_dedup_ref_type(d, d->btf->start_id + i);
4776 if (err < 0)
4777 return err;
4778 }
4779 /* we won't need d->dedup_table anymore */
4780 hashmap__free(d->dedup_table);
4781 d->dedup_table = NULL;
4782 return 0;
4783 }
4784
4785 /*
4786 * Collect a map from type names to type ids for all canonical structs
4787 * and unions. If the same name is shared by several canonical types
4788 * use a special value 0 to indicate this fact.
4789 */
4790 static int btf_dedup_fill_unique_names_map(struct btf_dedup *d, struct hashmap *names_map)
4791 {
4792 __u32 nr_types = btf__type_cnt(d->btf);
4793 struct btf_type *t;
4794 __u32 type_id;
4795 __u16 kind;
4796 int err;
4797
4798 /*
4799 * Iterate over base and split module ids in order to get all
4800 * available structs in the map.
4801 */
4802 for (type_id = 1; type_id < nr_types; ++type_id) {
4803 t = btf_type_by_id(d->btf, type_id);
4804 kind = btf_kind(t);
4805
4806 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4807 continue;
4808
4809 /* Skip non-canonical types */
4810 if (type_id != d->map[type_id])
4811 continue;
4812
4813 err = hashmap__add(names_map, t->name_off, type_id);
4814 if (err == -EEXIST)
4815 err = hashmap__set(names_map, t->name_off, 0, NULL, NULL);
4816
4817 if (err)
4818 return err;
4819 }
4820
4821 return 0;
4822 }
4823
4824 static int btf_dedup_resolve_fwd(struct btf_dedup *d, struct hashmap *names_map, __u32 type_id)
4825 {
4826 struct btf_type *t = btf_type_by_id(d->btf, type_id);
4827 enum btf_fwd_kind fwd_kind = btf_kflag(t);
4828 __u16 cand_kind, kind = btf_kind(t);
4829 struct btf_type *cand_t;
4830 uintptr_t cand_id;
4831
4832 if (kind != BTF_KIND_FWD)
4833 return 0;
4834
4835 /* Skip if this FWD already has a mapping */
4836 if (type_id != d->map[type_id])
4837 return 0;
4838
4839 if (!hashmap__find(names_map, t->name_off, &cand_id))
4840 return 0;
4841
4842 /* Zero is a special value indicating that name is not unique */
4843 if (!cand_id)
4844 return 0;
4845
4846 cand_t = btf_type_by_id(d->btf, cand_id);
4847 cand_kind = btf_kind(cand_t);
4848 if ((cand_kind == BTF_KIND_STRUCT && fwd_kind != BTF_FWD_STRUCT) ||
4849 (cand_kind == BTF_KIND_UNION && fwd_kind != BTF_FWD_UNION))
4850 return 0;
4851
4852 d->map[type_id] = cand_id;
4853
4854 return 0;
4855 }
4856
4857 /*
4858 * Resolve unambiguous forward declarations.
4859 *
4860 * The lion's share of all FWD declarations is resolved during
4861 * `btf_dedup_struct_types` phase when different type graphs are
4862 * compared against each other. However, if in some compilation unit a
4863 * FWD declaration is not a part of a type graph compared against
4864 * another type graph that declaration's canonical type would not be
4865 * changed. Example:
4866 *
4867 * CU #1:
4868 *
4869 * struct foo;
4870 * struct foo *some_global;
4871 *
4872 * CU #2:
4873 *
4874 * struct foo { int u; };
4875 * struct foo *another_global;
4876 *
4877 * After `btf_dedup_struct_types` the BTF looks as follows:
4878 *
4879 * [1] STRUCT 'foo' size=4 vlen=1 ...
4880 * [2] INT 'int' size=4 ...
4881 * [3] PTR '(anon)' type_id=1
4882 * [4] FWD 'foo' fwd_kind=struct
4883 * [5] PTR '(anon)' type_id=4
4884 *
4885 * This pass assumes that such FWD declarations should be mapped to
4886 * structs or unions with identical name in case if the name is not
4887 * ambiguous.
4888 */
4889 static int btf_dedup_resolve_fwds(struct btf_dedup *d)
4890 {
4891 int i, err;
4892 struct hashmap *names_map;
4893
4894 names_map = hashmap__new(btf_dedup_identity_hash_fn, btf_dedup_equal_fn, NULL);
4895 if (IS_ERR(names_map))
4896 return PTR_ERR(names_map);
4897
4898 err = btf_dedup_fill_unique_names_map(d, names_map);
4899 if (err < 0)
4900 goto exit;
4901
4902 for (i = 0; i < d->btf->nr_types; i++) {
4903 err = btf_dedup_resolve_fwd(d, names_map, d->btf->start_id + i);
4904 if (err < 0)
4905 break;
4906 }
4907
4908 exit:
4909 hashmap__free(names_map);
4910 return err;
4911 }
4912
4913 /*
4914 * Compact types.
4915 *
4916 * After we established for each type its corresponding canonical representative
4917 * type, we now can eliminate types that are not canonical and leave only
4918 * canonical ones layed out sequentially in memory by copying them over
4919 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
4920 * a map from original type ID to a new compacted type ID, which will be used
4921 * during next phase to "fix up" type IDs, referenced from struct/union and
4922 * reference types.
4923 */
4924 static int btf_dedup_compact_types(struct btf_dedup *d)
4925 {
4926 __u32 *new_offs;
4927 __u32 next_type_id = d->btf->start_id;
4928 const struct btf_type *t;
4929 void *p;
4930 int i, id, len;
4931
4932 /* we are going to reuse hypot_map to store compaction remapping */
4933 d->hypot_map[0] = 0;
4934 /* base BTF types are not renumbered */
4935 for (id = 1; id < d->btf->start_id; id++)
4936 d->hypot_map[id] = id;
4937 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++)
4938 d->hypot_map[id] = BTF_UNPROCESSED_ID;
4939
4940 p = d->btf->types_data;
4941
4942 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) {
4943 if (d->map[id] != id)
4944 continue;
4945
4946 t = btf__type_by_id(d->btf, id);
4947 len = btf_type_size(t);
4948 if (len < 0)
4949 return len;
4950
4951 memmove(p, t, len);
4952 d->hypot_map[id] = next_type_id;
4953 d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data;
4954 p += len;
4955 next_type_id++;
4956 }
4957
4958 /* shrink struct btf's internal types index and update btf_header */
4959 d->btf->nr_types = next_type_id - d->btf->start_id;
4960 d->btf->type_offs_cap = d->btf->nr_types;
4961 d->btf->hdr->type_len = p - d->btf->types_data;
4962 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
4963 sizeof(*new_offs));
4964 if (d->btf->type_offs_cap && !new_offs)
4965 return -ENOMEM;
4966 d->btf->type_offs = new_offs;
4967 d->btf->hdr->str_off = d->btf->hdr->type_len;
4968 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
4969 return 0;
4970 }
4971
4972 /*
4973 * Figure out final (deduplicated and compacted) type ID for provided original
4974 * `type_id` by first resolving it into corresponding canonical type ID and
4975 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
4976 * which is populated during compaction phase.
4977 */
4978 static int btf_dedup_remap_type_id(__u32 *type_id, void *ctx)
4979 {
4980 struct btf_dedup *d = ctx;
4981 __u32 resolved_type_id, new_type_id;
4982
4983 resolved_type_id = resolve_type_id(d, *type_id);
4984 new_type_id = d->hypot_map[resolved_type_id];
4985 if (new_type_id > BTF_MAX_NR_TYPES)
4986 return -EINVAL;
4987
4988 *type_id = new_type_id;
4989 return 0;
4990 }
4991
4992 /*
4993 * Remap referenced type IDs into deduped type IDs.
4994 *
4995 * After BTF types are deduplicated and compacted, their final type IDs may
4996 * differ from original ones. The map from original to a corresponding
4997 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
4998 * compaction phase. During remapping phase we are rewriting all type IDs
4999 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
5000 * their final deduped type IDs.
5001 */
5002 static int btf_dedup_remap_types(struct btf_dedup *d)
5003 {
5004 int i, r;
5005
5006 for (i = 0; i < d->btf->nr_types; i++) {
5007 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i);
5008
5009 r = btf_type_visit_type_ids(t, btf_dedup_remap_type_id, d);
5010 if (r)
5011 return r;
5012 }
5013
5014 if (!d->btf_ext)
5015 return 0;
5016
5017 r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d);
5018 if (r)
5019 return r;
5020
5021 return 0;
5022 }
5023
5024 /*
5025 * Probe few well-known locations for vmlinux kernel image and try to load BTF
5026 * data out of it to use for target BTF.
5027 */
5028 struct btf *btf__load_vmlinux_btf(void)
5029 {
5030 const char *locations[] = {
5031 /* try canonical vmlinux BTF through sysfs first */
5032 "/sys/kernel/btf/vmlinux",
5033 /* fall back to trying to find vmlinux on disk otherwise */
5034 "/boot/vmlinux-%1$s",
5035 "/lib/modules/%1$s/vmlinux-%1$s",
5036 "/lib/modules/%1$s/build/vmlinux",
5037 "/usr/lib/modules/%1$s/kernel/vmlinux",
5038 "/usr/lib/debug/boot/vmlinux-%1$s",
5039 "/usr/lib/debug/boot/vmlinux-%1$s.debug",
5040 "/usr/lib/debug/lib/modules/%1$s/vmlinux",
5041 };
5042 char path[PATH_MAX + 1];
5043 struct utsname buf;
5044 struct btf *btf;
5045 int i, err;
5046
5047 uname(&buf);
5048
5049 for (i = 0; i < ARRAY_SIZE(locations); i++) {
5050 snprintf(path, PATH_MAX, locations[i], buf.release);
5051
5052 if (faccessat(AT_FDCWD, path, R_OK, AT_EACCESS))
5053 continue;
5054
5055 btf = btf__parse(path, NULL);
5056 err = libbpf_get_error(btf);
5057 pr_debug("loading kernel BTF '%s': %d\n", path, err);
5058 if (err)
5059 continue;
5060
5061 return btf;
5062 }
5063
5064 pr_warn("failed to find valid kernel BTF\n");
5065 return libbpf_err_ptr(-ESRCH);
5066 }
5067
5068 struct btf *libbpf_find_kernel_btf(void) __attribute__((alias("btf__load_vmlinux_btf")));
5069
5070 struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf)
5071 {
5072 char path[80];
5073
5074 snprintf(path, sizeof(path), "/sys/kernel/btf/%s", module_name);
5075 return btf__parse_split(path, vmlinux_btf);
5076 }
5077
5078 int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx)
5079 {
5080 int i, n, err;
5081
5082 switch (btf_kind(t)) {
5083 case BTF_KIND_INT:
5084 case BTF_KIND_FLOAT:
5085 case BTF_KIND_ENUM:
5086 case BTF_KIND_ENUM64:
5087 return 0;
5088
5089 case BTF_KIND_FWD:
5090 case BTF_KIND_CONST:
5091 case BTF_KIND_VOLATILE:
5092 case BTF_KIND_RESTRICT:
5093 case BTF_KIND_PTR:
5094 case BTF_KIND_TYPEDEF:
5095 case BTF_KIND_FUNC:
5096 case BTF_KIND_VAR:
5097 case BTF_KIND_DECL_TAG:
5098 case BTF_KIND_TYPE_TAG:
5099 return visit(&t->type, ctx);
5100
5101 case BTF_KIND_ARRAY: {
5102 struct btf_array *a = btf_array(t);
5103
5104 err = visit(&a->type, ctx);
5105 err = err ?: visit(&a->index_type, ctx);
5106 return err;
5107 }
5108
5109 case BTF_KIND_STRUCT:
5110 case BTF_KIND_UNION: {
5111 struct btf_member *m = btf_members(t);
5112
5113 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5114 err = visit(&m->type, ctx);
5115 if (err)
5116 return err;
5117 }
5118 return 0;
5119 }
5120
5121 case BTF_KIND_FUNC_PROTO: {
5122 struct btf_param *m = btf_params(t);
5123
5124 err = visit(&t->type, ctx);
5125 if (err)
5126 return err;
5127 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5128 err = visit(&m->type, ctx);
5129 if (err)
5130 return err;
5131 }
5132 return 0;
5133 }
5134
5135 case BTF_KIND_DATASEC: {
5136 struct btf_var_secinfo *m = btf_var_secinfos(t);
5137
5138 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5139 err = visit(&m->type, ctx);
5140 if (err)
5141 return err;
5142 }
5143 return 0;
5144 }
5145
5146 default:
5147 return -EINVAL;
5148 }
5149 }
5150
5151 int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ctx)
5152 {
5153 int i, n, err;
5154
5155 err = visit(&t->name_off, ctx);
5156 if (err)
5157 return err;
5158
5159 switch (btf_kind(t)) {
5160 case BTF_KIND_STRUCT:
5161 case BTF_KIND_UNION: {
5162 struct btf_member *m = btf_members(t);
5163
5164 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5165 err = visit(&m->name_off, ctx);
5166 if (err)
5167 return err;
5168 }
5169 break;
5170 }
5171 case BTF_KIND_ENUM: {
5172 struct btf_enum *m = btf_enum(t);
5173
5174 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5175 err = visit(&m->name_off, ctx);
5176 if (err)
5177 return err;
5178 }
5179 break;
5180 }
5181 case BTF_KIND_ENUM64: {
5182 struct btf_enum64 *m = btf_enum64(t);
5183
5184 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5185 err = visit(&m->name_off, ctx);
5186 if (err)
5187 return err;
5188 }
5189 break;
5190 }
5191 case BTF_KIND_FUNC_PROTO: {
5192 struct btf_param *m = btf_params(t);
5193
5194 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
5195 err = visit(&m->name_off, ctx);
5196 if (err)
5197 return err;
5198 }
5199 break;
5200 }
5201 default:
5202 break;
5203 }
5204
5205 return 0;
5206 }
5207
5208 int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx)
5209 {
5210 const struct btf_ext_info *seg;
5211 struct btf_ext_info_sec *sec;
5212 int i, err;
5213
5214 seg = &btf_ext->func_info;
5215 for_each_btf_ext_sec(seg, sec) {
5216 struct bpf_func_info_min *rec;
5217
5218 for_each_btf_ext_rec(seg, sec, i, rec) {
5219 err = visit(&rec->type_id, ctx);
5220 if (err < 0)
5221 return err;
5222 }
5223 }
5224
5225 seg = &btf_ext->core_relo_info;
5226 for_each_btf_ext_sec(seg, sec) {
5227 struct bpf_core_relo *rec;
5228
5229 for_each_btf_ext_rec(seg, sec, i, rec) {
5230 err = visit(&rec->type_id, ctx);
5231 if (err < 0)
5232 return err;
5233 }
5234 }
5235
5236 return 0;
5237 }
5238
5239 int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx)
5240 {
5241 const struct btf_ext_info *seg;
5242 struct btf_ext_info_sec *sec;
5243 int i, err;
5244
5245 seg = &btf_ext->func_info;
5246 for_each_btf_ext_sec(seg, sec) {
5247 err = visit(&sec->sec_name_off, ctx);
5248 if (err)
5249 return err;
5250 }
5251
5252 seg = &btf_ext->line_info;
5253 for_each_btf_ext_sec(seg, sec) {
5254 struct bpf_line_info_min *rec;
5255
5256 err = visit(&sec->sec_name_off, ctx);
5257 if (err)
5258 return err;
5259
5260 for_each_btf_ext_rec(seg, sec, i, rec) {
5261 err = visit(&rec->file_name_off, ctx);
5262 if (err)
5263 return err;
5264 err = visit(&rec->line_off, ctx);
5265 if (err)
5266 return err;
5267 }
5268 }
5269
5270 seg = &btf_ext->core_relo_info;
5271 for_each_btf_ext_sec(seg, sec) {
5272 struct bpf_core_relo *rec;
5273
5274 err = visit(&sec->sec_name_off, ctx);
5275 if (err)
5276 return err;
5277
5278 for_each_btf_ext_rec(seg, sec, i, rec) {
5279 err = visit(&rec->access_str_off, ctx);
5280 if (err)
5281 return err;
5282 }
5283 }
5284
5285 return 0;
5286 }
5287