jit_disasm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /*
  3. * Based on:
  4. *
  5. * Minimal BPF JIT image disassembler
  6. *
  7. * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
  8. * debugging or verification purposes.
  9. *
  10. * Copyright 2013 Daniel Borkmann <daniel@iogearbox.net>
  11. * Licensed under the GNU General Public License, version 2.0 (GPLv2)
  12. */
  13. #ifndef _GNU_SOURCE
  14. #define _GNU_SOURCE
  15. #endif
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <limits.h>
  24. #include <bpf/libbpf.h>
  25. #ifdef HAVE_LLVM_SUPPORT
  26. #include <llvm-c/Core.h>
  27. #include <llvm-c/Disassembler.h>
  28. #include <llvm-c/Target.h>
  29. #include <llvm-c/TargetMachine.h>
  30. #endif
  31. #ifdef HAVE_LIBBFD_SUPPORT
  32. #include <bfd.h>
  33. #include <dis-asm.h>
  34. #include <tools/dis-asm-compat.h>
  35. #endif
  36. #include "json_writer.h"
  37. #include "main.h"
  38. static int oper_count;
  39. #ifdef HAVE_LLVM_SUPPORT
  40. #define DISASM_SPACER
  41. typedef LLVMDisasmContextRef disasm_ctx_t;
  42. static int printf_json(char *s)
  43. {
  44. s = strtok(s, " \t");
  45. jsonw_string_field(json_wtr, "operation", s);
  46. jsonw_name(json_wtr, "operands");
  47. jsonw_start_array(json_wtr);
  48. oper_count = 1;
  49. while ((s = strtok(NULL, " \t,()")) != 0) {
  50. jsonw_string(json_wtr, s);
  51. oper_count++;
  52. }
  53. return 0;
  54. }
  55. /* This callback to set the ref_type is necessary to have the LLVM disassembler
  56. * print PC-relative addresses instead of byte offsets for branch instruction
  57. * targets.
  58. */
  59. static const char *
  60. symbol_lookup_callback(__maybe_unused void *disasm_info,
  61. __maybe_unused uint64_t ref_value,
  62. uint64_t *ref_type, __maybe_unused uint64_t ref_PC,
  63. __maybe_unused const char **ref_name)
  64. {
  65. *ref_type = LLVMDisassembler_ReferenceType_InOut_None;
  66. return NULL;
  67. }
  68. static int
  69. init_context(disasm_ctx_t *ctx, const char *arch,
  70. __maybe_unused const char *disassembler_options,
  71. __maybe_unused unsigned char *image, __maybe_unused ssize_t len,
  72. __maybe_unused __u64 func_ksym)
  73. {
  74. char *triple;
  75. if (arch)
  76. triple = LLVMNormalizeTargetTriple(arch);
  77. else
  78. triple = LLVMGetDefaultTargetTriple();
  79. if (!triple) {
  80. p_err("Failed to retrieve triple");
  81. return -1;
  82. }
  83. *ctx = LLVMCreateDisasm(triple, NULL, 0, NULL, symbol_lookup_callback);
  84. LLVMDisposeMessage(triple);
  85. if (!*ctx) {
  86. p_err("Failed to create disassembler");
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static void destroy_context(disasm_ctx_t *ctx)
  92. {
  93. LLVMDisposeMessage(*ctx);
  94. }
  95. static int
  96. disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc,
  97. __u64 func_ksym)
  98. {
  99. char buf[256];
  100. int count;
  101. count = LLVMDisasmInstruction(*ctx, image + pc, len - pc, func_ksym + pc,
  102. buf, sizeof(buf));
  103. if (json_output)
  104. printf_json(buf);
  105. else
  106. printf("%s", buf);
  107. return count;
  108. }
  109. int disasm_init(void)
  110. {
  111. LLVMInitializeAllTargetInfos();
  112. LLVMInitializeAllTargetMCs();
  113. LLVMInitializeAllDisassemblers();
  114. return 0;
  115. }
  116. #endif /* HAVE_LLVM_SUPPORT */
  117. #ifdef HAVE_LIBBFD_SUPPORT
  118. #define DISASM_SPACER "\t"
  119. struct disasm_info {
  120. struct disassemble_info info;
  121. __u64 func_ksym;
  122. };
  123. static void disasm_print_addr(bfd_vma addr, struct disassemble_info *info)
  124. {
  125. struct disasm_info *dinfo = container_of(info, struct disasm_info, info);
  126. addr += dinfo->func_ksym;
  127. generic_print_address(addr, info);
  128. }
  129. typedef struct {
  130. struct disasm_info *info;
  131. disassembler_ftype disassemble;
  132. bfd *bfdf;
  133. } disasm_ctx_t;
  134. static int get_exec_path(char *tpath, size_t size)
  135. {
  136. const char *path = "/proc/self/exe";
  137. ssize_t len;
  138. len = readlink(path, tpath, size - 1);
  139. if (len <= 0)
  140. return -1;
  141. tpath[len] = 0;
  142. return 0;
  143. }
  144. static int printf_json(void *out, const char *fmt, va_list ap)
  145. {
  146. char *s;
  147. int err;
  148. err = vasprintf(&s, fmt, ap);
  149. if (err < 0)
  150. return -1;
  151. if (!oper_count) {
  152. int i;
  153. /* Strip trailing spaces */
  154. i = strlen(s) - 1;
  155. while (s[i] == ' ')
  156. s[i--] = '\0';
  157. jsonw_string_field(json_wtr, "operation", s);
  158. jsonw_name(json_wtr, "operands");
  159. jsonw_start_array(json_wtr);
  160. oper_count++;
  161. } else if (!strcmp(fmt, ",")) {
  162. /* Skip */
  163. } else {
  164. jsonw_string(json_wtr, s);
  165. oper_count++;
  166. }
  167. free(s);
  168. return 0;
  169. }
  170. static int fprintf_json(void *out, const char *fmt, ...)
  171. {
  172. va_list ap;
  173. int r;
  174. va_start(ap, fmt);
  175. r = printf_json(out, fmt, ap);
  176. va_end(ap);
  177. return r;
  178. }
  179. static int fprintf_json_styled(void *out,
  180. enum disassembler_style style __maybe_unused,
  181. const char *fmt, ...)
  182. {
  183. va_list ap;
  184. int r;
  185. va_start(ap, fmt);
  186. r = printf_json(out, fmt, ap);
  187. va_end(ap);
  188. return r;
  189. }
  190. static int init_context(disasm_ctx_t *ctx, const char *arch,
  191. const char *disassembler_options,
  192. unsigned char *image, ssize_t len, __u64 func_ksym)
  193. {
  194. struct disassemble_info *info;
  195. char tpath[PATH_MAX];
  196. bfd *bfdf;
  197. memset(tpath, 0, sizeof(tpath));
  198. if (get_exec_path(tpath, sizeof(tpath))) {
  199. p_err("failed to create disassembler (get_exec_path)");
  200. return -1;
  201. }
  202. ctx->bfdf = bfd_openr(tpath, NULL);
  203. if (!ctx->bfdf) {
  204. p_err("failed to create disassembler (bfd_openr)");
  205. return -1;
  206. }
  207. if (!bfd_check_format(ctx->bfdf, bfd_object)) {
  208. p_err("failed to create disassembler (bfd_check_format)");
  209. goto err_close;
  210. }
  211. bfdf = ctx->bfdf;
  212. ctx->info = malloc(sizeof(struct disasm_info));
  213. if (!ctx->info) {
  214. p_err("mem alloc failed");
  215. goto err_close;
  216. }
  217. ctx->info->func_ksym = func_ksym;
  218. info = &ctx->info->info;
  219. if (json_output)
  220. init_disassemble_info_compat(info, stdout,
  221. (fprintf_ftype) fprintf_json,
  222. fprintf_json_styled);
  223. else
  224. init_disassemble_info_compat(info, stdout,
  225. (fprintf_ftype) fprintf,
  226. fprintf_styled);
  227. /* Update architecture info for offload. */
  228. if (arch) {
  229. const bfd_arch_info_type *inf = bfd_scan_arch(arch);
  230. if (inf) {
  231. bfdf->arch_info = inf;
  232. } else {
  233. p_err("No libbfd support for %s", arch);
  234. goto err_free;
  235. }
  236. }
  237. info->arch = bfd_get_arch(bfdf);
  238. info->mach = bfd_get_mach(bfdf);
  239. if (disassembler_options)
  240. info->disassembler_options = disassembler_options;
  241. info->buffer = image;
  242. info->buffer_length = len;
  243. info->print_address_func = disasm_print_addr;
  244. disassemble_init_for_target(info);
  245. #ifdef DISASM_FOUR_ARGS_SIGNATURE
  246. ctx->disassemble = disassembler(info->arch,
  247. bfd_big_endian(bfdf),
  248. info->mach,
  249. bfdf);
  250. #else
  251. ctx->disassemble = disassembler(bfdf);
  252. #endif
  253. if (!ctx->disassemble) {
  254. p_err("failed to create disassembler");
  255. goto err_free;
  256. }
  257. return 0;
  258. err_free:
  259. free(info);
  260. err_close:
  261. bfd_close(ctx->bfdf);
  262. return -1;
  263. }
  264. static void destroy_context(disasm_ctx_t *ctx)
  265. {
  266. free(ctx->info);
  267. bfd_close(ctx->bfdf);
  268. }
  269. static int
  270. disassemble_insn(disasm_ctx_t *ctx, __maybe_unused unsigned char *image,
  271. __maybe_unused ssize_t len, int pc,
  272. __maybe_unused __u64 func_ksym)
  273. {
  274. return ctx->disassemble(pc, &ctx->info->info);
  275. }
  276. int disasm_init(void)
  277. {
  278. bfd_init();
  279. return 0;
  280. }
  281. #endif /* HAVE_LIBBPFD_SUPPORT */
  282. int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
  283. const char *arch, const char *disassembler_options,
  284. const struct btf *btf,
  285. const struct bpf_prog_linfo *prog_linfo,
  286. __u64 func_ksym, unsigned int func_idx,
  287. bool linum)
  288. {
  289. const struct bpf_line_info *linfo = NULL;
  290. unsigned int nr_skip = 0;
  291. int count, i;
  292. unsigned int pc = 0;
  293. disasm_ctx_t ctx;
  294. if (!len)
  295. return -1;
  296. if (init_context(&ctx, arch, disassembler_options, image, len, func_ksym))
  297. return -1;
  298. if (json_output)
  299. jsonw_start_array(json_wtr);
  300. do {
  301. if (prog_linfo) {
  302. linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
  303. func_ksym + pc,
  304. func_idx,
  305. nr_skip);
  306. if (linfo)
  307. nr_skip++;
  308. }
  309. if (json_output) {
  310. jsonw_start_object(json_wtr);
  311. oper_count = 0;
  312. if (linfo)
  313. btf_dump_linfo_json(btf, linfo, linum);
  314. jsonw_name(json_wtr, "pc");
  315. jsonw_printf(json_wtr, "\"0x%x\"", pc);
  316. } else {
  317. if (linfo)
  318. btf_dump_linfo_plain(btf, linfo, "; ",
  319. linum);
  320. printf("%4x:" DISASM_SPACER, pc);
  321. }
  322. count = disassemble_insn(&ctx, image, len, pc, func_ksym);
  323. if (json_output) {
  324. /* Operand array, was started in fprintf_json. Before
  325. * that, make sure we have a _null_ value if no operand
  326. * other than operation code was present.
  327. */
  328. if (oper_count == 1)
  329. jsonw_null(json_wtr);
  330. jsonw_end_array(json_wtr);
  331. }
  332. if (opcodes) {
  333. if (json_output) {
  334. jsonw_name(json_wtr, "opcodes");
  335. jsonw_start_array(json_wtr);
  336. for (i = 0; i < count; ++i)
  337. jsonw_printf(json_wtr, "\"0x%02hhx\"",
  338. (uint8_t)image[pc + i]);
  339. jsonw_end_array(json_wtr);
  340. } else {
  341. printf("\n\t");
  342. for (i = 0; i < count; ++i)
  343. printf("%02x ",
  344. (uint8_t)image[pc + i]);
  345. }
  346. }
  347. if (json_output)
  348. jsonw_end_object(json_wtr);
  349. else
  350. printf("\n");
  351. pc += count;
  352. } while (count > 0 && pc < len);
  353. if (json_output)
  354. jsonw_end_array(json_wtr);
  355. destroy_context(&ctx);
  356. return 0;
  357. }