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
14 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <assert.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <bfd.h>
24 #include <dis-asm.h>
25 #include <sys/stat.h>
26 #include <limits.h>
27 #include <libbpf.h>
28
29 #include "json_writer.h"
30 #include "main.h"
31
get_exec_path(char * tpath,size_t size)32 static void get_exec_path(char *tpath, size_t size)
33 {
34 const char *path = "/proc/self/exe";
35 ssize_t len;
36
37 len = readlink(path, tpath, size - 1);
38 assert(len > 0);
39 tpath[len] = 0;
40 }
41
42 static int oper_count;
fprintf_json(void * out,const char * fmt,...)43 static int fprintf_json(void *out, const char *fmt, ...)
44 {
45 va_list ap;
46 char *s;
47 int err;
48
49 va_start(ap, fmt);
50 err = vasprintf(&s, fmt, ap);
51 va_end(ap);
52 if (err < 0)
53 return -1;
54
55 if (!oper_count) {
56 int i;
57
58 /* Strip trailing spaces */
59 i = strlen(s) - 1;
60 while (s[i] == ' ')
61 s[i--] = '\0';
62
63 jsonw_string_field(json_wtr, "operation", s);
64 jsonw_name(json_wtr, "operands");
65 jsonw_start_array(json_wtr);
66 oper_count++;
67 } else if (!strcmp(fmt, ",")) {
68 /* Skip */
69 } else {
70 jsonw_string(json_wtr, s);
71 oper_count++;
72 }
73 free(s);
74 return 0;
75 }
76
disasm_print_insn(unsigned char * image,ssize_t len,int opcodes,const char * arch,const char * disassembler_options,const struct btf * btf,const struct bpf_prog_linfo * prog_linfo,__u64 func_ksym,unsigned int func_idx,bool linum)77 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
78 const char *arch, const char *disassembler_options,
79 const struct btf *btf,
80 const struct bpf_prog_linfo *prog_linfo,
81 __u64 func_ksym, unsigned int func_idx,
82 bool linum)
83 {
84 const struct bpf_line_info *linfo = NULL;
85 disassembler_ftype disassemble;
86 struct disassemble_info info;
87 unsigned int nr_skip = 0;
88 int count, i, pc = 0;
89 char tpath[PATH_MAX];
90 bfd *bfdf;
91
92 if (!len)
93 return;
94
95 memset(tpath, 0, sizeof(tpath));
96 get_exec_path(tpath, sizeof(tpath));
97
98 bfdf = bfd_openr(tpath, NULL);
99 assert(bfdf);
100 assert(bfd_check_format(bfdf, bfd_object));
101
102 if (json_output)
103 init_disassemble_info(&info, stdout,
104 (fprintf_ftype) fprintf_json);
105 else
106 init_disassemble_info(&info, stdout,
107 (fprintf_ftype) fprintf);
108
109 /* Update architecture info for offload. */
110 if (arch) {
111 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
112
113 if (inf) {
114 bfdf->arch_info = inf;
115 } else {
116 p_err("No libbfd support for %s", arch);
117 return;
118 }
119 }
120
121 info.arch = bfd_get_arch(bfdf);
122 info.mach = bfd_get_mach(bfdf);
123 if (disassembler_options)
124 info.disassembler_options = disassembler_options;
125 info.buffer = image;
126 info.buffer_length = len;
127
128 disassemble_init_for_target(&info);
129
130 #ifdef DISASM_FOUR_ARGS_SIGNATURE
131 disassemble = disassembler(info.arch,
132 bfd_big_endian(bfdf),
133 info.mach,
134 bfdf);
135 #else
136 disassemble = disassembler(bfdf);
137 #endif
138 assert(disassemble);
139
140 if (json_output)
141 jsonw_start_array(json_wtr);
142 do {
143 if (prog_linfo) {
144 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
145 func_ksym + pc,
146 func_idx,
147 nr_skip);
148 if (linfo)
149 nr_skip++;
150 }
151
152 if (json_output) {
153 jsonw_start_object(json_wtr);
154 oper_count = 0;
155 if (linfo)
156 btf_dump_linfo_json(btf, linfo, linum);
157 jsonw_name(json_wtr, "pc");
158 jsonw_printf(json_wtr, "\"0x%x\"", pc);
159 } else {
160 if (linfo)
161 btf_dump_linfo_plain(btf, linfo, "; ",
162 linum);
163 printf("%4x:\t", pc);
164 }
165
166 count = disassemble(pc, &info);
167 if (json_output) {
168 /* Operand array, was started in fprintf_json. Before
169 * that, make sure we have a _null_ value if no operand
170 * other than operation code was present.
171 */
172 if (oper_count == 1)
173 jsonw_null(json_wtr);
174 jsonw_end_array(json_wtr);
175 }
176
177 if (opcodes) {
178 if (json_output) {
179 jsonw_name(json_wtr, "opcodes");
180 jsonw_start_array(json_wtr);
181 for (i = 0; i < count; ++i)
182 jsonw_printf(json_wtr, "\"0x%02hhx\"",
183 (uint8_t)image[pc + i]);
184 jsonw_end_array(json_wtr);
185 } else {
186 printf("\n\t");
187 for (i = 0; i < count; ++i)
188 printf("%02x ",
189 (uint8_t)image[pc + i]);
190 }
191 }
192 if (json_output)
193 jsonw_end_object(json_wtr);
194 else
195 printf("\n");
196
197 pc += count;
198 } while (count > 0 && pc < len);
199 if (json_output)
200 jsonw_end_array(json_wtr);
201
202 bfd_close(bfdf);
203 }
204
disasm_init(void)205 int disasm_init(void)
206 {
207 bfd_init();
208 return 0;
209 }
210