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>
27 #include <tools/dis-asm-compat.h>
29 #include "json_writer.h"
32 static void get_exec_path(char *tpath, size_t size)
34 const char *path = "/proc/self/exe";
37 len = readlink(path, tpath, size - 1);
42 static int oper_count;
43 static int printf_json(void *out, const char *fmt, va_list ap)
48 err = vasprintf(&s, fmt, ap);
55 /* Strip trailing spaces */
60 jsonw_string_field(json_wtr, "operation", s);
61 jsonw_name(json_wtr, "operands");
62 jsonw_start_array(json_wtr);
64 } else if (!strcmp(fmt, ",")) {
67 jsonw_string(json_wtr, s);
74 static int fprintf_json(void *out, const char *fmt, ...)
80 r = printf_json(out, fmt, ap);
86 static int fprintf_json_styled(void *out,
87 enum disassembler_style style __maybe_unused,
94 r = printf_json(out, fmt, ap);
100 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
101 const char *arch, const char *disassembler_options,
102 const struct btf *btf,
103 const struct bpf_prog_linfo *prog_linfo,
104 __u64 func_ksym, unsigned int func_idx,
107 const struct bpf_line_info *linfo = NULL;
108 disassembler_ftype disassemble;
109 struct disassemble_info info;
110 unsigned int nr_skip = 0;
111 int count, i, pc = 0;
112 char tpath[PATH_MAX];
118 memset(tpath, 0, sizeof(tpath));
119 get_exec_path(tpath, sizeof(tpath));
121 bfdf = bfd_openr(tpath, NULL);
123 assert(bfd_check_format(bfdf, bfd_object));
126 init_disassemble_info_compat(&info, stdout,
127 (fprintf_ftype) fprintf_json,
128 fprintf_json_styled);
130 init_disassemble_info_compat(&info, stdout,
131 (fprintf_ftype) fprintf,
134 /* Update architecture info for offload. */
136 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
139 bfdf->arch_info = inf;
141 p_err("No libbfd support for %s", arch);
146 info.arch = bfd_get_arch(bfdf);
147 info.mach = bfd_get_mach(bfdf);
148 if (disassembler_options)
149 info.disassembler_options = disassembler_options;
151 info.buffer_length = len;
153 disassemble_init_for_target(&info);
155 #ifdef DISASM_FOUR_ARGS_SIGNATURE
156 disassemble = disassembler(info.arch,
157 bfd_big_endian(bfdf),
161 disassemble = disassembler(bfdf);
166 jsonw_start_array(json_wtr);
169 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
178 jsonw_start_object(json_wtr);
181 btf_dump_linfo_json(btf, linfo, linum);
182 jsonw_name(json_wtr, "pc");
183 jsonw_printf(json_wtr, "\"0x%x\"", pc);
186 btf_dump_linfo_plain(btf, linfo, "; ",
188 printf("%4x:\t", pc);
191 count = disassemble(pc, &info);
193 /* Operand array, was started in fprintf_json. Before
194 * that, make sure we have a _null_ value if no operand
195 * other than operation code was present.
198 jsonw_null(json_wtr);
199 jsonw_end_array(json_wtr);
204 jsonw_name(json_wtr, "opcodes");
205 jsonw_start_array(json_wtr);
206 for (i = 0; i < count; ++i)
207 jsonw_printf(json_wtr, "\"0x%02hhx\"",
208 (uint8_t)image[pc + i]);
209 jsonw_end_array(json_wtr);
212 for (i = 0; i < count; ++i)
214 (uint8_t)image[pc + i]);
218 jsonw_end_object(json_wtr);
223 } while (count > 0 && pc < len);
225 jsonw_end_array(json_wtr);
230 int disasm_init(void)