Merge tag 'trace-v5.10-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.git] / tools / bpf / resolve_btfids / main.c
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
50 #define  _GNU_SOURCE
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <libelf.h>
56 #include <gelf.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <linux/rbtree.h>
61 #include <linux/zalloc.h>
62 #include <linux/err.h>
63 #include <btf.h>
64 #include <libbpf.h>
65 #include <parse-options.h>
66
67 #define BTF_IDS_SECTION ".BTF_ids"
68 #define BTF_ID          "__BTF_ID__"
69
70 #define BTF_STRUCT      "struct"
71 #define BTF_UNION       "union"
72 #define BTF_TYPEDEF     "typedef"
73 #define BTF_FUNC        "func"
74 #define BTF_SET         "set"
75
76 #define ADDR_CNT        100
77
78 struct btf_id {
79         struct rb_node   rb_node;
80         char            *name;
81         union {
82                 int      id;
83                 int      cnt;
84         };
85         int              addr_cnt;
86         Elf64_Addr       addr[ADDR_CNT];
87 };
88
89 struct object {
90         const char *path;
91         const char *btf;
92
93         struct {
94                 int              fd;
95                 Elf             *elf;
96                 Elf_Data        *symbols;
97                 Elf_Data        *idlist;
98                 int              symbols_shndx;
99                 int              idlist_shndx;
100                 size_t           strtabidx;
101                 unsigned long    idlist_addr;
102         } efile;
103
104         struct rb_root  sets;
105         struct rb_root  structs;
106         struct rb_root  unions;
107         struct rb_root  typedefs;
108         struct rb_root  funcs;
109
110         int nr_funcs;
111         int nr_structs;
112         int nr_unions;
113         int nr_typedefs;
114 };
115
116 static int verbose;
117
118 int eprintf(int level, int var, const char *fmt, ...)
119 {
120         va_list args;
121         int ret;
122
123         if (var >= level) {
124                 va_start(args, fmt);
125                 ret = vfprintf(stderr, fmt, args);
126                 va_end(args);
127         }
128         return ret;
129 }
130
131 #ifndef pr_fmt
132 #define pr_fmt(fmt) fmt
133 #endif
134
135 #define pr_debug(fmt, ...) \
136         eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
137 #define pr_debugN(n, fmt, ...) \
138         eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
139 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
140 #define pr_err(fmt, ...) \
141         eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
142
143 static bool is_btf_id(const char *name)
144 {
145         return name && !strncmp(name, BTF_ID, sizeof(BTF_ID) - 1);
146 }
147
148 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
149 {
150         struct rb_node *p = root->rb_node;
151         struct btf_id *id;
152         int cmp;
153
154         while (p) {
155                 id = rb_entry(p, struct btf_id, rb_node);
156                 cmp = strcmp(id->name, name);
157                 if (cmp < 0)
158                         p = p->rb_left;
159                 else if (cmp > 0)
160                         p = p->rb_right;
161                 else
162                         return id;
163         }
164         return NULL;
165 }
166
167 static struct btf_id*
168 btf_id__add(struct rb_root *root, char *name, bool unique)
169 {
170         struct rb_node **p = &root->rb_node;
171         struct rb_node *parent = NULL;
172         struct btf_id *id;
173         int cmp;
174
175         while (*p != NULL) {
176                 parent = *p;
177                 id = rb_entry(parent, struct btf_id, rb_node);
178                 cmp = strcmp(id->name, name);
179                 if (cmp < 0)
180                         p = &(*p)->rb_left;
181                 else if (cmp > 0)
182                         p = &(*p)->rb_right;
183                 else
184                         return unique ? NULL : id;
185         }
186
187         id = zalloc(sizeof(*id));
188         if (id) {
189                 pr_debug("adding symbol %s\n", name);
190                 id->name = name;
191                 rb_link_node(&id->rb_node, parent, p);
192                 rb_insert_color(&id->rb_node, root);
193         }
194         return id;
195 }
196
197 static char *get_id(const char *prefix_end)
198 {
199         /*
200          * __BTF_ID__func__vfs_truncate__0
201          * prefix_end =  ^
202          * pos        =    ^
203          */
204         int len = strlen(prefix_end);
205         int pos = sizeof("__") - 1;
206         char *p, *id;
207
208         if (pos >= len)
209                 return NULL;
210
211         id = strdup(prefix_end + pos);
212         if (id) {
213                 /*
214                  * __BTF_ID__func__vfs_truncate__0
215                  * id =            ^
216                  *
217                  * cut the unique id part
218                  */
219                 p = strrchr(id, '_');
220                 p--;
221                 if (*p != '_') {
222                         free(id);
223                         return NULL;
224                 }
225                 *p = '\0';
226         }
227         return id;
228 }
229
230 static struct btf_id *add_set(struct object *obj, char *name)
231 {
232         /*
233          * __BTF_ID__set__name
234          * name =    ^
235          * id   =         ^
236          */
237         char *id = name + sizeof(BTF_SET "__") - 1;
238         int len = strlen(name);
239
240         if (id >= name + len) {
241                 pr_err("FAILED to parse set name: %s\n", name);
242                 return NULL;
243         }
244
245         return btf_id__add(&obj->sets, id, true);
246 }
247
248 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
249 {
250         char *id;
251
252         id = get_id(name + size);
253         if (!id) {
254                 pr_err("FAILED to parse symbol name: %s\n", name);
255                 return NULL;
256         }
257
258         return btf_id__add(root, id, false);
259 }
260
261 /*
262  * The data of compressed section should be aligned to 4
263  * (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
264  * sets sh_addralign to 1, which makes libelf fail with
265  * misaligned section error during the update:
266  *    FAILED elf_update(WRITE): invalid section alignment
267  *
268  * While waiting for ld fix, we fix the compressed sections
269  * sh_addralign value manualy.
270  */
271 static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
272 {
273         int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
274
275         if (!(sh->sh_flags & SHF_COMPRESSED))
276                 return 0;
277
278         if (sh->sh_addralign == expected)
279                 return 0;
280
281         pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
282                   sh->sh_addralign, expected);
283
284         sh->sh_addralign = expected;
285
286         if (gelf_update_shdr(scn, sh) == 0) {
287                 printf("FAILED cannot update section header: %s\n",
288                         elf_errmsg(-1));
289                 return -1;
290         }
291         return 0;
292 }
293
294 static int elf_collect(struct object *obj)
295 {
296         Elf_Scn *scn = NULL;
297         size_t shdrstrndx;
298         int idx = 0;
299         Elf *elf;
300         int fd;
301
302         fd = open(obj->path, O_RDWR, 0666);
303         if (fd == -1) {
304                 pr_err("FAILED cannot open %s: %s\n",
305                         obj->path, strerror(errno));
306                 return -1;
307         }
308
309         elf_version(EV_CURRENT);
310
311         elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
312         if (!elf) {
313                 pr_err("FAILED cannot create ELF descriptor: %s\n",
314                         elf_errmsg(-1));
315                 return -1;
316         }
317
318         obj->efile.fd  = fd;
319         obj->efile.elf = elf;
320
321         elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
322
323         if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
324                 pr_err("FAILED cannot get shdr str ndx\n");
325                 return -1;
326         }
327
328         /*
329          * Scan all the elf sections and look for save data
330          * from .BTF_ids section and symbols.
331          */
332         while ((scn = elf_nextscn(elf, scn)) != NULL) {
333                 Elf_Data *data;
334                 GElf_Shdr sh;
335                 char *name;
336
337                 idx++;
338                 if (gelf_getshdr(scn, &sh) != &sh) {
339                         pr_err("FAILED get section(%d) header\n", idx);
340                         return -1;
341                 }
342
343                 name = elf_strptr(elf, shdrstrndx, sh.sh_name);
344                 if (!name) {
345                         pr_err("FAILED get section(%d) name\n", idx);
346                         return -1;
347                 }
348
349                 data = elf_getdata(scn, 0);
350                 if (!data) {
351                         pr_err("FAILED to get section(%d) data from %s\n",
352                                 idx, name);
353                         return -1;
354                 }
355
356                 pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
357                           idx, name, (unsigned long) data->d_size,
358                           (int) sh.sh_link, (unsigned long) sh.sh_flags,
359                           (int) sh.sh_type);
360
361                 if (sh.sh_type == SHT_SYMTAB) {
362                         obj->efile.symbols       = data;
363                         obj->efile.symbols_shndx = idx;
364                         obj->efile.strtabidx     = sh.sh_link;
365                 } else if (!strcmp(name, BTF_IDS_SECTION)) {
366                         obj->efile.idlist       = data;
367                         obj->efile.idlist_shndx = idx;
368                         obj->efile.idlist_addr  = sh.sh_addr;
369                 }
370
371                 if (compressed_section_fix(elf, scn, &sh))
372                         return -1;
373         }
374
375         return 0;
376 }
377
378 static int symbols_collect(struct object *obj)
379 {
380         Elf_Scn *scn = NULL;
381         int n, i, err = 0;
382         GElf_Shdr sh;
383         char *name;
384
385         scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
386         if (!scn)
387                 return -1;
388
389         if (gelf_getshdr(scn, &sh) != &sh)
390                 return -1;
391
392         n = sh.sh_size / sh.sh_entsize;
393
394         /*
395          * Scan symbols and look for the ones starting with
396          * __BTF_ID__* over .BTF_ids section.
397          */
398         for (i = 0; !err && i < n; i++) {
399                 char *tmp, *prefix;
400                 struct btf_id *id;
401                 GElf_Sym sym;
402                 int err = -1;
403
404                 if (!gelf_getsym(obj->efile.symbols, i, &sym))
405                         return -1;
406
407                 if (sym.st_shndx != obj->efile.idlist_shndx)
408                         continue;
409
410                 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
411                                   sym.st_name);
412
413                 if (!is_btf_id(name))
414                         continue;
415
416                 /*
417                  * __BTF_ID__TYPE__vfs_truncate__0
418                  * prefix =  ^
419                  */
420                 prefix = name + sizeof(BTF_ID) - 1;
421
422                 /* struct */
423                 if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
424                         obj->nr_structs++;
425                         id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
426                 /* union  */
427                 } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
428                         obj->nr_unions++;
429                         id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
430                 /* typedef */
431                 } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
432                         obj->nr_typedefs++;
433                         id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
434                 /* func */
435                 } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
436                         obj->nr_funcs++;
437                         id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
438                 /* set */
439                 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
440                         id = add_set(obj, prefix);
441                         /*
442                          * SET objects store list's count, which is encoded
443                          * in symbol's size, together with 'cnt' field hence
444                          * that - 1.
445                          */
446                         if (id)
447                                 id->cnt = sym.st_size / sizeof(int) - 1;
448                 } else {
449                         pr_err("FAILED unsupported prefix %s\n", prefix);
450                         return -1;
451                 }
452
453                 if (!id)
454                         return -ENOMEM;
455
456                 if (id->addr_cnt >= ADDR_CNT) {
457                         pr_err("FAILED symbol %s crossed the number of allowed lists",
458                                 id->name);
459                         return -1;
460                 }
461                 id->addr[id->addr_cnt++] = sym.st_value;
462         }
463
464         return 0;
465 }
466
467 static int symbols_resolve(struct object *obj)
468 {
469         int nr_typedefs = obj->nr_typedefs;
470         int nr_structs  = obj->nr_structs;
471         int nr_unions   = obj->nr_unions;
472         int nr_funcs    = obj->nr_funcs;
473         int err, type_id;
474         struct btf *btf;
475         __u32 nr;
476
477         btf = btf__parse(obj->btf ?: obj->path, NULL);
478         err = libbpf_get_error(btf);
479         if (err) {
480                 pr_err("FAILED: load BTF from %s: %s",
481                         obj->path, strerror(err));
482                 return -1;
483         }
484
485         err = -1;
486         nr  = btf__get_nr_types(btf);
487
488         /*
489          * Iterate all the BTF types and search for collected symbol IDs.
490          */
491         for (type_id = 1; type_id <= nr; type_id++) {
492                 const struct btf_type *type;
493                 struct rb_root *root;
494                 struct btf_id *id;
495                 const char *str;
496                 int *nr;
497
498                 type = btf__type_by_id(btf, type_id);
499                 if (!type) {
500                         pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
501                                 type_id);
502                         goto out;
503                 }
504
505                 if (btf_is_func(type) && nr_funcs) {
506                         nr   = &nr_funcs;
507                         root = &obj->funcs;
508                 } else if (btf_is_struct(type) && nr_structs) {
509                         nr   = &nr_structs;
510                         root = &obj->structs;
511                 } else if (btf_is_union(type) && nr_unions) {
512                         nr   = &nr_unions;
513                         root = &obj->unions;
514                 } else if (btf_is_typedef(type) && nr_typedefs) {
515                         nr   = &nr_typedefs;
516                         root = &obj->typedefs;
517                 } else
518                         continue;
519
520                 str = btf__name_by_offset(btf, type->name_off);
521                 if (!str) {
522                         pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
523                                 type_id);
524                         goto out;
525                 }
526
527                 id = btf_id__find(root, str);
528                 if (id) {
529                         id->id = type_id;
530                         (*nr)--;
531                 }
532         }
533
534         err = 0;
535 out:
536         btf__free(btf);
537         return err;
538 }
539
540 static int id_patch(struct object *obj, struct btf_id *id)
541 {
542         Elf_Data *data = obj->efile.idlist;
543         int *ptr = data->d_buf;
544         int i;
545
546         if (!id->id) {
547                 pr_err("FAILED unresolved symbol %s\n", id->name);
548                 return -EINVAL;
549         }
550
551         for (i = 0; i < id->addr_cnt; i++) {
552                 unsigned long addr = id->addr[i];
553                 unsigned long idx = addr - obj->efile.idlist_addr;
554
555                 pr_debug("patching addr %5lu: ID %7d [%s]\n",
556                          idx, id->id, id->name);
557
558                 if (idx >= data->d_size) {
559                         pr_err("FAILED patching index %lu out of bounds %lu\n",
560                                 idx, data->d_size);
561                         return -1;
562                 }
563
564                 idx = idx / sizeof(int);
565                 ptr[idx] = id->id;
566         }
567
568         return 0;
569 }
570
571 static int __symbols_patch(struct object *obj, struct rb_root *root)
572 {
573         struct rb_node *next;
574         struct btf_id *id;
575
576         next = rb_first(root);
577         while (next) {
578                 id = rb_entry(next, struct btf_id, rb_node);
579
580                 if (id_patch(obj, id))
581                         return -1;
582
583                 next = rb_next(next);
584         }
585         return 0;
586 }
587
588 static int cmp_id(const void *pa, const void *pb)
589 {
590         const int *a = pa, *b = pb;
591
592         return *a - *b;
593 }
594
595 static int sets_patch(struct object *obj)
596 {
597         Elf_Data *data = obj->efile.idlist;
598         int *ptr = data->d_buf;
599         struct rb_node *next;
600
601         next = rb_first(&obj->sets);
602         while (next) {
603                 unsigned long addr, idx;
604                 struct btf_id *id;
605                 int *base;
606                 int cnt;
607
608                 id   = rb_entry(next, struct btf_id, rb_node);
609                 addr = id->addr[0];
610                 idx  = addr - obj->efile.idlist_addr;
611
612                 /* sets are unique */
613                 if (id->addr_cnt != 1) {
614                         pr_err("FAILED malformed data for set '%s'\n",
615                                 id->name);
616                         return -1;
617                 }
618
619                 idx = idx / sizeof(int);
620                 base = &ptr[idx] + 1;
621                 cnt = ptr[idx];
622
623                 pr_debug("sorting  addr %5lu: cnt %6d [%s]\n",
624                          (idx + 1) * sizeof(int), cnt, id->name);
625
626                 qsort(base, cnt, sizeof(int), cmp_id);
627
628                 next = rb_next(next);
629         }
630         return 0;
631 }
632
633 static int symbols_patch(struct object *obj)
634 {
635         int err;
636
637         if (__symbols_patch(obj, &obj->structs)  ||
638             __symbols_patch(obj, &obj->unions)   ||
639             __symbols_patch(obj, &obj->typedefs) ||
640             __symbols_patch(obj, &obj->funcs)    ||
641             __symbols_patch(obj, &obj->sets))
642                 return -1;
643
644         if (sets_patch(obj))
645                 return -1;
646
647         elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
648
649         err = elf_update(obj->efile.elf, ELF_C_WRITE);
650         if (err < 0) {
651                 pr_err("FAILED elf_update(WRITE): %s\n",
652                         elf_errmsg(-1));
653         }
654
655         pr_debug("update %s for %s\n",
656                  err >= 0 ? "ok" : "failed", obj->path);
657         return err < 0 ? -1 : 0;
658 }
659
660 static const char * const resolve_btfids_usage[] = {
661         "resolve_btfids [<options>] <ELF object>",
662         NULL
663 };
664
665 int main(int argc, const char **argv)
666 {
667         bool no_fail = false;
668         struct object obj = {
669                 .efile = {
670                         .idlist_shndx  = -1,
671                         .symbols_shndx = -1,
672                 },
673                 .structs  = RB_ROOT,
674                 .unions   = RB_ROOT,
675                 .typedefs = RB_ROOT,
676                 .funcs    = RB_ROOT,
677                 .sets     = RB_ROOT,
678         };
679         struct option btfid_options[] = {
680                 OPT_INCR('v', "verbose", &verbose,
681                          "be more verbose (show errors, etc)"),
682                 OPT_STRING(0, "btf", &obj.btf, "BTF data",
683                            "BTF data"),
684                 OPT_BOOLEAN(0, "no-fail", &no_fail,
685                            "do not fail if " BTF_IDS_SECTION " section is not found"),
686                 OPT_END()
687         };
688         int err = -1;
689
690         argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
691                              PARSE_OPT_STOP_AT_NON_OPTION);
692         if (argc != 1)
693                 usage_with_options(resolve_btfids_usage, btfid_options);
694
695         obj.path = argv[0];
696
697         if (elf_collect(&obj))
698                 goto out;
699
700         /*
701          * We did not find .BTF_ids section or symbols section,
702          * nothing to do..
703          */
704         if (obj.efile.idlist_shndx == -1 ||
705             obj.efile.symbols_shndx == -1) {
706                 if (no_fail)
707                         return 0;
708                 pr_err("FAILED to find needed sections\n");
709                 return -1;
710         }
711
712         if (symbols_collect(&obj))
713                 goto out;
714
715         if (symbols_resolve(&obj))
716                 goto out;
717
718         if (symbols_patch(&obj))
719                 goto out;
720
721         err = 0;
722 out:
723         if (obj.efile.elf)
724                 elf_end(obj.efile.elf);
725         close(obj.efile.fd);
726         return err;
727 }