]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Based on: | |
3 | * | |
4 | * Minimal BPF JIT image disassembler | |
5 | * | |
6 | * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for | |
7 | * debugging or verification purposes. | |
8 | * | |
9 | * Copyright 2013 Daniel Borkmann <daniel@iogearbox.net> | |
10 | * Licensed under the GNU General Public License, version 2.0 (GPLv2) | |
11 | */ | |
12 | ||
13 | #define _GNU_SOURCE | |
14 | #include <stdio.h> | |
15 | #include <stdarg.h> | |
16 | #include <stdint.h> | |
17 | #include <stdio.h> | |
18 | #include <stdlib.h> | |
19 | #include <assert.h> | |
20 | #include <unistd.h> | |
21 | #include <string.h> | |
22 | #include <bfd.h> | |
23 | #include <dis-asm.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <limits.h> | |
27 | ||
28 | #include "json_writer.h" | |
29 | #include "main.h" | |
30 | ||
31 | static void get_exec_path(char *tpath, size_t size) | |
32 | { | |
33 | ssize_t len; | |
34 | char *path; | |
35 | ||
36 | snprintf(tpath, size, "/proc/%d/exe", (int) getpid()); | |
37 | tpath[size - 1] = 0; | |
38 | ||
39 | path = strdup(tpath); | |
40 | assert(path); | |
41 | ||
42 | len = readlink(path, tpath, size - 1); | |
43 | assert(len > 0); | |
44 | tpath[len] = 0; | |
45 | ||
46 | free(path); | |
47 | } | |
48 | ||
49 | static int oper_count; | |
50 | static int fprintf_json(void *out, const char *fmt, ...) | |
51 | { | |
52 | va_list ap; | |
53 | char *s; | |
54 | ||
55 | va_start(ap, fmt); | |
56 | if (vasprintf(&s, fmt, ap) < 0) | |
57 | return -1; | |
58 | va_end(ap); | |
59 | ||
60 | if (!oper_count) { | |
61 | int i; | |
62 | ||
63 | /* Strip trailing spaces */ | |
64 | i = strlen(s) - 1; | |
65 | while (s[i] == ' ') | |
66 | s[i--] = '\0'; | |
67 | ||
68 | jsonw_string_field(json_wtr, "operation", s); | |
69 | jsonw_name(json_wtr, "operands"); | |
70 | jsonw_start_array(json_wtr); | |
71 | oper_count++; | |
72 | } else if (!strcmp(fmt, ",")) { | |
73 | /* Skip */ | |
74 | } else { | |
75 | jsonw_string(json_wtr, s); | |
76 | oper_count++; | |
77 | } | |
78 | free(s); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes) | |
83 | { | |
84 | disassembler_ftype disassemble; | |
85 | struct disassemble_info info; | |
86 | int count, i, pc = 0; | |
87 | char tpath[PATH_MAX]; | |
88 | bfd *bfdf; | |
89 | ||
90 | if (!len) | |
91 | return; | |
92 | ||
93 | memset(tpath, 0, sizeof(tpath)); | |
94 | get_exec_path(tpath, sizeof(tpath)); | |
95 | ||
96 | bfdf = bfd_openr(tpath, NULL); | |
97 | assert(bfdf); | |
98 | assert(bfd_check_format(bfdf, bfd_object)); | |
99 | ||
100 | if (json_output) | |
101 | init_disassemble_info(&info, stdout, | |
102 | (fprintf_ftype) fprintf_json); | |
103 | else | |
104 | init_disassemble_info(&info, stdout, | |
105 | (fprintf_ftype) fprintf); | |
106 | info.arch = bfd_get_arch(bfdf); | |
107 | info.mach = bfd_get_mach(bfdf); | |
108 | info.buffer = image; | |
109 | info.buffer_length = len; | |
110 | ||
111 | disassemble_init_for_target(&info); | |
112 | ||
113 | #ifdef DISASM_FOUR_ARGS_SIGNATURE | |
114 | disassemble = disassembler(info.arch, | |
115 | bfd_big_endian(bfdf), | |
116 | info.mach, | |
117 | bfdf); | |
118 | #else | |
119 | disassemble = disassembler(bfdf); | |
120 | #endif | |
121 | assert(disassemble); | |
122 | ||
123 | if (json_output) | |
124 | jsonw_start_array(json_wtr); | |
125 | do { | |
126 | if (json_output) { | |
127 | jsonw_start_object(json_wtr); | |
128 | oper_count = 0; | |
129 | jsonw_name(json_wtr, "pc"); | |
130 | jsonw_printf(json_wtr, "\"0x%x\"", pc); | |
131 | } else { | |
132 | printf("%4x:\t", pc); | |
133 | } | |
134 | ||
135 | count = disassemble(pc, &info); | |
136 | if (json_output) { | |
137 | /* Operand array, was started in fprintf_json. Before | |
138 | * that, make sure we have a _null_ value if no operand | |
139 | * other than operation code was present. | |
140 | */ | |
141 | if (oper_count == 1) | |
142 | jsonw_null(json_wtr); | |
143 | jsonw_end_array(json_wtr); | |
144 | } | |
145 | ||
146 | if (opcodes) { | |
147 | if (json_output) { | |
148 | jsonw_name(json_wtr, "opcodes"); | |
149 | jsonw_start_array(json_wtr); | |
150 | for (i = 0; i < count; ++i) | |
151 | jsonw_printf(json_wtr, "\"0x%02hhx\"", | |
152 | (uint8_t)image[pc + i]); | |
153 | jsonw_end_array(json_wtr); | |
154 | } else { | |
155 | printf("\n\t"); | |
156 | for (i = 0; i < count; ++i) | |
157 | printf("%02x ", | |
158 | (uint8_t)image[pc + i]); | |
159 | } | |
160 | } | |
161 | if (json_output) | |
162 | jsonw_end_object(json_wtr); | |
163 | else | |
164 | printf("\n"); | |
165 | ||
166 | pc += count; | |
167 | } while (count > 0 && pc < len); | |
168 | if (json_output) | |
169 | jsonw_end_array(json_wtr); | |
170 | ||
171 | bfd_close(bfdf); | |
172 | } | |
173 | ||
174 | int disasm_init(void) | |
175 | { | |
176 | bfd_init(); | |
177 | return 0; | |
178 | } |