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:
49 * set8 - store symbol size into first 4 bytes and sort following
52 * __BTF_ID__set8__list:
55 * __BTF_ID__func__vfs_getattr__3:
57 * .word (1 << 0) | (1 << 2)
58 * __BTF_ID__func__vfs_fallocate__5:
60 * .word (1 << 3) | (1 << 1) | (1 << 2)
73 #include <linux/btf_ids.h>
74 #include <linux/rbtree.h>
75 #include <linux/zalloc.h>
76 #include <linux/err.h>
78 #include <bpf/libbpf.h>
79 #include <subcmd/parse-options.h>
81 #define BTF_IDS_SECTION ".BTF_ids"
82 #define BTF_ID_PREFIX "__BTF_ID__"
84 #define BTF_STRUCT "struct"
85 #define BTF_UNION "union"
86 #define BTF_TYPEDEF "typedef"
87 #define BTF_FUNC "func"
89 #define BTF_SET8 "set8"
93 #if __BYTE_ORDER == __LITTLE_ENDIAN
94 # define ELFDATANATIVE ELFDATA2LSB
95 #elif __BYTE_ORDER == __BIG_ENDIAN
96 # define ELFDATANATIVE ELFDATA2MSB
98 # error "Unknown machine endianness!"
102 struct rb_node rb_node;
111 Elf64_Addr addr[ADDR_CNT];
117 const char *base_btf_path;
127 unsigned long idlist_addr;
132 struct rb_root structs;
133 struct rb_root unions;
134 struct rb_root typedefs;
135 struct rb_root funcs;
146 static int eprintf(int level, int var, const char *fmt, ...)
153 ret = vfprintf(stderr, fmt, args);
160 #define pr_fmt(fmt) fmt
163 #define pr_debug(fmt, ...) \
164 eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__)
165 #define pr_debugN(n, fmt, ...) \
166 eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__)
167 #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
168 #define pr_err(fmt, ...) \
169 eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
170 #define pr_info(fmt, ...) \
171 eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__)
173 static bool is_btf_id(const char *name)
175 return name && !strncmp(name, BTF_ID_PREFIX, sizeof(BTF_ID_PREFIX) - 1);
178 static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
180 struct rb_node *p = root->rb_node;
185 id = rb_entry(p, struct btf_id, rb_node);
186 cmp = strcmp(id->name, name);
197 static struct btf_id *
198 btf_id__add(struct rb_root *root, char *name, bool unique)
200 struct rb_node **p = &root->rb_node;
201 struct rb_node *parent = NULL;
207 id = rb_entry(parent, struct btf_id, rb_node);
208 cmp = strcmp(id->name, name);
214 return unique ? NULL : id;
217 id = zalloc(sizeof(*id));
219 pr_debug("adding symbol %s\n", name);
221 rb_link_node(&id->rb_node, parent, p);
222 rb_insert_color(&id->rb_node, root);
227 static char *get_id(const char *prefix_end)
230 * __BTF_ID__func__vfs_truncate__0
234 int len = strlen(prefix_end);
235 int pos = sizeof("__") - 1;
241 id = strdup(prefix_end + pos);
244 * __BTF_ID__func__vfs_truncate__0
247 * cut the unique id part
249 p = strrchr(id, '_');
260 static struct btf_id *add_set(struct object *obj, char *name, bool is_set8)
263 * __BTF_ID__set__name
267 char *id = name + (is_set8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__")) - 1;
268 int len = strlen(name);
270 if (id >= name + len) {
271 pr_err("FAILED to parse set name: %s\n", name);
275 return btf_id__add(&obj->sets, id, true);
278 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
282 id = get_id(name + size);
284 pr_err("FAILED to parse symbol name: %s\n", name);
288 return btf_id__add(root, id, false);
291 /* Older libelf.h and glibc elf.h might not yet define the ELF compression types. */
292 #ifndef SHF_COMPRESSED
293 #define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */
297 * The data of compressed section should be aligned to 4
298 * (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
299 * sets sh_addralign to 1, which makes libelf fail with
300 * misaligned section error during the update:
301 * FAILED elf_update(WRITE): invalid section alignment
303 * While waiting for ld fix, we fix the compressed sections
304 * sh_addralign value manualy.
306 static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
308 int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
310 if (!(sh->sh_flags & SHF_COMPRESSED))
313 if (sh->sh_addralign == expected)
316 pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
317 sh->sh_addralign, expected);
319 sh->sh_addralign = expected;
321 if (gelf_update_shdr(scn, sh) == 0) {
322 pr_err("FAILED cannot update section header: %s\n",
329 static int elf_collect(struct object *obj)
338 fd = open(obj->path, O_RDWR, 0666);
340 pr_err("FAILED cannot open %s: %s\n",
341 obj->path, strerror(errno));
345 elf_version(EV_CURRENT);
347 elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL);
350 pr_err("FAILED cannot create ELF descriptor: %s\n",
356 obj->efile.elf = elf;
358 elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
360 if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) {
361 pr_err("FAILED cannot get shdr str ndx\n");
365 if (gelf_getehdr(obj->efile.elf, &ehdr) == NULL) {
366 pr_err("FAILED cannot get ELF header: %s\n",
370 obj->efile.encoding = ehdr.e_ident[EI_DATA];
373 * Scan all the elf sections and look for save data
374 * from .BTF_ids section and symbols.
376 while ((scn = elf_nextscn(elf, scn)) != NULL) {
382 if (gelf_getshdr(scn, &sh) != &sh) {
383 pr_err("FAILED get section(%d) header\n", idx);
387 name = elf_strptr(elf, shdrstrndx, sh.sh_name);
389 pr_err("FAILED get section(%d) name\n", idx);
393 data = elf_getdata(scn, 0);
395 pr_err("FAILED to get section(%d) data from %s\n",
400 pr_debug2("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
401 idx, name, (unsigned long) data->d_size,
402 (int) sh.sh_link, (unsigned long) sh.sh_flags,
405 if (sh.sh_type == SHT_SYMTAB) {
406 obj->efile.symbols = data;
407 obj->efile.symbols_shndx = idx;
408 obj->efile.strtabidx = sh.sh_link;
409 } else if (!strcmp(name, BTF_IDS_SECTION)) {
410 obj->efile.idlist = data;
411 obj->efile.idlist_shndx = idx;
412 obj->efile.idlist_addr = sh.sh_addr;
413 } else if (!strcmp(name, BTF_BASE_ELF_SEC)) {
414 /* If a .BTF.base section is found, do not resolve
415 * BTF ids relative to vmlinux; resolve relative
416 * to the .BTF.base section instead. btf__parse_split()
417 * will take care of this once the base BTF it is
420 obj->base_btf_path = NULL;
423 if (compressed_section_fix(elf, scn, &sh))
430 static int symbols_collect(struct object *obj)
437 scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
441 if (gelf_getshdr(scn, &sh) != &sh)
444 n = sh.sh_size / sh.sh_entsize;
447 * Scan symbols and look for the ones starting with
448 * __BTF_ID__* over .BTF_ids section.
450 for (i = 0; i < n; i++) {
455 if (!gelf_getsym(obj->efile.symbols, i, &sym))
458 if (sym.st_shndx != obj->efile.idlist_shndx)
461 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
464 if (!is_btf_id(name))
468 * __BTF_ID__TYPE__vfs_truncate__0
471 prefix = name + sizeof(BTF_ID_PREFIX) - 1;
474 if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) {
476 id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1);
478 } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) {
480 id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1);
482 } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) {
484 id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1);
486 } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
488 id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
490 } else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
491 id = add_set(obj, prefix, true);
493 * SET8 objects store list's count, which is encoded
494 * in symbol's size, together with 'cnt' field hence
498 id->cnt = sym.st_size / sizeof(uint64_t) - 1;
502 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
503 id = add_set(obj, prefix, false);
505 * SET objects store list's count, which is encoded
506 * in symbol's size, together with 'cnt' field hence
510 id->cnt = sym.st_size / sizeof(int) - 1;
514 pr_err("FAILED unsupported prefix %s\n", prefix);
521 if (id->addr_cnt >= ADDR_CNT) {
522 pr_err("FAILED symbol %s crossed the number of allowed lists\n",
526 id->addr[id->addr_cnt++] = sym.st_value;
532 static int symbols_resolve(struct object *obj)
534 int nr_typedefs = obj->nr_typedefs;
535 int nr_structs = obj->nr_structs;
536 int nr_unions = obj->nr_unions;
537 int nr_funcs = obj->nr_funcs;
538 struct btf *base_btf = NULL;
543 if (obj->base_btf_path) {
544 base_btf = btf__parse(obj->base_btf_path, NULL);
545 err = libbpf_get_error(base_btf);
547 pr_err("FAILED: load base BTF from %s: %s\n",
548 obj->base_btf_path, strerror(-err));
553 btf = btf__parse_split(obj->btf ?: obj->path, base_btf);
554 err = libbpf_get_error(btf);
556 pr_err("FAILED: load BTF from %s: %s\n",
557 obj->btf ?: obj->path, strerror(-err));
562 nr_types = btf__type_cnt(btf);
565 * Iterate all the BTF types and search for collected symbol IDs.
567 for (type_id = 1; type_id < nr_types; type_id++) {
568 const struct btf_type *type;
569 struct rb_root *root;
574 type = btf__type_by_id(btf, type_id);
576 pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n",
581 if (btf_is_func(type) && nr_funcs) {
584 } else if (btf_is_struct(type) && nr_structs) {
586 root = &obj->structs;
587 } else if (btf_is_union(type) && nr_unions) {
590 } else if (btf_is_typedef(type) && nr_typedefs) {
592 root = &obj->typedefs;
596 str = btf__name_by_offset(btf, type->name_off);
598 pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n",
603 id = btf_id__find(root, str);
606 pr_info("WARN: multiple IDs found for '%s': %d, %d - using %d\n",
607 str, id->id, type_id, id->id);
623 static int id_patch(struct object *obj, struct btf_id *id)
625 Elf_Data *data = obj->efile.idlist;
626 int *ptr = data->d_buf;
629 /* For set, set8, id->id may be 0 */
630 if (!id->id && !id->is_set && !id->is_set8) {
631 pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
635 for (i = 0; i < id->addr_cnt; i++) {
636 unsigned long addr = id->addr[i];
637 unsigned long idx = addr - obj->efile.idlist_addr;
639 pr_debug("patching addr %5lu: ID %7d [%s]\n",
640 idx, id->id, id->name);
642 if (idx >= data->d_size) {
643 pr_err("FAILED patching index %lu out of bounds %lu\n",
648 idx = idx / sizeof(int);
655 static int __symbols_patch(struct object *obj, struct rb_root *root)
657 struct rb_node *next;
660 next = rb_first(root);
662 id = rb_entry(next, struct btf_id, rb_node);
664 if (id_patch(obj, id))
667 next = rb_next(next);
672 static int cmp_id(const void *pa, const void *pb)
674 const int *a = pa, *b = pb;
679 static int sets_patch(struct object *obj)
681 Elf_Data *data = obj->efile.idlist;
682 struct rb_node *next;
684 next = rb_first(&obj->sets);
686 struct btf_id_set8 *set8 = NULL;
687 struct btf_id_set *set = NULL;
688 unsigned long addr, off;
691 id = rb_entry(next, struct btf_id, rb_node);
693 off = addr - obj->efile.idlist_addr;
695 /* sets are unique */
696 if (id->addr_cnt != 1) {
697 pr_err("FAILED malformed data for set '%s'\n",
703 set = data->d_buf + off;
704 qsort(set->ids, set->cnt, sizeof(set->ids[0]), cmp_id);
706 set8 = data->d_buf + off;
708 * Make sure id is at the beginning of the pairs
709 * struct, otherwise the below qsort would not work.
711 BUILD_BUG_ON((u32 *)set8->pairs != &set8->pairs[0].id);
712 qsort(set8->pairs, set8->cnt, sizeof(set8->pairs[0]), cmp_id);
715 * When ELF endianness does not match endianness of the
716 * host, libelf will do the translation when updating
717 * the ELF. This, however, corrupts SET8 flags which are
718 * already in the target endianness. So, let's bswap
719 * them to the host endianness and libelf will then
720 * correctly translate everything.
722 if (obj->efile.encoding != ELFDATANATIVE) {
725 set8->flags = bswap_32(set8->flags);
726 for (i = 0; i < set8->cnt; i++) {
727 set8->pairs[i].flags =
728 bswap_32(set8->pairs[i].flags);
733 pr_debug("sorting addr %5lu: cnt %6d [%s]\n",
734 off, id->is_set ? set->cnt : set8->cnt, id->name);
736 next = rb_next(next);
741 static int symbols_patch(struct object *obj)
745 if (__symbols_patch(obj, &obj->structs) ||
746 __symbols_patch(obj, &obj->unions) ||
747 __symbols_patch(obj, &obj->typedefs) ||
748 __symbols_patch(obj, &obj->funcs) ||
749 __symbols_patch(obj, &obj->sets))
755 /* Set type to ensure endian translation occurs. */
756 obj->efile.idlist->d_type = ELF_T_WORD;
758 elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY);
760 err = elf_update(obj->efile.elf, ELF_C_WRITE);
762 pr_err("FAILED elf_update(WRITE): %s\n",
766 pr_debug("update %s for %s\n",
767 err >= 0 ? "ok" : "failed", obj->path);
768 return err < 0 ? -1 : 0;
771 static const char * const resolve_btfids_usage[] = {
772 "resolve_btfids [<options>] <ELF object>",
776 int main(int argc, const char **argv)
778 struct object obj = {
789 bool fatal_warnings = false;
790 struct option btfid_options[] = {
791 OPT_INCR('v', "verbose", &verbose,
792 "be more verbose (show errors, etc)"),
793 OPT_STRING(0, "btf", &obj.btf, "BTF data",
795 OPT_STRING('b', "btf_base", &obj.base_btf_path, "file",
796 "path of file providing base BTF"),
797 OPT_BOOLEAN(0, "fatal_warnings", &fatal_warnings,
798 "turn warnings into errors"),
803 argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage,
804 PARSE_OPT_STOP_AT_NON_OPTION);
806 usage_with_options(resolve_btfids_usage, btfid_options);
810 if (elf_collect(&obj))
814 * We did not find .BTF_ids section or symbols section,
817 if (obj.efile.idlist_shndx == -1 ||
818 obj.efile.symbols_shndx == -1) {
819 pr_debug("Cannot find .BTF_ids or symbols sections, nothing to do\n");
824 if (symbols_collect(&obj))
827 if (symbols_resolve(&obj))
830 if (symbols_patch(&obj))
833 if (!(fatal_warnings && warnings))
837 elf_end(obj.efile.elf);