1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * resolve_btfids scans Elf object for .BTF_ids section and resolves
5 * its symbols with BTF ID values.
7 * Each symbol points to 4 bytes data and is expected to have
8 * following name syntax:
10 * __BTF_ID__<type>__<symbol>[__<id>]
14 * func - lookup BTF_KIND_FUNC symbol with <symbol> name
15 * and store its ID into the data:
17 * __BTF_ID__func__vfs_close__1:
20 * struct - lookup BTF_KIND_STRUCT symbol with <symbol> name
21 * and store its ID into the data:
23 * __BTF_ID__struct__sk_buff__1:
26 * union - lookup BTF_KIND_UNION symbol with <symbol> name
27 * and store its ID into the data:
29 * __BTF_ID__union__thread_union__1:
32 * typedef - lookup BTF_KIND_TYPEDEF symbol with <symbol> name
33 * and store its ID into the data:
35 * __BTF_ID__typedef__pid_t__1:
38 * set - store symbol size into first 4 bytes and sort following
41 * __BTF_ID__set__list:
44 * __BTF_ID__func__vfs_getattr__3:
46 * __BTF_ID__func__vfs_fallocate__4:
60 #include <linux/rbtree.h>
61 #include <linux/zalloc.h>
62 #include <linux/err.h>
65 #include <parse-options.h>
67 #define BTF_IDS_SECTION ".BTF_ids"
68 #define BTF_ID "__BTF_ID__"
70 #define BTF_STRUCT "struct"
71 #define BTF_UNION "union"
72 #define BTF_TYPEDEF "typedef"
73 #define BTF_FUNC "func"
79 struct rb_node rb_node;
86 Elf64_Addr addr[ADDR_CNT];
101 unsigned long idlist_addr;
105 struct rb_root structs;
106 struct rb_root unions;
107 struct rb_root typedefs;
108 struct rb_root funcs;
118 int eprintf(int level, int var, const char *fmt, ...)
125 ret = vfprintf(stderr, fmt, args);
132 #define pr_fmt(fmt) fmt
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__)
143 static bool is_btf_id(const char *name)
145 return name && !strncmp(name, BTF_ID, sizeof(BTF_ID) - 1);
148 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
150 struct rb_node *p = root->rb_node;
155 id = rb_entry(p, struct btf_id, rb_node);
156 cmp = strcmp(id->name, name);
167 static struct btf_id*
168 btf_id__add(struct rb_root *root, char *name, bool unique)
170 struct rb_node **p = &root->rb_node;
171 struct rb_node *parent = NULL;
177 id = rb_entry(parent, struct btf_id, rb_node);
178 cmp = strcmp(id->name, name);
184 return unique ? NULL : id;
187 id = zalloc(sizeof(*id));
189 pr_debug("adding symbol %s\n", name);
191 rb_link_node(&id->rb_node, parent, p);
192 rb_insert_color(&id->rb_node, root);
197 static char *get_id(const char *prefix_end)
200 * __BTF_ID__func__vfs_truncate__0
203 char *p, *id = strdup(prefix_end + sizeof("__") - 1);
207 * __BTF_ID__func__vfs_truncate__0
210 * cut the unique id part
212 p = strrchr(id, '_');
223 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
227 id = get_id(name + size);
229 pr_err("FAILED to parse symbol name: %s\n", name);
233 return btf_id__add(root, id, false);
236 static int elf_collect(struct object *obj)
244 fd = open(obj->path, O_RDWR, 0666);
246 pr_err("FAILED cannot open %s: %s\n",
247 obj->path, strerror(errno));
251 elf_version(EV_CURRENT);
253 elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
255 pr_err("FAILED cannot create ELF descriptor: %s\n",
261 obj->efile.elf = elf;
263 elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
265 if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
266 pr_err("FAILED cannot get shdr str ndx\n");
271 * Scan all the elf sections and look for save data
272 * from .BTF_ids section and symbols.
274 while ((scn = elf_nextscn(elf, scn)) != NULL) {
280 if (gelf_getshdr(scn, &sh) != &sh) {
281 pr_err("FAILED get section(%d) header\n", idx);
285 name = elf_strptr(elf, shdrstrndx, sh.sh_name);
287 pr_err("FAILED get section(%d) name\n", idx);
291 data = elf_getdata(scn, 0);
293 pr_err("FAILED to get section(%d) data from %s\n",
298 pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
299 idx, name, (unsigned long) data->d_size,
300 (int) sh.sh_link, (unsigned long) sh.sh_flags,
303 if (sh.sh_type == SHT_SYMTAB) {
304 obj->efile.symbols = data;
305 obj->efile.symbols_shndx = idx;
306 obj->efile.strtabidx = sh.sh_link;
307 } else if (!strcmp(name, BTF_IDS_SECTION)) {
308 obj->efile.idlist = data;
309 obj->efile.idlist_shndx = idx;
310 obj->efile.idlist_addr = sh.sh_addr;
317 static int symbols_collect(struct object *obj)
324 scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
328 if (gelf_getshdr(scn, &sh) != &sh)
331 n = sh.sh_size / sh.sh_entsize;
334 * Scan symbols and look for the ones starting with
335 * __BTF_ID__* over .BTF_ids section.
337 for (i = 0; !err && i < n; i++) {
343 if (!gelf_getsym(obj->efile.symbols, i, &sym))
346 if (sym.st_shndx != obj->efile.idlist_shndx)
349 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
352 if (!is_btf_id(name))
356 * __BTF_ID__TYPE__vfs_truncate__0
359 prefix = name + sizeof(BTF_ID) - 1;
362 if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
364 id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
366 } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
368 id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
370 } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
372 id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
374 } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
376 id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
378 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
379 id = add_symbol(&obj->sets, prefix, sizeof(BTF_SET) - 1);
381 * SET objects store list's count, which is encoded
382 * in symbol's size, together with 'cnt' field hence
386 id->cnt = sym.st_size / sizeof(int) - 1;
388 pr_err("FAILED unsupported prefix %s\n", prefix);
395 if (id->addr_cnt >= ADDR_CNT) {
396 pr_err("FAILED symbol %s crossed the number of allowed lists",
400 id->addr[id->addr_cnt++] = sym.st_value;
406 static int symbols_resolve(struct object *obj)
408 int nr_typedefs = obj->nr_typedefs;
409 int nr_structs = obj->nr_structs;
410 int nr_unions = obj->nr_unions;
411 int nr_funcs = obj->nr_funcs;
416 btf = btf__parse(obj->btf ?: obj->path, NULL);
417 err = libbpf_get_error(btf);
419 pr_err("FAILED: load BTF from %s: %s",
420 obj->path, strerror(err));
425 nr = btf__get_nr_types(btf);
428 * Iterate all the BTF types and search for collected symbol IDs.
430 for (type_id = 1; type_id <= nr; type_id++) {
431 const struct btf_type *type;
432 struct rb_root *root;
437 type = btf__type_by_id(btf, type_id);
439 pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
444 if (btf_is_func(type) && nr_funcs) {
447 } else if (btf_is_struct(type) && nr_structs) {
449 root = &obj->structs;
450 } else if (btf_is_union(type) && nr_unions) {
453 } else if (btf_is_typedef(type) && nr_typedefs) {
455 root = &obj->typedefs;
459 str = btf__name_by_offset(btf, type->name_off);
461 pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
466 id = btf_id__find(root, str);
479 static int id_patch(struct object *obj, struct btf_id *id)
481 Elf_Data *data = obj->efile.idlist;
482 int *ptr = data->d_buf;
486 pr_err("FAILED unresolved symbol %s\n", id->name);
490 for (i = 0; i < id->addr_cnt; i++) {
491 unsigned long addr = id->addr[i];
492 unsigned long idx = addr - obj->efile.idlist_addr;
494 pr_debug("patching addr %5lu: ID %7d [%s]\n",
495 idx, id->id, id->name);
497 if (idx >= data->d_size) {
498 pr_err("FAILED patching index %lu out of bounds %lu\n",
503 idx = idx / sizeof(int);
510 static int __symbols_patch(struct object *obj, struct rb_root *root)
512 struct rb_node *next;
515 next = rb_first(root);
517 id = rb_entry(next, struct btf_id, rb_node);
519 if (id_patch(obj, id))
522 next = rb_next(next);
527 static int cmp_id(const void *pa, const void *pb)
529 const int *a = pa, *b = pb;
534 static int sets_patch(struct object *obj)
536 Elf_Data *data = obj->efile.idlist;
537 int *ptr = data->d_buf;
538 struct rb_node *next;
540 next = rb_first(&obj->sets);
542 unsigned long addr, idx;
547 id = rb_entry(next, struct btf_id, rb_node);
549 idx = addr - obj->efile.idlist_addr;
551 /* sets are unique */
552 if (id->addr_cnt != 1) {
553 pr_err("FAILED malformed data for set '%s'\n",
558 idx = idx / sizeof(int);
559 base = &ptr[idx] + 1;
562 pr_debug("sorting addr %5lu: cnt %6d [%s]\n",
563 (idx + 1) * sizeof(int), cnt, id->name);
565 qsort(base, cnt, sizeof(int), cmp_id);
567 next = rb_next(next);
572 static int symbols_patch(struct object *obj)
576 if (__symbols_patch(obj, &obj->structs) ||
577 __symbols_patch(obj, &obj->unions) ||
578 __symbols_patch(obj, &obj->typedefs) ||
579 __symbols_patch(obj, &obj->funcs) ||
580 __symbols_patch(obj, &obj->sets))
586 elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
588 err = elf_update(obj->efile.elf, ELF_C_WRITE);
590 pr_err("FAILED elf_update(WRITE): %s\n",
594 pr_debug("update %s for %s\n",
595 err >= 0 ? "ok" : "failed", obj->path);
596 return err < 0 ? -1 : 0;
599 static const char * const resolve_btfids_usage[] = {
600 "resolve_btfids [<options>] <ELF object>",
604 int main(int argc, const char **argv)
606 bool no_fail = false;
607 struct object obj = {
618 struct option btfid_options[] = {
619 OPT_INCR('v', "verbose", &verbose,
620 "be more verbose (show errors, etc)"),
621 OPT_STRING(0, "btf", &obj.btf, "BTF data",
623 OPT_BOOLEAN(0, "no-fail", &no_fail,
624 "do not fail if " BTF_IDS_SECTION " section is not found"),
629 argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
630 PARSE_OPT_STOP_AT_NON_OPTION);
632 usage_with_options(resolve_btfids_usage, btfid_options);
636 if (elf_collect(&obj))
640 * We did not find .BTF_ids section or symbols section,
643 if (obj.efile.idlist_shndx == -1 ||
644 obj.efile.symbols_shndx == -1) {
647 pr_err("FAILED to find needed sections\n");
651 if (symbols_collect(&obj))
654 if (symbols_resolve(&obj))
657 if (symbols_patch(&obj))
663 elf_end(obj.efile.elf);