Coverage Report

Created: 2026-11-21 09:00

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