1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
5 * Minimal BPF JIT image disassembler
7 * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
8 * debugging or verification purposes.
11 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
26 #include <bpf/libbpf.h>
28 #include "json_writer.h"
31 static void get_exec_path(char *tpath, size_t size)
33 const char *path = "/proc/self/exe";
36 len = readlink(path, tpath, size - 1);
41 static int oper_count;
42 static int fprintf_json(void *out, const char *fmt, ...)
49 err = vasprintf(&s, fmt, ap);
57 /* Strip trailing spaces */
62 jsonw_string_field(json_wtr, "operation", s);
63 jsonw_name(json_wtr, "operands");
64 jsonw_start_array(json_wtr);
66 } else if (!strcmp(fmt, ",")) {
69 jsonw_string(json_wtr, s);
76 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
77 const char *arch, const char *disassembler_options,
78 const struct btf *btf,
79 const struct bpf_prog_linfo *prog_linfo,
80 __u64 func_ksym, unsigned int func_idx,
83 const struct bpf_line_info *linfo = NULL;
84 disassembler_ftype disassemble;
85 struct disassemble_info info;
86 unsigned int nr_skip = 0;
94 memset(tpath, 0, sizeof(tpath));
95 get_exec_path(tpath, sizeof(tpath));
97 bfdf = bfd_openr(tpath, NULL);
99 assert(bfd_check_format(bfdf, bfd_object));
102 init_disassemble_info(&info, stdout,
103 (fprintf_ftype) fprintf_json);
105 init_disassemble_info(&info, stdout,
106 (fprintf_ftype) fprintf);
108 /* Update architecture info for offload. */
110 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
113 bfdf->arch_info = inf;
115 p_err("No libbfd support for %s", arch);
120 info.arch = bfd_get_arch(bfdf);
121 info.mach = bfd_get_mach(bfdf);
122 if (disassembler_options)
123 info.disassembler_options = disassembler_options;
125 info.buffer_length = len;
127 disassemble_init_for_target(&info);
129 #ifdef DISASM_FOUR_ARGS_SIGNATURE
130 disassemble = disassembler(info.arch,
131 bfd_big_endian(bfdf),
135 disassemble = disassembler(bfdf);
140 jsonw_start_array(json_wtr);
143 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
152 jsonw_start_object(json_wtr);
155 btf_dump_linfo_json(btf, linfo, linum);
156 jsonw_name(json_wtr, "pc");
157 jsonw_printf(json_wtr, "\"0x%x\"", pc);
160 btf_dump_linfo_plain(btf, linfo, "; ",
162 printf("%4x:\t", pc);
165 count = disassemble(pc, &info);
167 /* Operand array, was started in fprintf_json. Before
168 * that, make sure we have a _null_ value if no operand
169 * other than operation code was present.
172 jsonw_null(json_wtr);
173 jsonw_end_array(json_wtr);
178 jsonw_name(json_wtr, "opcodes");
179 jsonw_start_array(json_wtr);
180 for (i = 0; i < count; ++i)
181 jsonw_printf(json_wtr, "\"0x%02hhx\"",
182 (uint8_t)image[pc + i]);
183 jsonw_end_array(json_wtr);
186 for (i = 0; i < count; ++i)
188 (uint8_t)image[pc + i]);
192 jsonw_end_object(json_wtr);
197 } while (count > 0 && pc < len);
199 jsonw_end_array(json_wtr);
204 int disasm_init(void)