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.
10 * Copyright 2013 Daniel Borkmann <daniel@iogearbox.net>
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, ...)
48 if (vasprintf(&s, fmt, ap) < 0)
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 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
75 const char *arch, const char *disassembler_options,
76 const struct btf *btf,
77 const struct bpf_prog_linfo *prog_linfo,
78 __u64 func_ksym, unsigned int func_idx,
81 const struct bpf_line_info *linfo = NULL;
82 disassembler_ftype disassemble;
83 struct disassemble_info info;
84 unsigned int nr_skip = 0;
92 memset(tpath, 0, sizeof(tpath));
93 get_exec_path(tpath, sizeof(tpath));
95 bfdf = bfd_openr(tpath, NULL);
97 assert(bfd_check_format(bfdf, bfd_object));
100 init_disassemble_info(&info, stdout,
101 (fprintf_ftype) fprintf_json);
103 init_disassemble_info(&info, stdout,
104 (fprintf_ftype) fprintf);
106 /* Update architecture info for offload. */
108 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
111 bfdf->arch_info = inf;
113 p_err("No libbfd support for %s", arch);
118 info.arch = bfd_get_arch(bfdf);
119 info.mach = bfd_get_mach(bfdf);
120 if (disassembler_options)
121 info.disassembler_options = disassembler_options;
123 info.buffer_length = len;
125 disassemble_init_for_target(&info);
127 #ifdef DISASM_FOUR_ARGS_SIGNATURE
128 disassemble = disassembler(info.arch,
129 bfd_big_endian(bfdf),
133 disassemble = disassembler(bfdf);
138 jsonw_start_array(json_wtr);
141 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
150 jsonw_start_object(json_wtr);
153 btf_dump_linfo_json(btf, linfo, linum);
154 jsonw_name(json_wtr, "pc");
155 jsonw_printf(json_wtr, "\"0x%x\"", pc);
158 btf_dump_linfo_plain(btf, linfo, "; ",
160 printf("%4x:\t", pc);
163 count = disassemble(pc, &info);
165 /* Operand array, was started in fprintf_json. Before
166 * that, make sure we have a _null_ value if no operand
167 * other than operation code was present.
170 jsonw_null(json_wtr);
171 jsonw_end_array(json_wtr);
176 jsonw_name(json_wtr, "opcodes");
177 jsonw_start_array(json_wtr);
178 for (i = 0; i < count; ++i)
179 jsonw_printf(json_wtr, "\"0x%02hhx\"",
180 (uint8_t)image[pc + i]);
181 jsonw_end_array(json_wtr);
184 for (i = 0; i < count; ++i)
186 (uint8_t)image[pc + i]);
190 jsonw_end_object(json_wtr);
195 } while (count > 0 && pc < len);
197 jsonw_end_array(json_wtr);
202 int disasm_init(void)