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 */ 203 char *p, *id = strdup(prefix_end + sizeof("__") - 1); 204 205 if (id) { 206 /* 207 * __BTF_ID__func__vfs_truncate__0 208 * id = ^ 209 * 210 * cut the unique id part 211 */ 212 p = strrchr(id, '_'); 213 p--; 214 if (*p != '_') { 215 free(id); 216 return NULL; 217 } 218 *p = '\0'; 219 } 220 return id; 221 } 222 223 static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size) 224 { 225 char *id; 226 227 id = get_id(name + size); 228 if (!id) { 229 pr_err("FAILED to parse symbol name: %s\n", name); 230 return NULL; 231 } 232 233 return btf_id__add(root, id, false); 234 } 235 236 static int elf_collect(struct object *obj) 237 { 238 Elf_Scn *scn = NULL; 239 size_t shdrstrndx; 240 int idx = 0; 241 Elf *elf; 242 int fd; 243 244 fd = open(obj->path, O_RDWR, 0666); 245 if (fd == -1) { 246 pr_err("FAILED cannot open %s: %s\n", 247 obj->path, strerror(errno)); 248 return -1; 249 } 250 251 elf_version(EV_CURRENT); 252 253 elf = elf_begin(fd, ELF_C_RDWR_MMAP, NULL); 254 if (!elf) { 255 pr_err("FAILED cannot create ELF descriptor: %s\n", 256 elf_errmsg(-1)); 257 return -1; 258 } 259 260 obj->efile.fd = fd; 261 obj->efile.elf = elf; 262 263 elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT); 264 265 if (elf_getshdrstrndx(elf, &shdrstrndx) != 0) { 266 pr_err("FAILED cannot get shdr str ndx\n"); 267 return -1; 268 } 269 270 /* 271 * Scan all the elf sections and look for save data 272 * from .BTF_ids section and symbols. 273 */ 274 while ((scn = elf_nextscn(elf, scn)) != NULL) { 275 Elf_Data *data; 276 GElf_Shdr sh; 277 char *name; 278 279 idx++; 280 if (gelf_getshdr(scn, &sh) != &sh) { 281 pr_err("FAILED get section(%d) header\n", idx); 282 return -1; 283 } 284 285 name = elf_strptr(elf, shdrstrndx, sh.sh_name); 286 if (!name) { 287 pr_err("FAILED get section(%d) name\n", idx); 288 return -1; 289 } 290 291 data = elf_getdata(scn, 0); 292 if (!data) { 293 pr_err("FAILED to get section(%d) data from %s\n", 294 idx, name); 295 return -1; 296 } 297 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, 301 (int) sh.sh_type); 302 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; 311 } 312 } 313 314 return 0; 315 } 316 317 static int symbols_collect(struct object *obj) 318 { 319 Elf_Scn *scn = NULL; 320 int n, i, err = 0; 321 GElf_Shdr sh; 322 char *name; 323 324 scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx); 325 if (!scn) 326 return -1; 327 328 if (gelf_getshdr(scn, &sh) != &sh) 329 return -1; 330 331 n = sh.sh_size / sh.sh_entsize; 332 333 /* 334 * Scan symbols and look for the ones starting with 335 * __BTF_ID__* over .BTF_ids section. 336 */ 337 for (i = 0; !err && i < n; i++) { 338 char *tmp, *prefix; 339 struct btf_id *id; 340 GElf_Sym sym; 341 int err = -1; 342 343 if (!gelf_getsym(obj->efile.symbols, i, &sym)) 344 return -1; 345 346 if (sym.st_shndx != obj->efile.idlist_shndx) 347 continue; 348 349 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, 350 sym.st_name); 351 352 if (!is_btf_id(name)) 353 continue; 354 355 /* 356 * __BTF_ID__TYPE__vfs_truncate__0 357 * prefix = ^ 358 */ 359 prefix = name + sizeof(BTF_ID) - 1; 360 361 /* struct */ 362 if (!strncmp(prefix, BTF_STRUCT, sizeof(BTF_STRUCT) - 1)) { 363 obj->nr_structs++; 364 id = add_symbol(&obj->structs, prefix, sizeof(BTF_STRUCT) - 1); 365 /* union */ 366 } else if (!strncmp(prefix, BTF_UNION, sizeof(BTF_UNION) - 1)) { 367 obj->nr_unions++; 368 id = add_symbol(&obj->unions, prefix, sizeof(BTF_UNION) - 1); 369 /* typedef */ 370 } else if (!strncmp(prefix, BTF_TYPEDEF, sizeof(BTF_TYPEDEF) - 1)) { 371 obj->nr_typedefs++; 372 id = add_symbol(&obj->typedefs, prefix, sizeof(BTF_TYPEDEF) - 1); 373 /* func */ 374 } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) { 375 obj->nr_funcs++; 376 id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1); 377 /* set */ 378 } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) { 379 id = add_symbol(&obj->sets, prefix, sizeof(BTF_SET) - 1); 380 /* 381 * SET objects store list's count, which is encoded 382 * in symbol's size, together with 'cnt' field hence 383 * that - 1. 384 */ 385 if (id) 386 id->cnt = sym.st_size / sizeof(int) - 1; 387 } else { 388 pr_err("FAILED unsupported prefix %s\n", prefix); 389 return -1; 390 } 391 392 if (!id) 393 return -ENOMEM; 394 395 if (id->addr_cnt >= ADDR_CNT) { 396 pr_err("FAILED symbol %s crossed the number of allowed lists", 397 id->name); 398 return -1; 399 } 400 id->addr[id->addr_cnt++] = sym.st_value; 401 } 402 403 return 0; 404 } 405 406 static struct btf *btf__parse_raw(const char *file) 407 { 408 struct btf *btf; 409 struct stat st; 410 __u8 *buf; 411 FILE *f; 412 413 if (stat(file, &st)) 414 return NULL; 415 416 f = fopen(file, "rb"); 417 if (!f) 418 return NULL; 419 420 buf = malloc(st.st_size); 421 if (!buf) { 422 btf = ERR_PTR(-ENOMEM); 423 goto exit_close; 424 } 425 426 if ((size_t) st.st_size != fread(buf, 1, st.st_size, f)) { 427 btf = ERR_PTR(-EINVAL); 428 goto exit_free; 429 } 430 431 btf = btf__new(buf, st.st_size); 432 433 exit_free: 434 free(buf); 435 exit_close: 436 fclose(f); 437 return btf; 438 } 439 440 static bool is_btf_raw(const char *file) 441 { 442 __u16 magic = 0; 443 int fd, nb_read; 444 445 fd = open(file, O_RDONLY); 446 if (fd < 0) 447 return false; 448 449 nb_read = read(fd, &magic, sizeof(magic)); 450 close(fd); 451 return nb_read == sizeof(magic) && magic == BTF_MAGIC; 452 } 453 454 static struct btf *btf_open(const char *path) 455 { 456 if (is_btf_raw(path)) 457 return btf__parse_raw(path); 458 else 459 return btf__parse_elf(path, NULL); 460 } 461 462 static int symbols_resolve(struct object *obj) 463 { 464 int nr_typedefs = obj->nr_typedefs; 465 int nr_structs = obj->nr_structs; 466 int nr_unions = obj->nr_unions; 467 int nr_funcs = obj->nr_funcs; 468 int err, type_id; 469 struct btf *btf; 470 __u32 nr; 471 472 btf = btf_open(obj->btf ?: obj->path); 473 err = libbpf_get_error(btf); 474 if (err) { 475 pr_err("FAILED: load BTF from %s: %s", 476 obj->path, strerror(err)); 477 return -1; 478 } 479 480 err = -1; 481 nr = btf__get_nr_types(btf); 482 483 /* 484 * Iterate all the BTF types and search for collected symbol IDs. 485 */ 486 for (type_id = 1; type_id <= nr; type_id++) { 487 const struct btf_type *type; 488 struct rb_root *root; 489 struct btf_id *id; 490 const char *str; 491 int *nr; 492 493 type = btf__type_by_id(btf, type_id); 494 if (!type) { 495 pr_err("FAILED: malformed BTF, can't resolve type for ID %d\n", 496 type_id); 497 goto out; 498 } 499 500 if (btf_is_func(type) && nr_funcs) { 501 nr = &nr_funcs; 502 root = &obj->funcs; 503 } else if (btf_is_struct(type) && nr_structs) { 504 nr = &nr_structs; 505 root = &obj->structs; 506 } else if (btf_is_union(type) && nr_unions) { 507 nr = &nr_unions; 508 root = &obj->unions; 509 } else if (btf_is_typedef(type) && nr_typedefs) { 510 nr = &nr_typedefs; 511 root = &obj->typedefs; 512 } else 513 continue; 514 515 str = btf__name_by_offset(btf, type->name_off); 516 if (!str) { 517 pr_err("FAILED: malformed BTF, can't resolve name for ID %d\n", 518 type_id); 519 goto out; 520 } 521 522 id = btf_id__find(root, str); 523 if (id) { 524 id->id = type_id; 525 (*nr)--; 526 } 527 } 528 529 err = 0; 530 out: 531 btf__free(btf); 532 return err; 533 } 534 535 static int id_patch(struct object *obj, struct btf_id *id) 536 { 537 Elf_Data *data = obj->efile.idlist; 538 int *ptr = data->d_buf; 539 int i; 540 541 if (!id->id) { 542 pr_err("FAILED unresolved symbol %s\n", id->name); 543 return -EINVAL; 544 } 545 546 for (i = 0; i < id->addr_cnt; i++) { 547 unsigned long addr = id->addr[i]; 548 unsigned long idx = addr - obj->efile.idlist_addr; 549 550 pr_debug("patching addr %5lu: ID %7d [%s]\n", 551 idx, id->id, id->name); 552 553 if (idx >= data->d_size) { 554 pr_err("FAILED patching index %lu out of bounds %lu\n", 555 idx, data->d_size); 556 return -1; 557 } 558 559 idx = idx / sizeof(int); 560 ptr[idx] = id->id; 561 } 562 563 return 0; 564 } 565 566 static int __symbols_patch(struct object *obj, struct rb_root *root) 567 { 568 struct rb_node *next; 569 struct btf_id *id; 570 571 next = rb_first(root); 572 while (next) { 573 id = rb_entry(next, struct btf_id, rb_node); 574 575 if (id_patch(obj, id)) 576 return -1; 577 578 next = rb_next(next); 579 } 580 return 0; 581 } 582 583 static int cmp_id(const void *pa, const void *pb) 584 { 585 const int *a = pa, *b = pb; 586 587 return *a - *b; 588 } 589 590 static int sets_patch(struct object *obj) 591 { 592 Elf_Data *data = obj->efile.idlist; 593 int *ptr = data->d_buf; 594 struct rb_node *next; 595 596 next = rb_first(&obj->sets); 597 while (next) { 598 unsigned long addr, idx; 599 struct btf_id *id; 600 int *base; 601 int cnt; 602 603 id = rb_entry(next, struct btf_id, rb_node); 604 addr = id->addr[0]; 605 idx = addr - obj->efile.idlist_addr; 606 607 /* sets are unique */ 608 if (id->addr_cnt != 1) { 609 pr_err("FAILED malformed data for set '%s'\n", 610 id->name); 611 return -1; 612 } 613 614 idx = idx / sizeof(int); 615 base = &ptr[idx] + 1; 616 cnt = ptr[idx]; 617 618 pr_debug("sorting addr %5lu: cnt %6d [%s]\n", 619 (idx + 1) * sizeof(int), cnt, id->name); 620 621 qsort(base, cnt, sizeof(int), cmp_id); 622 623 next = rb_next(next); 624 } 625 } 626 627 static int symbols_patch(struct object *obj) 628 { 629 int err; 630 631 if (__symbols_patch(obj, &obj->structs) || 632 __symbols_patch(obj, &obj->unions) || 633 __symbols_patch(obj, &obj->typedefs) || 634 __symbols_patch(obj, &obj->funcs) || 635 __symbols_patch(obj, &obj->sets)) 636 return -1; 637 638 if (sets_patch(obj)) 639 return -1; 640 641 elf_flagdata(obj->efile.idlist, ELF_C_SET, ELF_F_DIRTY); 642 643 err = elf_update(obj->efile.elf, ELF_C_WRITE); 644 if (err < 0) { 645 pr_err("FAILED elf_update(WRITE): %s\n", 646 elf_errmsg(-1)); 647 } 648 649 pr_debug("update %s for %s\n", 650 err >= 0 ? "ok" : "failed", obj->path); 651 return err < 0 ? -1 : 0; 652 } 653 654 static const char * const resolve_btfids_usage[] = { 655 "resolve_btfids [<options>] <ELF object>", 656 NULL 657 }; 658 659 int main(int argc, const char **argv) 660 { 661 bool no_fail = false; 662 struct object obj = { 663 .efile = { 664 .idlist_shndx = -1, 665 .symbols_shndx = -1, 666 }, 667 .structs = RB_ROOT, 668 .unions = RB_ROOT, 669 .typedefs = RB_ROOT, 670 .funcs = RB_ROOT, 671 .sets = RB_ROOT, 672 }; 673 struct option btfid_options[] = { 674 OPT_INCR('v', "verbose", &verbose, 675 "be more verbose (show errors, etc)"), 676 OPT_STRING(0, "btf", &obj.btf, "BTF data", 677 "BTF data"), 678 OPT_BOOLEAN(0, "no-fail", &no_fail, 679 "do not fail if " BTF_IDS_SECTION " section is not found"), 680 OPT_END() 681 }; 682 int err = -1; 683 684 argc = parse_options(argc, argv, btfid_options, resolve_btfids_usage, 685 PARSE_OPT_STOP_AT_NON_OPTION); 686 if (argc != 1) 687 usage_with_options(resolve_btfids_usage, btfid_options); 688 689 obj.path = argv[0]; 690 691 if (elf_collect(&obj)) 692 goto out; 693 694 /* 695 * We did not find .BTF_ids section or symbols section, 696 * nothing to do.. 697 */ 698 if (obj.efile.idlist_shndx == -1 || 699 obj.efile.symbols_shndx == -1) { 700 if (no_fail) 701 return 0; 702 pr_err("FAILED to find needed sections\n"); 703 return -1; 704 } 705 706 if (symbols_collect(&obj)) 707 goto out; 708 709 if (symbols_resolve(&obj)) 710 goto out; 711 712 if (symbols_patch(&obj)) 713 goto out; 714 715 err = 0; 716 out: 717 if (obj.efile.elf) 718 elf_end(obj.efile.elf); 719 close(obj.efile.fd); 720 return err; 721 } 722