66ac3d940965858c98e2df946d3b56f161305894
[openwrt/staging/pepe2k.git] /
1 From a82db18ab34ba7f9d38319e8cc01ffe382e3e55e Mon Sep 17 00:00:00 2001
2 From: Andres Freund <andres@anarazel.de>
3 Date: Sun, 31 Jul 2022 18:38:33 -0700
4 Subject: [PATCH 5/5] tools bpftool: Fix compilation error with new binutils
5
6 binutils changed the signature of init_disassemble_info(), which now causes
7 compilation to fail for tools/bpf/bpftool/jit_disasm.c, e.g. on debian
8 unstable.
9
10 Relevant binutils commit:
11
12 https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=60a3da00bd5407f07
13
14 Wire up the feature test and switch to init_disassemble_info_compat(),
15 which were introduced in prior commits, fixing the compilation failure.
16
17 I verified that bpftool can still disassemble bpf programs, both with an
18 old and new dis-asm.h API. There are no output changes for plain and json
19 formats. When comparing the output from old binutils (2.35)
20 to new bintuils with the patch (upstream snapshot) there are a few output
21 differences, but they are unrelated to this patch. An example hunk is:
22
23 2f: pop %r14
24 31: pop %r13
25 33: pop %rbx
26 - 34: leaveq
27 - 35: retq
28 + 34: leave
29 + 35: ret
30
31 Signed-off-by: Andres Freund <andres@anarazel.de>
32 Acked-by: Quentin Monnet <quentin@isovalent.com>
33 Cc: Alexei Starovoitov <ast@kernel.org>
34 Cc: Ben Hutchings <benh@debian.org>
35 Cc: Jiri Olsa <jolsa@kernel.org>
36 Cc: Quentin Monnet <quentin@isovalent.com>
37 Cc: Sedat Dilek <sedat.dilek@gmail.com>
38 Cc: bpf@vger.kernel.org
39 Link: http://lore.kernel.org/lkml/20220622181918.ykrs5rsnmx3og4sv@alap3.anarazel.de
40 Link: https://lore.kernel.org/r/20220801013834.156015-8-andres@anarazel.de
41 Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
42 (cherry picked from commit 600b7b26c07a070d0153daa76b3806c1e52c9e00)
43 ---
44 tools/bpf/bpftool/Makefile | 5 +++-
45 tools/bpf/bpftool/jit_disasm.c | 42 +++++++++++++++++++++++++++-------
46 2 files changed, 38 insertions(+), 9 deletions(-)
47
48 --- a/tools/bpf/bpftool/Makefile
49 +++ b/tools/bpf/bpftool/Makefile
50 @@ -76,7 +76,7 @@ INSTALL ?= install
51 RM ?= rm -f
52
53 FEATURE_USER = .bpftool
54 -FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
55 +FEATURE_TESTS = libbfd disassembler-four-args disassembler-init-styled reallocarray zlib libcap \
56 clang-bpf-co-re
57 FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
58 clang-bpf-co-re
59 @@ -100,6 +100,9 @@ endif
60 ifeq ($(feature-disassembler-four-args), 1)
61 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
62 endif
63 +ifeq ($(feature-disassembler-init-styled), 1)
64 + CFLAGS += -DDISASM_INIT_STYLED
65 +endif
66
67 ifeq ($(feature-reallocarray), 0)
68 CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
69 --- a/tools/bpf/bpftool/jit_disasm.c
70 +++ b/tools/bpf/bpftool/jit_disasm.c
71 @@ -24,6 +24,7 @@
72 #include <sys/stat.h>
73 #include <limits.h>
74 #include <bpf/libbpf.h>
75 +#include <tools/dis-asm-compat.h>
76
77 #include "json_writer.h"
78 #include "main.h"
79 @@ -39,15 +40,12 @@ static void get_exec_path(char *tpath, s
80 }
81
82 static int oper_count;
83 -static int fprintf_json(void *out, const char *fmt, ...)
84 +static int printf_json(void *out, const char *fmt, va_list ap)
85 {
86 - va_list ap;
87 char *s;
88 int err;
89
90 - va_start(ap, fmt);
91 err = vasprintf(&s, fmt, ap);
92 - va_end(ap);
93 if (err < 0)
94 return -1;
95
96 @@ -73,6 +71,32 @@ static int fprintf_json(void *out, const
97 return 0;
98 }
99
100 +static int fprintf_json(void *out, const char *fmt, ...)
101 +{
102 + va_list ap;
103 + int r;
104 +
105 + va_start(ap, fmt);
106 + r = printf_json(out, fmt, ap);
107 + va_end(ap);
108 +
109 + return r;
110 +}
111 +
112 +static int fprintf_json_styled(void *out,
113 + enum disassembler_style style __maybe_unused,
114 + const char *fmt, ...)
115 +{
116 + va_list ap;
117 + int r;
118 +
119 + va_start(ap, fmt);
120 + r = printf_json(out, fmt, ap);
121 + va_end(ap);
122 +
123 + return r;
124 +}
125 +
126 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
127 const char *arch, const char *disassembler_options,
128 const struct btf *btf,
129 @@ -99,11 +123,13 @@ void disasm_print_insn(unsigned char *im
130 assert(bfd_check_format(bfdf, bfd_object));
131
132 if (json_output)
133 - init_disassemble_info(&info, stdout,
134 - (fprintf_ftype) fprintf_json);
135 + init_disassemble_info_compat(&info, stdout,
136 + (fprintf_ftype) fprintf_json,
137 + fprintf_json_styled);
138 else
139 - init_disassemble_info(&info, stdout,
140 - (fprintf_ftype) fprintf);
141 + init_disassemble_info_compat(&info, stdout,
142 + (fprintf_ftype) fprintf,
143 + fprintf_styled);
144
145 /* Update architecture info for offload. */
146 if (arch) {