blob: aaf99a0168c90b2636d474e1d46015d2c7bd487e [file] [log] [blame]
Jakub Kicinski907b2232018-12-12 19:59:26 -08001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
Jakub Kicinski71bb4282017-10-04 20:10:04 -07002/*
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
Jiri Olsaaa52bcb2019-07-05 14:10:31 +020014#define _GNU_SOURCE
15#include <stdio.h>
Quentin Monnet107f0412017-10-23 09:24:09 -070016#include <stdarg.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070017#include <stdint.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070018#include <stdlib.h>
19#include <assert.h>
20#include <unistd.h>
21#include <string.h>
22#include <bfd.h>
23#include <dis-asm.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070024#include <sys/stat.h>
Prashant Bholecdc89c92017-11-02 17:09:45 +090025#include <limits.h>
Toke Høiland-Jørgensen229c3b42020-01-20 14:06:46 +010026#include <bpf/libbpf.h>
Andres Freund600b7b22022-07-31 18:38:33 -070027#include <tools/dis-asm-compat.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070028
Quentin Monnet107f0412017-10-23 09:24:09 -070029#include "json_writer.h"
30#include "main.h"
31
Jakub Kicinski71bb4282017-10-04 20:10:04 -070032static void get_exec_path(char *tpath, size_t size)
33{
Quentin Monnet327e5da2018-11-30 16:25:44 +000034 const char *path = "/proc/self/exe";
Jakub Kicinski71bb4282017-10-04 20:10:04 -070035 ssize_t len;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070036
37 len = readlink(path, tpath, size - 1);
38 assert(len > 0);
39 tpath[len] = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070040}
41
Quentin Monnet107f0412017-10-23 09:24:09 -070042static int oper_count;
Andres Freund600b7b22022-07-31 18:38:33 -070043static int printf_json(void *out, const char *fmt, va_list ap)
Quentin Monnet107f0412017-10-23 09:24:09 -070044{
Quentin Monnet107f0412017-10-23 09:24:09 -070045 char *s;
Gu Shengxianbc832062021-07-05 18:35:43 -070046 int err;
Quentin Monnet107f0412017-10-23 09:24:09 -070047
Gu Shengxianbc832062021-07-05 18:35:43 -070048 err = vasprintf(&s, fmt, ap);
Gu Shengxianbc832062021-07-05 18:35:43 -070049 if (err < 0)
50 return -1;
Jiri Olsaaa52bcb2019-07-05 14:10:31 +020051
Quentin Monnet107f0412017-10-23 09:24:09 -070052 if (!oper_count) {
53 int i;
54
Quentin Monnet107f0412017-10-23 09:24:09 -070055 /* Strip trailing spaces */
56 i = strlen(s) - 1;
57 while (s[i] == ' ')
58 s[i--] = '\0';
59
60 jsonw_string_field(json_wtr, "operation", s);
61 jsonw_name(json_wtr, "operands");
62 jsonw_start_array(json_wtr);
63 oper_count++;
64 } else if (!strcmp(fmt, ",")) {
65 /* Skip */
66 } else {
Quentin Monnet107f0412017-10-23 09:24:09 -070067 jsonw_string(json_wtr, s);
68 oper_count++;
69 }
Jiri Olsaaa52bcb2019-07-05 14:10:31 +020070 free(s);
Quentin Monnet107f0412017-10-23 09:24:09 -070071 return 0;
72}
73
Andres Freund600b7b22022-07-31 18:38:33 -070074static int fprintf_json(void *out, const char *fmt, ...)
75{
76 va_list ap;
77 int r;
78
79 va_start(ap, fmt);
80 r = printf_json(out, fmt, ap);
81 va_end(ap);
82
83 return r;
84}
85
86static int fprintf_json_styled(void *out,
87 enum disassembler_style style __maybe_unused,
88 const char *fmt, ...)
89{
90 va_list ap;
91 int r;
92
93 va_start(ap, fmt);
94 r = printf_json(out, fmt, ap);
95 va_end(ap);
96
97 return r;
98}
99
Jiong Wange6593592018-01-16 16:05:21 -0800100void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
Martin KaFai Laub053b432018-12-07 16:42:32 -0800101 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,
105 bool linum)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700106{
Martin KaFai Laub053b432018-12-07 16:42:32 -0800107 const struct bpf_line_info *linfo = NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700108 disassembler_ftype disassemble;
109 struct disassemble_info info;
Martin KaFai Laub053b432018-12-07 16:42:32 -0800110 unsigned int nr_skip = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700111 int count, i, pc = 0;
Prashant Bholecdc89c92017-11-02 17:09:45 +0900112 char tpath[PATH_MAX];
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700113 bfd *bfdf;
114
115 if (!len)
116 return;
117
118 memset(tpath, 0, sizeof(tpath));
119 get_exec_path(tpath, sizeof(tpath));
120
121 bfdf = bfd_openr(tpath, NULL);
122 assert(bfdf);
123 assert(bfd_check_format(bfdf, bfd_object));
124
Quentin Monnet107f0412017-10-23 09:24:09 -0700125 if (json_output)
Andres Freund600b7b22022-07-31 18:38:33 -0700126 init_disassemble_info_compat(&info, stdout,
127 (fprintf_ftype) fprintf_json,
128 fprintf_json_styled);
Quentin Monnet107f0412017-10-23 09:24:09 -0700129 else
Andres Freund600b7b22022-07-31 18:38:33 -0700130 init_disassemble_info_compat(&info, stdout,
131 (fprintf_ftype) fprintf,
132 fprintf_styled);
Jiong Wange6593592018-01-16 16:05:21 -0800133
134 /* Update architecture info for offload. */
135 if (arch) {
136 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
137
138 if (inf) {
139 bfdf->arch_info = inf;
140 } else {
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800141 p_err("No libbfd support for %s", arch);
Jiong Wange6593592018-01-16 16:05:21 -0800142 return;
143 }
144 }
145
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700146 info.arch = bfd_get_arch(bfdf);
147 info.mach = bfd_get_mach(bfdf);
Jakub Kicinski3ddeac62018-10-18 11:34:55 -0700148 if (disassembler_options)
149 info.disassembler_options = disassembler_options;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700150 info.buffer = image;
151 info.buffer_length = len;
152
153 disassemble_init_for_target(&info);
154
Roman Gushchinfb982662017-12-27 19:16:29 +0000155#ifdef DISASM_FOUR_ARGS_SIGNATURE
156 disassemble = disassembler(info.arch,
157 bfd_big_endian(bfdf),
158 info.mach,
159 bfdf);
160#else
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700161 disassemble = disassembler(bfdf);
Roman Gushchinfb982662017-12-27 19:16:29 +0000162#endif
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700163 assert(disassemble);
164
Quentin Monnet107f0412017-10-23 09:24:09 -0700165 if (json_output)
166 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700167 do {
Martin KaFai Laub053b432018-12-07 16:42:32 -0800168 if (prog_linfo) {
169 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
170 func_ksym + pc,
171 func_idx,
172 nr_skip);
173 if (linfo)
174 nr_skip++;
175 }
176
Quentin Monnet107f0412017-10-23 09:24:09 -0700177 if (json_output) {
178 jsonw_start_object(json_wtr);
179 oper_count = 0;
Martin KaFai Laub053b432018-12-07 16:42:32 -0800180 if (linfo)
181 btf_dump_linfo_json(btf, linfo, linum);
Quentin Monnet107f0412017-10-23 09:24:09 -0700182 jsonw_name(json_wtr, "pc");
183 jsonw_printf(json_wtr, "\"0x%x\"", pc);
184 } else {
Martin KaFai Laub053b432018-12-07 16:42:32 -0800185 if (linfo)
186 btf_dump_linfo_plain(btf, linfo, "; ",
187 linum);
Quentin Monnet107f0412017-10-23 09:24:09 -0700188 printf("%4x:\t", pc);
189 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700190
191 count = disassemble(pc, &info);
Quentin Monnet107f0412017-10-23 09:24:09 -0700192 if (json_output) {
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.
196 */
197 if (oper_count == 1)
198 jsonw_null(json_wtr);
199 jsonw_end_array(json_wtr);
200 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700201
202 if (opcodes) {
Quentin Monnet107f0412017-10-23 09:24:09 -0700203 if (json_output) {
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);
210 } else {
211 printf("\n\t");
212 for (i = 0; i < count; ++i)
213 printf("%02x ",
214 (uint8_t)image[pc + i]);
215 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700216 }
Quentin Monnet107f0412017-10-23 09:24:09 -0700217 if (json_output)
218 jsonw_end_object(json_wtr);
219 else
220 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700221
222 pc += count;
223 } while (count > 0 && pc < len);
Quentin Monnet107f0412017-10-23 09:24:09 -0700224 if (json_output)
225 jsonw_end_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700226
227 bfd_close(bfdf);
228}
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800229
230int disasm_init(void)
231{
232 bfd_init();
233 return 0;
234}