060-v6.0-05-tools-bpftool-Fix-compilation-error-with-new-binutil.patch 4.3 KB

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