main.c 18 KB

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