main.c 17 KB

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