1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/* bpf-dis.c - BPF disassembler.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by Oracle Inc.
This file is part of the GNU binutils.
This is free software; you can redistribute them and/or modify them
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3. If not,
see <http://www.gnu.org/licenses/>. */
#include "sysdep.h"
#include "disassemble.h"
#include "libiberty.h"
#include "opintl.h"
#include "opcode/bpf.h"
#include <string.h>
#include <inttypes.h>
/* This disassembler supports two different syntaxes for BPF assembly.
One is called "normal" and has the typical form for assembly
languages, with mnemonics and the like. The othe is called
"pseudoc" and looks like C. */
enum bpf_dialect
{
BPF_DIALECT_NORMAL,
BPF_DIALECT_PSEUDOC
};
/* Global configuration for the disassembler. */
static enum bpf_dialect asm_dialect = BPF_DIALECT_NORMAL;
static int asm_bpf_version = BPF_V4;
static int asm_obase = 10;
/* Print BPF specific command-line options. */
void
print_bpf_disassembler_options (FILE *stream)
{
fprintf (stream, _("\n\
The following BPF specific disassembler options are supported for use\n\
with the -M switch (multiple options should be separated by commas):\n"));
fprintf (stream, "\n");
fprintf (stream, _("\
pseudoc Use pseudo-c syntax.\n\
v1,v2,v3,v4,xbpf Version of the BPF ISA to use.\n\
hex,oct,dec Output numerical base for immediates.\n"));
}
/* Parse BPF specific command-line options. */
static void
parse_bpf_dis_option (const char *option)
{
if (strcmp (option, "pseudoc") == 0)
asm_dialect = BPF_DIALECT_PSEUDOC;
else if (strcmp (option, "v1") == 0)
asm_bpf_version = BPF_V1;
else if (strcmp (option, "v2") == 0)
asm_bpf_version = BPF_V2;
else if (strcmp (option, "v3") == 0)
asm_bpf_version = BPF_V3;
else if (strcmp (option, "v4") == 0)
asm_bpf_version = BPF_V4;
else if (strcmp (option, "xbpf") == 0)
asm_bpf_version = BPF_XBPF;
else if (strcmp (option, "hex") == 0)
asm_obase = 16;
else if (strcmp (option, "oct") == 0)
asm_obase = 8;
else if (strcmp (option, "dec") == 0)
asm_obase = 10;
else
/* xgettext:c-format */
opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
}
static void
parse_bpf_dis_options (const char *opts_in)
{
char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
for ( ; opt_end != NULL; opt = opt_end + 1)
{
if ((opt_end = strchr (opt, ',')) != NULL)
*opt_end = 0;
parse_bpf_dis_option (opt);
}
free (opts);
}
/* Auxiliary function used in print_insn_bpf below. */
static void
print_register (disassemble_info *info,
const char *tag, uint8_t regno)
{
const char *fmt
= (asm_dialect == BPF_DIALECT_NORMAL
? "%%r%d"
: ((*(tag + 2) == 'w')
? "w%d"
: "r%d"));
(*info->fprintf_styled_func) (info->stream, dis_style_register, fmt, regno);
}
/* Main entry point.
Print one instruction from PC on INFO->STREAM.
Return the size of the instruction (in bytes). */
int
print_insn_bpf (bfd_vma pc, disassemble_info *info)
{
int insn_size = 8, status;
bfd_byte insn_bytes[16];
bpf_insn_word word = 0;
const struct bpf_opcode *insn = NULL;
enum bpf_endian endian = (info->endian == BFD_ENDIAN_LITTLE
? BPF_ENDIAN_LITTLE : BPF_ENDIAN_BIG);
/* Handle bpf-specific command-line options. */
if (info->disassembler_options != NULL)
{
parse_bpf_dis_options (info->disassembler_options);
/* Avoid repeteadly parsing the options. */
info->disassembler_options = NULL;
}
/* Print eight bytes per line. */
info->bytes_per_chunk = 1;
info->bytes_per_line = 8;
/* Read an instruction word. */
status = (*info->read_memory_func) (pc, insn_bytes, 8, info);
if (status != 0)
{
(*info->memory_error_func) (status, pc, info);
return -1;
}
word = (bpf_insn_word) bfd_getb64 (insn_bytes);
/* Try to match an instruction with it. */
insn = bpf_match_insn (word, endian, asm_bpf_version);
/* Print it out. */
if (insn)
{
const char *insn_tmpl
= asm_dialect == BPF_DIALECT_NORMAL ? insn->normal : insn->pseudoc;
const char *p = insn_tmpl;
/* Print the template contents completed with the instruction
operands. */
for (p = insn_tmpl; *p != '\0';)
{
switch (*p)
{
case ' ':
/* Single space prints to nothing. */
p += 1;
break;
case '%':
if (*(p + 1) == '%')
{
(*info->fprintf_styled_func) (info->stream, dis_style_text, "%%");
p += 2;
}
else if (*(p + 1) == 'w' || *(p + 1) == 'W')
{
/* %W prints to a single space. */
(*info->fprintf_styled_func) (info->stream, dis_style_text, " ");
p += 2;
}
else if (strncmp (p, "%dr", 3) == 0)
{
print_register (info, p, bpf_extract_dst (word, endian));
p += 3;
}
else if (strncmp (p, "%sr", 3) == 0)
{
print_register (info, p, bpf_extract_src (word, endian));
p += 3;
}
else if (strncmp (p, "%dw", 3) == 0)
{
print_register (info, p, bpf_extract_dst (word, endian));
p += 3;
}
else if (strncmp (p, "%sw", 3) == 0)
{
print_register (info, p, bpf_extract_src (word, endian));
p += 3;
}
else if (strncmp (p, "%i32", 4) == 0
|| strncmp (p, "%d32", 4) == 0
|| strncmp (p, "%I32", 4) == 0)
{
int32_t imm32 = bpf_extract_imm32 (word, endian);
if (p[1] == 'I')
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
"%s",
asm_obase != 10 || imm32 > 0 ? "+" : "");
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
asm_obase == 10 ? "%" PRIi32
: asm_obase == 8 ? "%" PRIo32
: "0x%" PRIx32,
imm32);
p += 4;
}
else if (strncmp (p, "%o16", 4) == 0
|| strncmp (p, "%d16", 4) == 0)
{
int16_t offset16 = bpf_extract_offset16 (word, endian);
if (p[1] == 'o')
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
"%s",
asm_obase != 10 || offset16 > 0 ? "+" : "");
if (asm_obase == 16 || asm_obase == 8)
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
asm_obase == 8 ? "0%" PRIo16 : "0x%" PRIx16,
(uint16_t) offset16);
else
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
"%" PRIi16, offset16);
p += 4;
}
else if (strncmp (p, "%i64", 4) == 0)
{
bpf_insn_word word2 = 0;
status = (*info->read_memory_func) (pc + 8, insn_bytes + 8,
8, info);
if (status != 0)
{
(*info->memory_error_func) (status, pc + 8, info);
return -1;
}
word2 = (bpf_insn_word) bfd_getb64 (insn_bytes + 8);
(*info->fprintf_styled_func) (info->stream, dis_style_immediate,
asm_obase == 10 ? "%" PRIi64
: asm_obase == 8 ? "0%" PRIo64
: "0x%" PRIx64,
bpf_extract_imm64 (word, word2, endian));
insn_size = 16;
p += 4;
}
else
{
/* xgettext:c-format */
opcodes_error_handler (_("# internal error, unknown tag in opcode template (%s)"),
insn_tmpl);
return -1;
}
break;
default:
/* Any other character is printed literally. */
(*info->fprintf_styled_func) (info->stream, dis_style_text, "%c", *p);
p += 1;
}
}
}
else
(*info->fprintf_styled_func) (info->stream, dis_style_text, "<unknown>");
return insn_size;
}
|