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