1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3 /*
4 * resolve_btfids scans ELF object for .BTF_ids section and resolves
5 * its symbols with BTF ID values.
6 *
7 * Each symbol points to 4 bytes data and is expected to have
8 * following name syntax:
9 *
10 * __BTF_ID__<type>__<symbol>[__<id>]
11 *
12 * type is:
13 *
14 * func - lookup BTF_KIND_FUNC symbol with <symbol> name
15 * and store its ID into the data:
16 *
17 * __BTF_ID__func__vfs_close__1:
18 * .zero 4
19 *
20 * struct - lookup BTF_KIND_STRUCT symbol with <symbol> name
21 * and store its ID into the data:
22 *
23 * __BTF_ID__struct__sk_buff__1:
24 * .zero 4
25 *
26 * union - lookup BTF_KIND_UNION symbol with <symbol> name
27 * and store its ID into the data:
28 *
29 * __BTF_ID__union__thread_union__1:
30 * .zero 4
31 *
32 * typedef - lookup BTF_KIND_TYPEDEF symbol with <symbol> name
33 * and store its ID into the data:
34 *
35 * __BTF_ID__typedef__pid_t__1:
36 * .zero 4
37 *
38 * set - store symbol size into first 4 bytes and sort following
39 * ID list
40 *
41 * __BTF_ID__set__list:
42 * .zero 4
43 * list:
44 * __BTF_ID__func__vfs_getattr__3:
45 * .zero 4
46 * __BTF_ID__func__vfs_fallocate__4:
47 * .zero 4
48 *
49 * set8 - store symbol size into first 4 bytes and sort following
50 * ID list
51 *
52 * __BTF_ID__set8__list:
53 * .zero 8
54 * list:
55 * __BTF_ID__func__vfs_getattr__3:
56 * .zero 4
57 * .word (1 << 0) | (1 << 2)
58 * __BTF_ID__func__vfs_fallocate__5:
59 * .zero 4
60 * .word (1 << 3) | (1 << 1) | (1 << 2)
61 */
62
63 #define _GNU_SOURCE
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <libelf.h>
69 #include <gelf.h>
70 #include <sys/stat.h>
71 #include <fcntl.h>
72 #include <errno.h>
73 #include <linux/btf_ids.h>
74 #include <linux/kallsyms.h>
75 #include <linux/rbtree.h>
76 #include <linux/zalloc.h>
77 #include <linux/err.h>
78 #include <linux/limits.h>
79 #include <bpf/btf.h>
80 #include <bpf/libbpf.h>
81 #include <subcmd/parse-options.h>
82
83 #define BTF_IDS_SECTION ".BTF_ids"
84 #define BTF_ID_PREFIX "__BTF_ID__"
85
86 #define BTF_STRUCT "struct"
87 #define BTF_UNION "union"
88 #define BTF_TYPEDEF "typedef"
89 #define BTF_FUNC "func"
90 #define BTF_SET "set"
91 #define BTF_SET8 "set8"
92
93 #define ADDR_CNT 100
94
95 #if __BYTE_ORDER == __LITTLE_ENDIAN
96 # define ELFDATANATIVE ELFDATA2LSB
97 #elif __BYTE_ORDER == __BIG_ENDIAN
98 # define ELFDATANATIVE ELFDATA2MSB
99 #else
100 # error "Unknown machine endianness!"
101 #endif
102
103 enum btf_id_kind {
104 BTF_ID_KIND_NONE,
105 BTF_ID_KIND_SYM,
106 BTF_ID_KIND_SET,
107 BTF_ID_KIND_SET8
108 };
109
110 struct btf_id {
111 struct rb_node rb_node;
112 char *name;
113 union {
114 int id;
115 int cnt;
116 };
117 enum btf_id_kind kind;
118 int addr_cnt;
119 Elf64_Addr addr[ADDR_CNT];
120 };
121
122 struct object {
123 const char *path;
124 const char *btf_path;
125 const char *base_btf_path;
126
127 struct btf *btf;
128 struct btf *base_btf;
129 bool distill_base;
130
131 struct {
132 int fd;
133 Elf *elf;
134 Elf_Data *symbols;
135 Elf_Data *idlist;
136 int symbols_shndx;
137 int idlist_shndx;
138 size_t strtabidx;
139 unsigned long idlist_addr;
140 int encoding;
141 } efile;
142
143 struct rb_root sets;
144 struct rb_root structs;
145 struct rb_root unions;
146 struct rb_root typedefs;
147 struct rb_root funcs;
148
149 int nr_funcs;
150 int nr_structs;
151 int nr_unions;
152 int nr_typedefs;
153 };
154
155 #define KF_IMPLICIT_ARGS (1 << 16)
156 #define KF_IMPL_SUFFIX "_impl"
157
158 struct kfunc {
159 const char *name;
160 u32 btf_id;
161 u32 flags;
162 };
163
164 struct btf2btf_context {
165 struct btf *btf;
166 u32 *decl_tags;
167 u32 nr_decl_tags;
168 u32 max_decl_tags;
169 struct kfunc *kfuncs;
170 u32 nr_kfuncs;
171 u32 max_kfuncs;
172 };
173
174 static int verbose;
175 static int warnings;
176
eprintf(int level,int var,const char * fmt,...)177 static int eprintf(int level, int var, const char *fmt, ...)
178 {
179 va_list args;
180 int ret = 0;
181
182 if (var >= level) {
183 va_start(args, fmt);
184 ret = vfprintf(stderr, fmt, args);
185 va_end(args);
186 }
187 return ret;
188 }
189
190 #ifndef pr_fmt
191 #define pr_fmt(fmt) fmt
192 #endif
193
194 #define pr_debug(fmt, ...) \
195 eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
196 #define pr_debugN(n, fmt, ...) \
197 eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
198 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
199 #define pr_err(fmt, ...) \
200 eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
201 #define pr_info(fmt, ...) \
202 eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
203
is_btf_id(const char * name)204 static bool is_btf_id(const char *name)
205 {
206 return name && !strncmp(name, BTF_ID_PREFIX, sizeof(BTF_ID_PREFIX) - 1);
207 }
208
btf_id__find(struct rb_root * root,const char * name)209 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
210 {
211 struct rb_node *p = root->rb_node;
212 struct btf_id *id;
213 int cmp;
214
215 while (p) {
216 id = rb_entry(p, struct btf_id, rb_node);
217 cmp = strcmp(id->name, name);
218 if (cmp < 0)
219 p = p->rb_left;
220 else if (cmp > 0)
221 p = p->rb_right;
222 else
223 return id;
224 }
225 return NULL;
226 }
227
__btf_id__add(struct rb_root * root,const char * name,enum btf_id_kind kind,bool unique)228 static struct btf_id *__btf_id__add(struct rb_root *root,
229 const char *name,
230 enum btf_id_kind kind,
231 bool unique)
232 {
233 struct rb_node **p = &root->rb_node;
234 struct rb_node *parent = NULL;
235 struct btf_id *id;
236 int cmp;
237
238 while (*p != NULL) {
239 parent = *p;
240 id = rb_entry(parent, struct btf_id, rb_node);
241 cmp = strcmp(id->name, name);
242 if (cmp < 0)
243 p = &(*p)->rb_left;
244 else if (cmp > 0)
245 p = &(*p)->rb_right;
246 else
247 return unique ? NULL : id;
248 }
249
250 id = zalloc(sizeof(*id));
251 if (id) {
252 pr_debug("adding symbol %s\n", name);
253 id->name = strdup(name);
254 if (!id->name) {
255 free(id);
256 return NULL;
257 }
258 id->kind = kind;
259 rb_link_node(&id->rb_node, parent, p);
260 rb_insert_color(&id->rb_node, root);
261 }
262 return id;
263 }
264
btf_id__add(struct rb_root * root,const char * name,enum btf_id_kind kind)265 static inline struct btf_id *btf_id__add(struct rb_root *root,
266 const char *name,
267 enum btf_id_kind kind)
268 {
269 return __btf_id__add(root, name, kind, false);
270 }
271
btf_id__add_unique(struct rb_root * root,const char * name,enum btf_id_kind kind)272 static inline struct btf_id *btf_id__add_unique(struct rb_root *root,
273 const char *name,
274 enum btf_id_kind kind)
275 {
276 return __btf_id__add(root, name, kind, true);
277 }
278
get_id(const char * prefix_end,char * buf,size_t buf_sz)279 static int get_id(const char *prefix_end, char *buf, size_t buf_sz)
280 {
281 /*
282 * __BTF_ID__func__vfs_truncate__0
283 * prefix_end = ^
284 * pos = ^
285 */
286 int len = strlen(prefix_end);
287 int pos = sizeof("__") - 1;
288 char *p;
289
290 if (pos >= len)
291 return -1;
292
293 if (len - pos >= buf_sz)
294 return -1;
295
296 strcpy(buf, prefix_end + pos);
297 /*
298 * __BTF_ID__func__vfs_truncate__0
299 * buf = ^
300 *
301 * cut the unique id part
302 */
303 p = strrchr(buf, '_');
304 p--;
305 if (*p != '_')
306 return -1;
307 *p = '\0';
308
309 return 0;
310 }
311
add_set(struct object * obj,char * name,enum btf_id_kind kind)312 static struct btf_id *add_set(struct object *obj, char *name, enum btf_id_kind kind)
313 {
314 int len = strlen(name);
315 int prefixlen;
316 char *id;
317
318 /*
319 * __BTF_ID__set__name
320 * name = ^
321 * id = ^
322 */
323 switch (kind) {
324 case BTF_ID_KIND_SET:
325 prefixlen = sizeof(BTF_SET "__") - 1;
326 break;
327 case BTF_ID_KIND_SET8:
328 prefixlen = sizeof(BTF_SET8 "__") - 1;
329 break;
330 default:
331 pr_err("Unexpected kind %d passed to %s() for symbol %s\n", kind, __func__, name);
332 return NULL;
333 }
334
335 id = name + prefixlen;
336 if (id >= name + len) {
337 pr_err("FAILED to parse set name: %s\n", name);
338 return NULL;
339 }
340
341 return btf_id__add_unique(&obj->sets, id, kind);
342 }
343
add_symbol(struct rb_root * root,char * name,size_t size)344 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
345 {
346 char id[KSYM_NAME_LEN];
347
348 if (get_id(name + size, id, sizeof(id))) {
349 pr_err("FAILED to parse symbol name: %s\n", name);
350 return NULL;
351 }
352
353 return btf_id__add(root, id, BTF_ID_KIND_SYM);
354 }
355
btf_id__free_all(struct rb_root * root)356 static void btf_id__free_all(struct rb_root *root)
357 {
358 struct rb_node *next;
359 struct btf_id *id;
360
361 next = rb_first(root);
362 while (next) {
363 id = rb_entry(next, struct btf_id, rb_node);
364 next = rb_next(&id->rb_node);
365 rb_erase(&id->rb_node, root);
366 free(id->name);
367 free(id);
368 }
369 }
370
bswap_32_data(void * data,u32 nr_bytes)371 static void bswap_32_data(void *data, u32 nr_bytes)
372 {
373 u32 cnt, i;
374 u32 *ptr;
375
376 cnt = nr_bytes / sizeof(u32);
377 ptr = data;
378
379 for (i = 0; i < cnt; i++)
380 ptr[i] = bswap_32(ptr[i]);
381 }
382
elf_collect(struct object * obj)383 static int elf_collect(struct object *obj)
384 {
385 Elf_Scn *scn = NULL;
386 size_t shdrstrndx;
387 GElf_Ehdr ehdr;
388 int idx = 0;
389 Elf *elf;
390 int fd;
391
392 fd = open(obj->path, O_RDWR, 0666);
393 if (fd == -1) {
394 pr_err("FAILED cannot open %s: %s\n",
395 obj->path, strerror(errno));
396 return -1;
397 }
398
399 elf_version(EV_CURRENT);
400
401 elf = elf_begin(fd, ELF_C_READ_MMAP_PRIVATE, NULL);
402 if (!elf) {
403 close(fd);
404 pr_err("FAILED cannot create ELF descriptor: %s\n",
405 elf_errmsg(-1));
406 return -1;
407 }
408
409 obj->efile.fd = fd;
410 obj->efile.elf = elf;
411
412 elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
413
414 if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
415 pr_err("FAILED cannot get shdr str ndx\n");
416 return -1;
417 }
418
419 if (gelf_getehdr(obj->efile.elf, &ehdr) == NULL) {
420 pr_err("FAILED cannot get ELF header: %s\n",
421 elf_errmsg(-1));
422 return -1;
423 }
424 obj->efile.encoding = ehdr.e_ident[EI_DATA];
425
426 /*
427 * Scan all the elf sections and look for save data
428 * from .BTF_ids section and symbols.
429 */
430 while ((scn = elf_nextscn(elf, scn)) != NULL) {
431 Elf_Data *data;
432 GElf_Shdr sh;
433 char *name;
434
435 idx++;
436 if (gelf_getshdr(scn, &sh) != &sh) {
437 pr_err("FAILED get section(%d) header\n", idx);
438 return -1;
439 }
440
441 name = elf_strptr(elf, shdrstrndx, sh.sh_name);
442 if (!name) {
443 pr_err("FAILED get section(%d) name\n", idx);
444 return -1;
445 }
446
447 data = elf_getdata(scn, 0);
448 if (!data) {
449 pr_err("FAILED to get section(%d) data from %s\n",
450 idx, name);
451 return -1;
452 }
453
454 pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
455 idx, name, (unsigned long) data->d_size,
456 (int) sh.sh_link, (unsigned long) sh.sh_flags,
457 (int) sh.sh_type);
458
459 if (sh.sh_type == SHT_SYMTAB) {
460 obj->efile.symbols = data;
461 obj->efile.symbols_shndx = idx;
462 obj->efile.strtabidx = sh.sh_link;
463 } else if (!strcmp(name, BTF_IDS_SECTION)) {
464 /*
465 * If target endianness differs from host, we need to bswap32
466 * the .BTF_ids section data on load, because .BTF_ids has
467 * Elf_Type = ELF_T_BYTE, and so libelf returns data buffer in
468 * the target endianness. We repeat this on dump.
469 */
470 if (obj->efile.encoding != ELFDATANATIVE) {
471 pr_debug("bswap_32 .BTF_ids data from target to host endianness\n");
472 bswap_32_data(data->d_buf, data->d_size);
473 }
474 obj->efile.idlist = data;
475 obj->efile.idlist_shndx = idx;
476 obj->efile.idlist_addr = sh.sh_addr;
477 }
478 }
479
480 return 0;
481 }
482
symbols_collect(struct object * obj)483 static int symbols_collect(struct object *obj)
484 {
485 Elf_Scn *scn = NULL;
486 int n, i;
487 GElf_Shdr sh;
488 char *name;
489
490 scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
491 if (!scn)
492 return -1;
493
494 if (gelf_getshdr(scn, &sh) != &sh)
495 return -1;
496
497 n = sh.sh_size / sh.sh_entsize;
498
499 /*
500 * Scan symbols and look for the ones starting with
501 * __BTF_ID__* over .BTF_ids section.
502 */
503 for (i = 0; i < n; i++) {
504 char *prefix;
505 struct btf_id *id;
506 GElf_Sym sym;
507
508 if (!gelf_getsym(obj->efile.symbols, i, &sym))
509 return -1;
510
511 if (sym.st_shndx != obj->efile.idlist_shndx)
512 continue;
513
514 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
515 sym.st_name);
516
517 if (!is_btf_id(name))
518 continue;
519
520 /*
521 * __BTF_ID__TYPE__vfs_truncate__0
522 * prefix = ^
523 */
524 prefix = name + sizeof(BTF_ID_PREFIX) - 1;
525
526 /* struct */
527 if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
528 obj->nr_structs++;
529 id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
530 /* union */
531 } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
532 obj->nr_unions++;
533 id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
534 /* typedef */
535 } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
536 obj->nr_typedefs++;
537 id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
538 /* func */
539 } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
540 obj->nr_funcs++;
541 id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
542 /* set8 */
543 } else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
544 id = add_set(obj, prefix, BTF_ID_KIND_SET8);
545 /*
546 * SET8 objects store list's count, which is encoded
547 * in symbol's size, together with 'cnt' field hence
548 * that - 1.
549 */
550 if (id)
551 id->cnt = sym.st_size / sizeof(uint64_t) - 1;
552 /* set */
553 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
554 id = add_set(obj, prefix, BTF_ID_KIND_SET);
555 /*
556 * SET objects store list's count, which is encoded
557 * in symbol's size, together with 'cnt' field hence
558 * that - 1.
559 */
560 if (id)
561 id->cnt = sym.st_size / sizeof(int) - 1;
562 } else {
563 pr_err("FAILED unsupported prefix %s\n", prefix);
564 return -1;
565 }
566
567 if (!id)
568 return -EINVAL;
569
570 if (id->addr_cnt >= ADDR_CNT) {
571 pr_err("FAILED symbol %s crossed the number of allowed lists\n",
572 id->name);
573 return -1;
574 }
575 id->addr[id->addr_cnt++] = sym.st_value;
576 }
577
578 return 0;
579 }
580
load_btf(struct object * obj)581 static int load_btf(struct object *obj)
582 {
583 struct btf *base_btf = NULL, *btf = NULL;
584 int err;
585
586 if (obj->base_btf_path) {
587 base_btf = btf__parse(obj->base_btf_path, NULL);
588 err = libbpf_get_error(base_btf);
589 if (err) {
590 pr_err("FAILED: load base BTF from %s: %s\n",
591 obj->base_btf_path, strerror(-err));
592 goto out_err;
593 }
594 }
595
596 btf = btf__parse_split(obj->btf_path ?: obj->path, base_btf);
597 err = libbpf_get_error(btf);
598 if (err) {
599 pr_err("FAILED: load BTF from %s: %s\n",
600 obj->btf_path ?: obj->path, strerror(-err));
601 goto out_err;
602 }
603
604 obj->base_btf = base_btf;
605 obj->btf = btf;
606
607 return 0;
608
609 out_err:
610 btf__free(base_btf);
611 btf__free(btf);
612 obj->base_btf = NULL;
613 obj->btf = NULL;
614 return err;
615 }
616
symbols_resolve(struct object * obj)617 static int symbols_resolve(struct object *obj)
618 {
619 int nr_typedefs = obj->nr_typedefs;
620 int nr_structs = obj->nr_structs;
621 int nr_unions = obj->nr_unions;
622 int nr_funcs = obj->nr_funcs;
623 struct btf *btf = obj->btf;
624 int err, type_id;
625 __u32 nr_types;
626
627 err = -1;
628 nr_types = btf__type_cnt(btf);
629
630 /*
631 * Iterate all the BTF types and search for collected symbol IDs.
632 */
633 for (type_id = 1; type_id < nr_types; type_id++) {
634 const struct btf_type *type;
635 struct rb_root *root;
636 struct btf_id *id;
637 const char *str;
638 int *nr;
639
640 type = btf__type_by_id(btf, type_id);
641 if (!type) {
642 pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
643 type_id);
644 goto out;
645 }
646
647 if (btf_is_func(type) && nr_funcs) {
648 nr = &nr_funcs;
649 root = &obj->funcs;
650 } else if (btf_is_struct(type) && nr_structs) {
651 nr = &nr_structs;
652 root = &obj->structs;
653 } else if (btf_is_union(type) && nr_unions) {
654 nr = &nr_unions;
655 root = &obj->unions;
656 } else if (btf_is_typedef(type) && nr_typedefs) {
657 nr = &nr_typedefs;
658 root = &obj->typedefs;
659 } else
660 continue;
661
662 str = btf__name_by_offset(btf, type->name_off);
663 if (!str) {
664 pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
665 type_id);
666 goto out;
667 }
668
669 id = btf_id__find(root, str);
670 if (id) {
671 if (id->id) {
672 pr_info("WARN: multiple IDs found for '%s': %d, %d - using %d\n",
673 str, id->id, type_id, id->id);
674 warnings++;
675 } else {
676 id->id = type_id;
677 (*nr)--;
678 }
679 }
680 }
681
682 err = 0;
683 out:
684 return err;
685 }
686
id_patch(struct object * obj,struct btf_id * id)687 static int id_patch(struct object *obj, struct btf_id *id)
688 {
689 Elf_Data *data = obj->efile.idlist;
690 int *ptr = data->d_buf;
691 int i;
692
693 /* For set, set8, id->id may be 0 */
694 if (!id->id && id->kind != BTF_ID_KIND_SET && id->kind != BTF_ID_KIND_SET8) {
695 pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
696 warnings++;
697 }
698
699 for (i = 0; i < id->addr_cnt; i++) {
700 unsigned long addr = id->addr[i];
701 unsigned long idx = addr - obj->efile.idlist_addr;
702
703 pr_debug("patching addr %5lu: ID %7d [%s]\n",
704 idx, id->id, id->name);
705
706 if (idx >= data->d_size) {
707 pr_err("FAILED patching index %lu out of bounds %lu\n",
708 idx, data->d_size);
709 return -1;
710 }
711
712 idx = idx / sizeof(int);
713 ptr[idx] = id->id;
714 }
715
716 return 0;
717 }
718
__symbols_patch(struct object * obj,struct rb_root * root)719 static int __symbols_patch(struct object *obj, struct rb_root *root)
720 {
721 struct rb_node *next;
722 struct btf_id *id;
723
724 next = rb_first(root);
725 while (next) {
726 id = rb_entry(next, struct btf_id, rb_node);
727
728 if (id_patch(obj, id))
729 return -1;
730
731 next = rb_next(next);
732 }
733 return 0;
734 }
735
cmp_id(const void * pa,const void * pb)736 static int cmp_id(const void *pa, const void *pb)
737 {
738 const int *a = pa, *b = pb;
739
740 return *a - *b;
741 }
742
sets_patch(struct object * obj)743 static int sets_patch(struct object *obj)
744 {
745 Elf_Data *data = obj->efile.idlist;
746 struct rb_node *next;
747 int cnt;
748
749 next = rb_first(&obj->sets);
750 while (next) {
751 struct btf_id_set8 *set8 = NULL;
752 struct btf_id_set *set = NULL;
753 unsigned long addr, off;
754 struct btf_id *id;
755
756 id = rb_entry(next, struct btf_id, rb_node);
757 addr = id->addr[0];
758 off = addr - obj->efile.idlist_addr;
759
760 /* sets are unique */
761 if (id->addr_cnt != 1) {
762 pr_err("FAILED malformed data for set '%s'\n",
763 id->name);
764 return -1;
765 }
766
767 switch (id->kind) {
768 case BTF_ID_KIND_SET:
769 set = data->d_buf + off;
770 cnt = set->cnt;
771 qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id);
772 break;
773 case BTF_ID_KIND_SET8:
774 set8 = data->d_buf + off;
775 cnt = set8->cnt;
776 /*
777 * Make sure id is at the beginning of the pairs
778 * struct, otherwise the below qsort would not work.
779 */
780 BUILD_BUG_ON((u32 *)set8->pairs != &set8->pairs[0].id);
781 qsort(set8->pairs, set8->cnt, sizeof(set8->pairs[0]), cmp_id);
782 break;
783 default:
784 pr_err("Unexpected btf_id_kind %d for set '%s'\n", id->kind, id->name);
785 return -1;
786 }
787
788 pr_debug("sorting addr %5lu: cnt %6d [%s]\n", off, cnt, id->name);
789
790 next = rb_next(next);
791 }
792 return 0;
793 }
794
symbols_patch(struct object * obj)795 static int symbols_patch(struct object *obj)
796 {
797 if (__symbols_patch(obj, &obj->structs) ||
798 __symbols_patch(obj, &obj->unions) ||
799 __symbols_patch(obj, &obj->typedefs) ||
800 __symbols_patch(obj, &obj->funcs) ||
801 __symbols_patch(obj, &obj->sets))
802 return -1;
803
804 if (sets_patch(obj))
805 return -1;
806
807 return 0;
808 }
809
dump_raw_data(const char * out_path,const void * data,u32 size)810 static int dump_raw_data(const char *out_path, const void *data, u32 size)
811 {
812 size_t written;
813 FILE *file;
814
815 file = fopen(out_path, "wb");
816 if (!file) {
817 pr_err("Couldn't open %s for writing\n", out_path);
818 return -1;
819 }
820
821 written = fwrite(data, 1, size, file);
822 if (written != size) {
823 pr_err("Failed to write data to %s\n", out_path);
824 fclose(file);
825 unlink(out_path);
826 return -1;
827 }
828
829 fclose(file);
830 pr_debug("Dumped %lu bytes of data to %s\n", size, out_path);
831
832 return 0;
833 }
834
dump_raw_btf_ids(struct object * obj,const char * out_path)835 static int dump_raw_btf_ids(struct object *obj, const char *out_path)
836 {
837 Elf_Data *data = obj->efile.idlist;
838 int err;
839
840 if (!data || !data->d_buf) {
841 pr_debug("%s has no BTF_ids data to dump\n", obj->path);
842 return 0;
843 }
844
845 /*
846 * If target endianness differs from host, we need to bswap32 the
847 * .BTF_ids section data before dumping so that the output is in
848 * target endianness.
849 */
850 if (obj->efile.encoding != ELFDATANATIVE) {
851 pr_debug("bswap_32 .BTF_ids data from host to target endianness\n");
852 bswap_32_data(data->d_buf, data->d_size);
853 }
854
855 err = dump_raw_data(out_path, data->d_buf, data->d_size);
856 if (err)
857 return -1;
858
859 return 0;
860 }
861
dump_raw_btf(struct btf * btf,const char * out_path)862 static int dump_raw_btf(struct btf *btf, const char *out_path)
863 {
864 const void *raw_btf_data;
865 u32 raw_btf_size;
866 int err;
867
868 raw_btf_data = btf__raw_data(btf, &raw_btf_size);
869 if (!raw_btf_data) {
870 pr_err("btf__raw_data() failed\n");
871 return -1;
872 }
873
874 err = dump_raw_data(out_path, raw_btf_data, raw_btf_size);
875 if (err)
876 return -1;
877
878 return 0;
879 }
880
btf_type_skip_qualifiers(const struct btf * btf,s32 type_id)881 static const struct btf_type *btf_type_skip_qualifiers(const struct btf *btf, s32 type_id)
882 {
883 const struct btf_type *t = btf__type_by_id(btf, type_id);
884
885 while (btf_is_mod(t))
886 t = btf__type_by_id(btf, t->type);
887
888 return t;
889 }
890
push_decl_tag_id(struct btf2btf_context * ctx,u32 decl_tag_id)891 static int push_decl_tag_id(struct btf2btf_context *ctx, u32 decl_tag_id)
892 {
893 u32 *arr = ctx->decl_tags;
894 u32 cap = ctx->max_decl_tags;
895
896 if (ctx->nr_decl_tags + 1 > cap) {
897 cap = max(cap + 256, cap * 2);
898 arr = realloc(arr, sizeof(u32) * cap);
899 if (!arr)
900 return -ENOMEM;
901 ctx->max_decl_tags = cap;
902 ctx->decl_tags = arr;
903 }
904
905 ctx->decl_tags[ctx->nr_decl_tags++] = decl_tag_id;
906
907 return 0;
908 }
909
push_kfunc(struct btf2btf_context * ctx,struct kfunc * kfunc)910 static int push_kfunc(struct btf2btf_context *ctx, struct kfunc *kfunc)
911 {
912 struct kfunc *arr = ctx->kfuncs;
913 u32 cap = ctx->max_kfuncs;
914
915 if (ctx->nr_kfuncs + 1 > cap) {
916 cap = max(cap + 256, cap * 2);
917 arr = realloc(arr, sizeof(struct kfunc) * cap);
918 if (!arr)
919 return -ENOMEM;
920 ctx->max_kfuncs = cap;
921 ctx->kfuncs = arr;
922 }
923
924 ctx->kfuncs[ctx->nr_kfuncs++] = *kfunc;
925
926 return 0;
927 }
928
collect_decl_tags(struct btf2btf_context * ctx)929 static int collect_decl_tags(struct btf2btf_context *ctx)
930 {
931 const u32 type_cnt = btf__type_cnt(ctx->btf);
932 struct btf *btf = ctx->btf;
933 const struct btf_type *t;
934 int err;
935
936 for (u32 id = 1; id < type_cnt; id++) {
937 t = btf__type_by_id(btf, id);
938 if (!btf_is_decl_tag(t))
939 continue;
940 err = push_decl_tag_id(ctx, id);
941 if (err)
942 return err;
943 }
944
945 return 0;
946 }
947
948 /*
949 * To find the kfunc flags having its struct btf_id (with ELF addresses)
950 * we need to find the address that is in range of a set8.
951 * If a set8 is found, then the flags are located at addr + 4 bytes.
952 * Return 0 (no flags!) if not found.
953 */
find_kfunc_flags(struct object * obj,struct btf_id * kfunc_id)954 static u32 find_kfunc_flags(struct object *obj, struct btf_id *kfunc_id)
955 {
956 const u32 *elf_data_ptr = obj->efile.idlist->d_buf;
957 u64 set_lower_addr, set_upper_addr, addr;
958 struct btf_id *set_id;
959 struct rb_node *next;
960 u32 flags;
961 u64 idx;
962
963 for (next = rb_first(&obj->sets); next; next = rb_next(next)) {
964 set_id = rb_entry(next, struct btf_id, rb_node);
965 if (set_id->kind != BTF_ID_KIND_SET8 || set_id->addr_cnt != 1)
966 continue;
967
968 set_lower_addr = set_id->addr[0];
969 set_upper_addr = set_lower_addr + set_id->cnt * sizeof(u64);
970
971 for (u32 i = 0; i < kfunc_id->addr_cnt; i++) {
972 addr = kfunc_id->addr[i];
973 /*
974 * Lower bound is exclusive to skip the 8-byte header of the set.
975 * Upper bound is inclusive to capture the last entry at offset 8*cnt.
976 */
977 if (set_lower_addr < addr && addr <= set_upper_addr) {
978 pr_debug("found kfunc %s in BTF_ID_FLAGS %s\n",
979 kfunc_id->name, set_id->name);
980 idx = addr - obj->efile.idlist_addr;
981 idx = idx / sizeof(u32) + 1;
982 flags = elf_data_ptr[idx];
983
984 return flags;
985 }
986 }
987 }
988
989 return 0;
990 }
991
collect_kfuncs(struct object * obj,struct btf2btf_context * ctx)992 static int collect_kfuncs(struct object *obj, struct btf2btf_context *ctx)
993 {
994 const char *tag_name, *func_name;
995 struct btf *btf = ctx->btf;
996 const struct btf_type *t;
997 u32 flags, func_id;
998 struct kfunc kfunc;
999 struct btf_id *id;
1000 int err;
1001
1002 if (ctx->nr_decl_tags == 0)
1003 return 0;
1004
1005 for (u32 i = 0; i < ctx->nr_decl_tags; i++) {
1006 t = btf__type_by_id(btf, ctx->decl_tags[i]);
1007 if (btf_kflag(t) || btf_decl_tag(t)->component_idx != -1)
1008 continue;
1009
1010 tag_name = btf__name_by_offset(btf, t->name_off);
1011 if (strcmp(tag_name, "bpf_kfunc") != 0)
1012 continue;
1013
1014 func_id = t->type;
1015 t = btf__type_by_id(btf, func_id);
1016 if (!btf_is_func(t))
1017 continue;
1018
1019 func_name = btf__name_by_offset(btf, t->name_off);
1020 if (!func_name)
1021 continue;
1022
1023 id = btf_id__find(&obj->funcs, func_name);
1024 if (!id || id->kind != BTF_ID_KIND_SYM)
1025 continue;
1026
1027 flags = find_kfunc_flags(obj, id);
1028
1029 kfunc.name = id->name;
1030 kfunc.btf_id = func_id;
1031 kfunc.flags = flags;
1032
1033 err = push_kfunc(ctx, &kfunc);
1034 if (err)
1035 return err;
1036 }
1037
1038 return 0;
1039 }
1040
build_btf2btf_context(struct object * obj,struct btf2btf_context * ctx)1041 static int build_btf2btf_context(struct object *obj, struct btf2btf_context *ctx)
1042 {
1043 int err;
1044
1045 ctx->btf = obj->btf;
1046
1047 err = collect_decl_tags(ctx);
1048 if (err) {
1049 pr_err("ERROR: resolve_btfids: failed to collect decl tags from BTF\n");
1050 return err;
1051 }
1052
1053 err = collect_kfuncs(obj, ctx);
1054 if (err) {
1055 pr_err("ERROR: resolve_btfids: failed to collect kfuncs from BTF\n");
1056 return err;
1057 }
1058
1059 return 0;
1060 }
1061
1062
1063 /* Implicit BPF kfunc arguments can only be of particular types */
is_kf_implicit_arg(const struct btf * btf,const struct btf_param * p)1064 static bool is_kf_implicit_arg(const struct btf *btf, const struct btf_param *p)
1065 {
1066 static const char *const kf_implicit_arg_types[] = {
1067 "bpf_prog_aux",
1068 "btf_struct_meta",
1069 };
1070 const struct btf_type *t;
1071 const char *name;
1072
1073 t = btf_type_skip_qualifiers(btf, p->type);
1074 if (!btf_is_ptr(t))
1075 return false;
1076
1077 t = btf_type_skip_qualifiers(btf, t->type);
1078 if (!btf_is_struct(t))
1079 return false;
1080
1081 name = btf__name_by_offset(btf, t->name_off);
1082 if (!name)
1083 return false;
1084
1085 for (int i = 0; i < ARRAY_SIZE(kf_implicit_arg_types); i++)
1086 if (strcmp(name, kf_implicit_arg_types[i]) == 0)
1087 return true;
1088
1089 return false;
1090 }
1091
1092 /*
1093 * For a kfunc with KF_IMPLICIT_ARGS we do the following:
1094 * 1. Add a new function with _impl suffix in the name, with the prototype
1095 * of the original kfunc.
1096 * 2. Add all decl tags except "bpf_kfunc" for the _impl func.
1097 * 3. Add a new function prototype with modified list of arguments:
1098 * omitting implicit args.
1099 * 4. Change the prototype of the original kfunc to the new one.
1100 *
1101 * This way we transform the BTF associated with the kfunc from
1102 * __bpf_kfunc bpf_foo(int arg1, void *implicit_arg);
1103 * into
1104 * bpf_foo_impl(int arg1, void *implicit_arg);
1105 * __bpf_kfunc bpf_foo(int arg1);
1106 *
1107 * If a kfunc with KF_IMPLICIT_ARGS already has an _impl counterpart
1108 * in BTF, then it's a legacy case: an _impl function is declared in the
1109 * source code. In this case, we can skip adding an _impl function, but we
1110 * still have to add a func prototype that omits implicit args.
1111 */
process_kfunc_with_implicit_args(struct btf2btf_context * ctx,struct kfunc * kfunc)1112 static int process_kfunc_with_implicit_args(struct btf2btf_context *ctx, struct kfunc *kfunc)
1113 {
1114 s32 idx, new_proto_id, new_func_id, proto_id;
1115 const char *param_name, *tag_name;
1116 const struct btf_param *params;
1117 enum btf_func_linkage linkage;
1118 char tmp_name[KSYM_NAME_LEN];
1119 struct btf *btf = ctx->btf;
1120 int err, len, nr_params;
1121 struct btf_type *t;
1122
1123 t = (struct btf_type *)btf__type_by_id(btf, kfunc->btf_id);
1124 if (!t || !btf_is_func(t)) {
1125 pr_err("ERROR: resolve_btfids: btf id %d is not a function\n", kfunc->btf_id);
1126 return -EINVAL;
1127 }
1128
1129 linkage = btf_vlen(t);
1130
1131 proto_id = t->type;
1132 t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1133 if (!t || !btf_is_func_proto(t)) {
1134 pr_err("ERROR: resolve_btfids: btf id %d is not a function prototype\n", proto_id);
1135 return -EINVAL;
1136 }
1137
1138 len = snprintf(tmp_name, sizeof(tmp_name), "%s%s", kfunc->name, KF_IMPL_SUFFIX);
1139 if (len < 0 || len >= sizeof(tmp_name)) {
1140 pr_err("ERROR: function name is too long: %s%s\n", kfunc->name, KF_IMPL_SUFFIX);
1141 return -E2BIG;
1142 }
1143
1144 if (btf__find_by_name_kind(btf, tmp_name, BTF_KIND_FUNC) > 0) {
1145 pr_debug("resolve_btfids: function %s already exists in BTF\n", tmp_name);
1146 goto add_new_proto;
1147 }
1148
1149 /* Add a new function with _impl suffix and original prototype */
1150 new_func_id = btf__add_func(btf, tmp_name, linkage, proto_id);
1151 if (new_func_id < 0) {
1152 pr_err("ERROR: resolve_btfids: failed to add func %s to BTF\n", tmp_name);
1153 return new_func_id;
1154 }
1155
1156 /* Copy all decl tags except "bpf_kfunc" from the original kfunc to the new one */
1157 for (int i = 0; i < ctx->nr_decl_tags; i++) {
1158 t = (struct btf_type *)btf__type_by_id(btf, ctx->decl_tags[i]);
1159 if (t->type != kfunc->btf_id)
1160 continue;
1161
1162 tag_name = btf__name_by_offset(btf, t->name_off);
1163 if (strcmp(tag_name, "bpf_kfunc") == 0)
1164 continue;
1165
1166 idx = btf_decl_tag(t)->component_idx;
1167
1168 if (btf_kflag(t))
1169 err = btf__add_decl_attr(btf, tag_name, new_func_id, idx);
1170 else
1171 err = btf__add_decl_tag(btf, tag_name, new_func_id, idx);
1172
1173 if (err < 0) {
1174 pr_err("ERROR: resolve_btfids: failed to add decl tag %s for %s\n",
1175 tag_name, tmp_name);
1176 return -EINVAL;
1177 }
1178 }
1179
1180 add_new_proto:
1181 t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1182 new_proto_id = btf__add_func_proto(btf, t->type);
1183 if (new_proto_id < 0) {
1184 pr_err("ERROR: resolve_btfids: failed to add func proto for %s\n", kfunc->name);
1185 return new_proto_id;
1186 }
1187
1188 /* Add non-implicit args to the new prototype */
1189 t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1190 nr_params = btf_vlen(t);
1191 for (int i = 0; i < nr_params; i++) {
1192 params = btf_params(t);
1193 if (is_kf_implicit_arg(btf, ¶ms[i]))
1194 break;
1195 param_name = btf__name_by_offset(btf, params[i].name_off);
1196 err = btf__add_func_param(btf, param_name, params[i].type);
1197 if (err < 0) {
1198 pr_err("ERROR: resolve_btfids: failed to add param %s for %s\n",
1199 param_name, kfunc->name);
1200 return err;
1201 }
1202 t = (struct btf_type *)btf__type_by_id(btf, proto_id);
1203 }
1204
1205 /* Finally change the prototype of the original kfunc to the new one */
1206 t = (struct btf_type *)btf__type_by_id(btf, kfunc->btf_id);
1207 t->type = new_proto_id;
1208
1209 pr_debug("resolve_btfids: updated BTF for kfunc with implicit args %s\n", kfunc->name);
1210
1211 return 0;
1212 }
1213
btf2btf(struct object * obj)1214 static int btf2btf(struct object *obj)
1215 {
1216 struct btf2btf_context ctx = {};
1217 int err;
1218
1219 err = build_btf2btf_context(obj, &ctx);
1220 if (err)
1221 goto out;
1222
1223 for (u32 i = 0; i < ctx.nr_kfuncs; i++) {
1224 struct kfunc *kfunc = &ctx.kfuncs[i];
1225
1226 if (!(kfunc->flags & KF_IMPLICIT_ARGS))
1227 continue;
1228
1229 err = process_kfunc_with_implicit_args(&ctx, kfunc);
1230 if (err)
1231 goto out;
1232 }
1233
1234 err = 0;
1235 out:
1236 free(ctx.decl_tags);
1237 free(ctx.kfuncs);
1238
1239 return err;
1240 }
1241
1242 /*
1243 * Sort types by name in ascending order resulting in all
1244 * anonymous types being placed before named types.
1245 */
cmp_type_names(const void * a,const void * b,void * priv)1246 static int cmp_type_names(const void *a, const void *b, void *priv)
1247 {
1248 struct btf *btf = (struct btf *)priv;
1249 const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
1250 const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
1251 const char *na, *nb;
1252 int r;
1253
1254 na = btf__str_by_offset(btf, ta->name_off);
1255 nb = btf__str_by_offset(btf, tb->name_off);
1256 r = strcmp(na, nb);
1257 if (r != 0)
1258 return r;
1259
1260 /* preserve original relative order of anonymous or same-named types */
1261 return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
1262 }
1263
sort_btf_by_name(struct btf * btf)1264 static int sort_btf_by_name(struct btf *btf)
1265 {
1266 __u32 *permute_ids = NULL, *id_map = NULL;
1267 int nr_types, i, err = 0;
1268 __u32 start_id = 0, id;
1269
1270 if (btf__base_btf(btf))
1271 start_id = btf__type_cnt(btf__base_btf(btf));
1272 nr_types = btf__type_cnt(btf) - start_id;
1273
1274 permute_ids = calloc(nr_types, sizeof(*permute_ids));
1275 if (!permute_ids) {
1276 err = -ENOMEM;
1277 goto out;
1278 }
1279
1280 id_map = calloc(nr_types, sizeof(*id_map));
1281 if (!id_map) {
1282 err = -ENOMEM;
1283 goto out;
1284 }
1285
1286 for (i = 0, id = start_id; i < nr_types; i++, id++)
1287 permute_ids[i] = id;
1288
1289 qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
1290 btf);
1291
1292 for (i = 0; i < nr_types; i++) {
1293 id = permute_ids[i] - start_id;
1294 id_map[id] = i + start_id;
1295 }
1296
1297 err = btf__permute(btf, id_map, nr_types, NULL);
1298 if (err)
1299 pr_err("FAILED: btf permute: %s\n", strerror(-err));
1300
1301 out:
1302 free(permute_ids);
1303 free(id_map);
1304 return err;
1305 }
1306
finalize_btf(struct object * obj)1307 static int finalize_btf(struct object *obj)
1308 {
1309 struct btf *base_btf = obj->base_btf, *btf = obj->btf;
1310 int err;
1311
1312 if (obj->base_btf && obj->distill_base) {
1313 err = btf__distill_base(obj->btf, &base_btf, &btf);
1314 if (err) {
1315 pr_err("FAILED to distill base BTF: %s\n", strerror(errno));
1316 goto out_err;
1317 }
1318
1319 btf__free(obj->base_btf);
1320 btf__free(obj->btf);
1321 obj->base_btf = base_btf;
1322 obj->btf = btf;
1323 }
1324
1325 err = sort_btf_by_name(obj->btf);
1326 if (err) {
1327 pr_err("FAILED to sort BTF: %s\n", strerror(errno));
1328 goto out_err;
1329 }
1330
1331 return 0;
1332
1333 out_err:
1334 btf__free(base_btf);
1335 btf__free(btf);
1336 obj->base_btf = NULL;
1337 obj->btf = NULL;
1338
1339 return err;
1340 }
1341
make_out_path(char * buf,u32 buf_sz,const char * in_path,const char * suffix)1342 static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path, const char *suffix)
1343 {
1344 int len = snprintf(buf, buf_sz, "%s%s", in_path, suffix);
1345
1346 if (len < 0 || len >= buf_sz) {
1347 pr_err("Output path is too long: %s%s\n", in_path, suffix);
1348 return -E2BIG;
1349 }
1350
1351 return 0;
1352 }
1353
1354 /*
1355 * Patch the .BTF_ids section of an ELF file with data from provided file.
1356 * Equivalent to: objcopy --update-section .BTF_ids=<btfids> <elf>
1357 *
1358 * 1. Find .BTF_ids section in the ELF
1359 * 2. Verify that blob file size matches section size
1360 * 3. Update section data buffer with blob data
1361 * 4. Write the ELF file
1362 */
patch_btfids(const char * btfids_path,const char * elf_path)1363 static int patch_btfids(const char *btfids_path, const char *elf_path)
1364 {
1365 Elf_Scn *scn = NULL;
1366 FILE *btfids_file;
1367 size_t shdrstrndx;
1368 int fd, err = -1;
1369 Elf_Data *data;
1370 struct stat st;
1371 GElf_Shdr sh;
1372 char *name;
1373 Elf *elf;
1374
1375 elf_version(EV_CURRENT);
1376
1377 fd = open(elf_path, O_RDWR, 0666);
1378 if (fd < 0) {
1379 pr_err("FAILED to open %s: %s\n", elf_path, strerror(errno));
1380 return -1;
1381 }
1382
1383 elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
1384 if (!elf) {
1385 close(fd);
1386 pr_err("FAILED cannot create ELF descriptor: %s\n", elf_errmsg(-1));
1387 return -1;
1388 }
1389
1390 elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
1391
1392 if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
1393 pr_err("FAILED cannot get shdr str ndx\n");
1394 goto out;
1395 }
1396
1397 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1398
1399 if (gelf_getshdr(scn, &sh) != &sh) {
1400 pr_err("FAILED to get section header\n");
1401 goto out;
1402 }
1403
1404 name = elf_strptr(elf, shdrstrndx, sh.sh_name);
1405 if (!name)
1406 continue;
1407
1408 if (strcmp(name, BTF_IDS_SECTION) == 0)
1409 break;
1410 }
1411
1412 if (!scn) {
1413 pr_err("FAILED: section %s not found in %s\n", BTF_IDS_SECTION, elf_path);
1414 goto out;
1415 }
1416
1417 data = elf_getdata(scn, NULL);
1418 if (!data) {
1419 pr_err("FAILED to get %s section data from %s\n", BTF_IDS_SECTION, elf_path);
1420 goto out;
1421 }
1422
1423 if (stat(btfids_path, &st) < 0) {
1424 pr_err("FAILED to stat %s: %s\n", btfids_path, strerror(errno));
1425 goto out;
1426 }
1427
1428 if ((size_t)st.st_size != data->d_size) {
1429 pr_err("FAILED: size mismatch - %s section in %s is %zu bytes, %s is %zu bytes\n",
1430 BTF_IDS_SECTION, elf_path, data->d_size, btfids_path, (size_t)st.st_size);
1431 goto out;
1432 }
1433
1434 btfids_file = fopen(btfids_path, "rb");
1435 if (!btfids_file) {
1436 pr_err("FAILED to open %s: %s\n", btfids_path, strerror(errno));
1437 goto out;
1438 }
1439
1440 pr_debug("Copying data from %s to %s section of %s (%zu bytes)\n",
1441 btfids_path, BTF_IDS_SECTION, elf_path, data->d_size);
1442
1443 if (fread(data->d_buf, data->d_size, 1, btfids_file) != 1) {
1444 pr_err("FAILED to read %s\n", btfids_path);
1445 fclose(btfids_file);
1446 goto out;
1447 }
1448 fclose(btfids_file);
1449
1450 elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1451 if (elf_update(elf, ELF_C_WRITE) < 0) {
1452 pr_err("FAILED to update ELF file %s\n", elf_path);
1453 goto out;
1454 }
1455
1456 err = 0;
1457 out:
1458 elf_end(elf);
1459 close(fd);
1460
1461 return err;
1462 }
1463
1464 static const char * const resolve_btfids_usage[] = {
1465 "resolve_btfids [<options>] <ELF object>",
1466 "resolve_btfids --patch_btfids <.BTF_ids file> <ELF object>",
1467 NULL
1468 };
1469
main(int argc,const char ** argv)1470 int main(int argc, const char **argv)
1471 {
1472 struct object obj = {
1473 .efile = {
1474 .idlist_shndx = -1,
1475 .symbols_shndx = -1,
1476 },
1477 .structs = RB_ROOT,
1478 .unions = RB_ROOT,
1479 .typedefs = RB_ROOT,
1480 .funcs = RB_ROOT,
1481 .sets = RB_ROOT,
1482 };
1483 const char *btfids_path = NULL;
1484 bool fatal_warnings = false;
1485 bool resolve_btfids = true;
1486 char out_path[PATH_MAX];
1487
1488 struct option btfid_options[] = {
1489 OPT_INCR('v', "verbose", &verbose,
1490 "be more verbose (show errors, etc)"),
1491 OPT_STRING(0, "btf", &obj.btf_path, "file",
1492 "path to a file with input BTF data"),
1493 OPT_STRING('b', "btf_base", &obj.base_btf_path, "file",
1494 "path of file providing base BTF"),
1495 OPT_BOOLEAN(0, "fatal_warnings", &fatal_warnings,
1496 "turn warnings into errors"),
1497 OPT_BOOLEAN(0, "distill_base", &obj.distill_base,
1498 "distill --btf_base and emit .BTF.base section data"),
1499 OPT_STRING(0, "patch_btfids", &btfids_path, "file",
1500 "path to .BTF_ids section data blob to patch into ELF file"),
1501 OPT_END()
1502 };
1503 int err = -1;
1504
1505 argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
1506 PARSE_OPT_STOP_AT_NON_OPTION);
1507 if (argc != 1)
1508 usage_with_options(resolve_btfids_usage, btfid_options);
1509
1510 obj.path = argv[0];
1511
1512 if (btfids_path)
1513 return patch_btfids(btfids_path, obj.path);
1514
1515 if (elf_collect(&obj))
1516 goto out;
1517
1518 /*
1519 * We did not find .BTF_ids section or symbols section,
1520 * nothing to do..
1521 */
1522 if (obj.efile.idlist_shndx == -1 ||
1523 obj.efile.symbols_shndx == -1) {
1524 pr_debug("Cannot find .BTF_ids or symbols sections, skip symbols resolution\n");
1525 resolve_btfids = false;
1526 }
1527
1528 if (resolve_btfids)
1529 if (symbols_collect(&obj))
1530 goto out;
1531
1532 if (load_btf(&obj))
1533 goto out;
1534
1535 if (btf2btf(&obj))
1536 goto out;
1537
1538 if (finalize_btf(&obj))
1539 goto out;
1540
1541 if (!resolve_btfids)
1542 goto dump_btf;
1543
1544 if (symbols_resolve(&obj))
1545 goto out;
1546
1547 if (symbols_patch(&obj))
1548 goto out;
1549
1550 err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_IDS_SECTION);
1551 err = err ?: dump_raw_btf_ids(&obj, out_path);
1552 if (err)
1553 goto out;
1554
1555 dump_btf:
1556 err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_ELF_SEC);
1557 err = err ?: dump_raw_btf(obj.btf, out_path);
1558 if (err)
1559 goto out;
1560
1561 if (obj.base_btf && obj.distill_base) {
1562 err = make_out_path(out_path, sizeof(out_path), obj.path, BTF_BASE_ELF_SEC);
1563 err = err ?: dump_raw_btf(obj.base_btf, out_path);
1564 if (err)
1565 goto out;
1566 }
1567
1568 if (!(fatal_warnings && warnings))
1569 err = 0;
1570 out:
1571 btf__free(obj.base_btf);
1572 btf__free(obj.btf);
1573 btf_id__free_all(&obj.structs);
1574 btf_id__free_all(&obj.unions);
1575 btf_id__free_all(&obj.typedefs);
1576 btf_id__free_all(&obj.funcs);
1577 btf_id__free_all(&obj.sets);
1578 if (obj.efile.elf) {
1579 elf_end(obj.efile.elf);
1580 close(obj.efile.fd);
1581 }
1582 return err;
1583 }
1584