]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/bpf/bpftool/jit_disasm.c
bpftool: make libbfd optional
[mirror_ubuntu-bionic-kernel.git] / tools / bpf / bpftool / jit_disasm.c
CommitLineData
71bb428f
JK
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
ed172b9e
JO
13#define _GNU_SOURCE
14#include <stdio.h>
107f0412 15#include <stdarg.h>
71bb428f
JK
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>
cdc89c91 26#include <limits.h>
71bb428f 27
107f0412
QM
28#include "json_writer.h"
29#include "main.h"
30
71bb428f
JK
31static 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
107f0412
QM
49static int oper_count;
50static int fprintf_json(void *out, const char *fmt, ...)
51{
52 va_list ap;
53 char *s;
54
55 va_start(ap, fmt);
ed172b9e
JO
56 if (vasprintf(&s, fmt, ap) < 0)
57 return -1;
58 va_end(ap);
59
107f0412
QM
60 if (!oper_count) {
61 int i;
62
107f0412
QM
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 {
107f0412
QM
75 jsonw_string(json_wtr, s);
76 oper_count++;
77 }
ed172b9e 78 free(s);
107f0412
QM
79 return 0;
80}
81
71bb428f
JK
82void 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;
cdc89c91 87 char tpath[PATH_MAX];
71bb428f
JK
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
107f0412
QM
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);
71bb428f
JK
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
ff65ef9c
RG
113#ifdef DISASM_FOUR_ARGS_SIGNATURE
114 disassemble = disassembler(info.arch,
115 bfd_big_endian(bfdf),
116 info.mach,
117 bfdf);
118#else
71bb428f 119 disassemble = disassembler(bfdf);
ff65ef9c 120#endif
71bb428f
JK
121 assert(disassemble);
122
107f0412
QM
123 if (json_output)
124 jsonw_start_array(json_wtr);
71bb428f 125 do {
107f0412
QM
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 }
71bb428f
JK
134
135 count = disassemble(pc, &info);
107f0412
QM
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 }
71bb428f
JK
145
146 if (opcodes) {
107f0412
QM
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 }
71bb428f 160 }
107f0412
QM
161 if (json_output)
162 jsonw_end_object(json_wtr);
163 else
164 printf("\n");
71bb428f
JK
165
166 pc += count;
167 } while (count > 0 && pc < len);
107f0412
QM
168 if (json_output)
169 jsonw_end_array(json_wtr);
71bb428f
JK
170
171 bfd_close(bfdf);
172}
66155515
SF
173
174int disasm_init(void)
175{
176 bfd_init();
177 return 0;
178}