Skip to contentSkip to conversation

tools/bpf: fix build with binutils >= 2.28

RFC PATCH In review Roman Gushchin 2 participants 6 messages Dec 19, 2017 – Dec 21, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 tools/bpf: fix build with binutils >= 2.28

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by checking binutils version and use an appropriate disassembler() signature.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile             | 6 ++++++
 tools/bpf/bpf_jit_disasm.c     | 7 +++++++
 tools/bpf/bpftool/Makefile     | 6 ++++++
 tools/bpf/bpftool/jit_disasm.c | 5 +++++
 4 files changed, 24 insertions(+)
tools/bpf/Makefile +6 -0
index 07a6697466ef..3fd32fd0daa1 100644
@@ -6,8 +6,14 @@ LEX = flex
 YACC = bison
 MAKE = make
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
 
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
tools/bpf/bpf_jit_disasm.c +7 -0
index 75bf526a0168..3ef7c8bdc0f3 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +6 -0
index 3f17ad317512..94ad51bf14b5 100644
@@ -40,6 +40,12 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/
 LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
+
 INSTALL ?= install
 RM ?= rm -f
 
tools/bpf/bpftool/jit_disasm.c +5 -0
index 1551d3918d4c..eaa7127e9eeb 100644
@@ -107,7 +107,12 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf), bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf), bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	if (json_output)
Signature · 1 line
2.14.3
#1 lore raw

Hi Roman, thanks for working on this!

2017-12-19 14:38 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 62 lines

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by checking binutils version and use an appropriate disassembler() signature.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile             | 6 ++++++
 tools/bpf/bpf_jit_disasm.c     | 7 +++++++
 tools/bpf/bpftool/Makefile     | 6 ++++++
 tools/bpf/bpftool/jit_disasm.c | 5 +++++
 4 files changed, 24 insertions(+)
tools/bpf/Makefile +6 -0
index 07a6697466ef..3fd32fd0daa1 100644
@@ -6,8 +6,14 @@ LEX = flex
 YACC = bison
 MAKE = make
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
 
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
tools/bpf/bpf_jit_disasm.c +7 -0
index 75bf526a0168..3ef7c8bdc0f3 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +0 -0
index 3f17ad317512..94ad51bf14b5 100644

tools/bpf/bpftool/Makefile CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow · line 43

Quoted 4 lines
tools/bpf/bpftool/Makefile +1 -0
@@ -40,6 +40,12 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/
 LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))

This does not seem to be portable. I tried that on Ubuntu and readelf -v returns "GNU readelf (GNU Binutils for Ubuntu) 2.26.1", and BINUTILS_VER catches "Binutils".

Quoted 15 lines
tools/bpf/bpftool/Makefile +5 -0
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
+
 INSTALL ?= install
 RM ?= rm -f
 
tools/bpf/bpftool/jit_disasm.c +0 -0
index 1551d3918d4c..eaa7127e9eeb 100644
@@ -107,7 +107,12 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);

tools/bpf/bpftool/jit_disasm.c void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes) · line 118

Quoted 10 lines
tools/bpf/bpftool/jit_disasm.c +5 -0
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf), bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf), bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	if (json_output)

I discussed this issue with Jakub recently, and one suggestion he had was to look in tools/build/feature to add a new "feature", by trying to compile short programs, for making the distinction between binutils versions. It probably requires more work, but could be more robust than parsing the version from the command line?

Best,
Quentin

#2 lore raw
Quoted 98 lines · Quentin Monnet

Hi Roman, thanks for working on this!

2017-12-19 14:38 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 62 lines

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by checking binutils version and use an appropriate disassembler() signature.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile             | 6 ++++++
 tools/bpf/bpf_jit_disasm.c     | 7 +++++++
 tools/bpf/bpftool/Makefile     | 6 ++++++
 tools/bpf/bpftool/jit_disasm.c | 5 +++++
 4 files changed, 24 insertions(+)
tools/bpf/Makefile +6 -0
index 07a6697466ef..3fd32fd0daa1 100644
@@ -6,8 +6,14 @@ LEX = flex
 YACC = bison
 MAKE = make
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
 
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
tools/bpf/bpf_jit_disasm.c +7 -0
index 75bf526a0168..3ef7c8bdc0f3 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +0 -0
index 3f17ad317512..94ad51bf14b5 100644

tools/bpf/bpftool/Makefile CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow · line 43

Quoted 4 lines
tools/bpf/bpftool/Makefile +1 -0
@@ -40,6 +40,12 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/
 LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))

This does not seem to be portable. I tried that on Ubuntu and readelf -v returns "GNU readelf (GNU Binutils for Ubuntu) 2.26.1", and BINUTILS_VER catches "Binutils".

Quoted 24 lines
tools/bpf/bpftool/Makefile +5 -0
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
+
 INSTALL ?= install
 RM ?= rm -f
 
tools/bpf/bpftool/jit_disasm.c +5 -0
index 1551d3918d4c..eaa7127e9eeb 100644
@@ -107,7 +107,12 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf), bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf), bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
Quoted 7 lines

tools/bpf/bpftool/jit_disasm.c void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes) · line 118

Quoted 1 line
tools/bpf/bpftool/jit_disasm.c +0 -0
 	if (json_output)

I discussed this issue with Jakub recently, and one suggestion he had was to look in tools/build/feature to add a new "feature", by trying to compile short programs, for making the distinction between binutils versions. It probably requires more work, but could be more robust than parsing the version from the command line?

Hm, might be an option. Parsing readelf output is pretty ugly, here I agree. In general it feels more like a binutils issue, so we have to workaround it in either way.

Is Jakub or someone else working on it?

Thanks!

#3 lore raw

2017-12-19 16:10 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 107 lines · Quentin Monnet
Quoted 98 lines

Hi Roman, thanks for working on this!

2017-12-19 14:38 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 62 lines

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by checking binutils version and use an appropriate disassembler() signature.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile             | 6 ++++++
 tools/bpf/bpf_jit_disasm.c     | 7 +++++++
 tools/bpf/bpftool/Makefile     | 6 ++++++
 tools/bpf/bpftool/jit_disasm.c | 5 +++++
 4 files changed, 24 insertions(+)
tools/bpf/Makefile +6 -0
index 07a6697466ef..3fd32fd0daa1 100644
@@ -6,8 +6,14 @@ LEX = flex
 YACC = bison
 MAKE = make
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
 
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
tools/bpf/bpf_jit_disasm.c +7 -0
index 75bf526a0168..3ef7c8bdc0f3 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +0 -0
index 3f17ad317512..94ad51bf14b5 100644

tools/bpf/bpftool/Makefile CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow · line 43

Quoted 4 lines
tools/bpf/bpftool/Makefile +1 -0
@@ -40,6 +40,12 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/
 LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 
+BINUTILS_VER := $(word 4, $(shell readelf -v | head -n 1))

This does not seem to be portable. I tried that on Ubuntu and readelf -v returns "GNU readelf (GNU Binutils for Ubuntu) 2.26.1", and BINUTILS_VER catches "Binutils".

Quoted 24 lines
tools/bpf/bpftool/Makefile +5 -0
+BINUTILS_VER_MAJ := $(word 1, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+BINUTILS_VER_MIN := $(word 2, $(subst ., , $(subst -, , ${BINUTILS_VER})))
+CFLAGS += -DBINUTILS_VER_MAJ=${BINUTILS_VER_MAJ}
+CFLAGS += -DBINUTILS_VER_MIN=${BINUTILS_VER_MIN}
+
 INSTALL ?= install
 RM ?= rm -f
 
tools/bpf/bpftool/jit_disasm.c +5 -0
index 1551d3918d4c..eaa7127e9eeb 100644
@@ -107,7 +107,12 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#if (BINUTILS_VER_MAJ >= 2) && (BINUTILS_VER_MIN >= 28)
+	disassemble = disassembler(bfd_get_arch(bfdf), bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf), bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
Quoted 7 lines

tools/bpf/bpftool/jit_disasm.c void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes) · line 118

Quoted 1 line
tools/bpf/bpftool/jit_disasm.c +0 -0
 	if (json_output)

I discussed this issue with Jakub recently, and one suggestion he had was to look in tools/build/feature to add a new "feature", by trying to compile short programs, for making the distinction between binutils versions. It probably requires more work, but could be more robust than parsing the version from the command line?

Quoted 8 lines

Hm, might be an option. Parsing readelf output is pretty ugly, here I agree. In general it feels more like a binutils issue, so we have to workaround it in either way.

Is Jakub or someone else working on it?

Thanks!

Jakub isn't. On our side, I noticed last week that there was this change in binutils, and started to have a look at how these "features" work. But I have nothing that works so far, so feel free to tackle this.

Quentin

#4 lore raw
Quoted 17 lines · Quentin Monnet

2017-12-19 16:10 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 16 lines · Quentin Monnet
Quoted 8 lines

Hi Roman, thanks for working on this!

I discussed this issue with Jakub recently, and one suggestion he had was to look in tools/build/feature to add a new "feature", by trying to compile short programs, for making the distinction between binutils versions. It probably requires more work, but could be more robust than parsing the version from the command line?

Hm, might be an option. Parsing readelf output is pretty ugly, here I agree. In general it feels more like a binutils issue, so we have to workaround it in either way.

Is Jakub or someone else working on it?

Quoted 8 lines
Quoted 2 lines

Thanks!

Jakub isn't. On our side, I noticed last week that there was this change in binutils, and started to have a look at how these "features" work. But I have nothing that works so far, so feel free to tackle this.

Quentin

Hi Quentin!

Can you, please, check that the patch below works in your environment.

Thanks!

--

Quoted 1 line

From b08deabf42e4c143b9e0eec8c49714e4d2c928e3 Mon Sep 17 00:00:00 2001

From: Roman Gushchin <[email protected]>
Date: Wed, 20 Dec 2017 13:27:32 +0000
Subject: [RFC PATCH net-next] tools/bpftool: fix bpftool build with bintutils

Quoted 1 line

= 2.8

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by adding a new "feature" to the tools/build/features infrastructure and make it responsible for decision which disassembler() function signature to use.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile                      | 18 ++++++++++++++++++
 tools/bpf/bpf_jit_disasm.c              |  7 +++++++
 tools/bpf/bpftool/Makefile              | 13 +++++++++++++
 tools/bpf/bpftool/jit_disasm.c          |  7 +++++++
 tools/build/feature/Makefile            |  4 ++++
 tools/build/feature/test-disassembler.c | 15 +++++++++++++++
 6 files changed, 64 insertions(+)
 create mode 100644 tools/build/feature/test-disassembler.c
tools/bpf/Makefile +18 -0
index 07a6697466ef..c62b3a311486 100644
@@ -9,6 +9,24 @@ MAKE = make
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
 
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+endif
+
+FEATURE_TESTS = disassembler
+FEATURE_DISPLAY = disassembler
+
+ifeq ($(FEATURES_DUMP),)
+include $(srctree)/tools/build/Makefile.feature
+else
+include $(FEATURES_DUMP)
+endif
+
+ifeq ($(feature-disassembler), 1)
+CFLAGS += -DNEW_DISSASSEMBLER_SIGNATURE
+endif
+
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
 
tools/bpf/bpf_jit_disasm.c +7 -0
index 75bf526a0168..a5f4dbacdb11 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#ifdef NEW_DISSASSEMBLER_SIGNATURE
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +13 -0
index 3f17ad317512..9c089cfa5f3f 100644
@@ -43,6 +43,19 @@ LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 INSTALL ?= install
 RM ?= rm -f
 
+FEATURE_TESTS = disassembler
+FEATURE_DISPLAY = disassembler
+
+ifeq ($(FEATURES_DUMP),)
+include $(srctree)/tools/build/Makefile.feature
+else
+include $(FEATURES_DUMP)
+endif
+
+ifeq ($(feature-disassembler), 1)
+CFLAGS += -DNEW_DISSASSEMBLER_SIGNATURE
+endif
+
 include $(wildcard *.d)
 
 all: $(OUTPUT)bpftool
tools/bpf/bpftool/jit_disasm.c +7 -0
index 1551d3918d4c..8295e2f14ed7 100644
@@ -107,7 +107,14 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#ifdef NEW_DISSASSEMBLER_SIGNATURE
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	if (json_output)
tools/build/feature/Makefile +4 -0
index 96982640fbf8..91f937943918 100644
@@ -13,6 +13,7 @@ FILES=                                          \
          test-hello.bin                         \
          test-libaudit.bin                      \
          test-libbfd.bin                        \
+         test-disassembler.bin                  \
          test-liberty.bin                       \
          test-liberty-z.bin                     \
          test-cplus-demangle.bin                \
@@ -188,6 +189,9 @@ $(OUTPUT)test-libpython-version.bin:
 $(OUTPUT)test-libbfd.bin:
 	$(BUILD) -DPACKAGE='"perf"' -lbfd -lz -liberty -ldl
 
+$(OUTPUT)test-disassembler.bin:
+	$(BUILD) -lopcodes
+
 $(OUTPUT)test-liberty.bin:
 	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
 
tools/build/feature/test-disassembler.c new file +15 -0
new file mode 100644
index 000000000000..45ce65cfddf0
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <bfd.h>
+#include <dis-asm.h>
+
+int main(void)
+{
+	bfd *abfd = bfd_openr(NULL, NULL);
+
+	disassembler(bfd_get_arch(abfd),
+		     bfd_big_endian(abfd),
+		     bfd_get_mach(abfd),
+		     abfd);
+
+	return 0;
+}
Signature · 1 line
2.14.3
#5 lore raw

2017-12-20 18:32 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 25 lines · Quentin Monnet
Quoted 24 lines

2017-12-19 16:10 UTC+0000 ~ Roman Gushchin <[email protected]>

Quoted 10 lines · Quentin Monnet
Quoted 8 lines

Hi Roman, thanks for working on this!

I discussed this issue with Jakub recently, and one suggestion he had was to look in tools/build/feature to add a new "feature", by trying to compile short programs, for making the distinction between binutils versions. It probably requires more work, but could be more robust than parsing the version from the command line?

Quoted 8 lines

Hm, might be an option. Parsing readelf output is pretty ugly, here I agree. In general it feels more like a binutils issue, so we have to workaround it in either way.

Is Jakub or someone else working on it?

Thanks!

Jakub isn't. On our side, I noticed last week that there was this change in binutils, and started to have a look at how these "features" work. But I have nothing that works so far, so feel free to tackle this.

Quoted 7 lines
Quoted 1 line

Quentin

Hi Quentin!

Can you, please, check that the patch below works in your environment.

Thanks!

Hi Roman,

It failed for me, but I could make it work with just a small fix in tools/build/feature/Makefile, see below. I also add some generic comments inline.

Quoted 38 lines

--

From b08deabf42e4c143b9e0eec8c49714e4d2c928e3 Mon Sep 17 00:00:00 2001
From: Roman Gushchin <[email protected]>
Date: Wed, 20 Dec 2017 13:27:32 +0000
Subject: [RFC PATCH net-next] tools/bpftool: fix bpftool build with bintutils

Quoted 1 line

= 2.8

Bpftool build is broken with binutils version 2.28 and later. The cause is commit 003ca0fd2286 ("Refactor disassembler selection") in the binutils repo, which changed the disassembler() function signature.

Fix this by adding a new "feature" to the tools/build/features infrastructure and make it responsible for decision which disassembler() function signature to use.

Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/bpf/Makefile                      | 18 ++++++++++++++++++
 tools/bpf/bpf_jit_disasm.c              |  7 +++++++
 tools/bpf/bpftool/Makefile              | 13 +++++++++++++
 tools/bpf/bpftool/jit_disasm.c          |  7 +++++++
 tools/build/feature/Makefile            |  4 ++++
 tools/build/feature/test-disassembler.c | 15 +++++++++++++++
 6 files changed, 64 insertions(+)
 create mode 100644 tools/build/feature/test-disassembler.c
tools/bpf/Makefile +0 -0
index 07a6697466ef..c62b3a311486 100644

tools/bpf/Makefile MAKE = make · line 16

Quoted 8 lines
tools/bpf/Makefile +5 -0
@@ -9,6 +9,24 @@ MAKE = make
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I../../include/uapi -I../../include
 
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+endif
+

libbpf has a FEATURE_USER = .libbpf here, that seems to be used as a suffix for file FEATURE-DUMP. Not sure what it is used for, but could be a good practice, so we should check this maybe.

tools/bpf/Makefile MAKE = make · line 18

Quoted 2 lines
tools/bpf/Makefile +2 -0
+FEATURE_TESTS = disassembler
+FEATURE_DISPLAY = disassembler

While at it, could you add the "bfd" feature verification before "disassembler"? The former already exists in tools/build/feature. Logically feature "disassembler" would fail to be detected if bfd is missing, but that would seem cleaner to me.

tools/bpf/Makefile MAKE = make · line 28

Quoted 10 lines
tools/bpf/Makefile +10 -0
+
+ifeq ($(FEATURES_DUMP),)
+include $(srctree)/tools/build/Makefile.feature
+else
+include $(FEATURES_DUMP)
+endif
+
+ifeq ($(feature-disassembler), 1)
+CFLAGS += -DNEW_DISSASSEMBLER_SIGNATURE
+endif

Nice, but this means that we check the feature for all targets. This is not necessary, in particular in bpftool Makefile below, this means that we check the feature even on make doc for example.

libbpf has something in its Makefile to prevent that, you may want to have a look at its check_feat variable.

Also checking the feature compiles some files in the tree that are not removed with existing "clean" target. Again, I use libbpf as a reference (see config-clean target, and removal of FEATURE-DUMP.

tools/bpf/bpf_jit_disasm.c static void get_asm_insns(uint8_t *image, size_t len, int opcodes) · line 75

Quoted 13 lines
tools/bpf/Makefile +1 -0
+
 %.yacc.c: %.y
 	$(YACC) -o $@ -d $<
 
tools/bpf/bpf_jit_disasm.c +1 -0
index 75bf526a0168..a5f4dbacdb11 100644
@@ -72,7 +72,14 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#ifdef NEW_DISSASSEMBLER_SIGNATURE

Could we find another macro name? It makes perfect sense today, but maybe no so much in the future, in particular if the prototype were to change again. Something like BINUTILS_VERSION_GEQ_2_29, or DISASM_FOUR_ARGS_SIGNATURE?

Also for the name of the feature ("disassembler" in your patch) we might want to find something more explicit, we're not trying to check whether the disassembler simply exists. "diassembler-four-args"? Any better idea?

tools/bpf/bpf_jit_disasm.c static void get_asm_insns(uint8_t *image, size_t len, int opcodes) · line 79

Quoted 4 lines
tools/bpf/bpf_jit_disasm.c +4 -0
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);

Nit: we already have some values in info.arch and info.mach, no need to call the functions again.

tools/bpf/bpftool/Makefile

Quoted 10 lines
tools/bpf/bpf_jit_disasm.c +2 -0
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	do {
tools/bpf/bpftool/Makefile +0 -0
index 3f17ad317512..9c089cfa5f3f 100644

(Same comments as for previous Makefile.)

Quoted 52 lines
tools/bpf/bpftool/Makefile +13 -0
@@ -43,6 +43,19 @@ LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
 INSTALL ?= install
 RM ?= rm -f
 
+FEATURE_TESTS = disassembler
+FEATURE_DISPLAY = disassembler
+
+ifeq ($(FEATURES_DUMP),)
+include $(srctree)/tools/build/Makefile.feature
+else
+include $(FEATURES_DUMP)
+endif
+
+ifeq ($(feature-disassembler), 1)
+CFLAGS += -DNEW_DISSASSEMBLER_SIGNATURE
+endif
+
 include $(wildcard *.d)
 
 all: $(OUTPUT)bpftool
tools/bpf/bpftool/jit_disasm.c +7 -0
index 1551d3918d4c..8295e2f14ed7 100644
@@ -107,7 +107,14 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 
 	disassemble_init_for_target(&info);
 
+#ifdef NEW_DISSASSEMBLER_SIGNATURE
+	disassemble = disassembler(bfd_get_arch(bfdf),
+				   bfd_big_endian(bfdf),
+				   bfd_get_mach(bfdf),
+				   bfdf);
+#else
 	disassemble = disassembler(bfdf);
+#endif
 	assert(disassemble);
 
 	if (json_output)
tools/build/feature/Makefile +1 -0
index 96982640fbf8..91f937943918 100644
@@ -13,6 +13,7 @@ FILES=                                          \
          test-hello.bin                         \
          test-libaudit.bin                      \
          test-libbfd.bin                        \
+         test-disassembler.bin                  \
          test-liberty.bin                       \
          test-liberty-z.bin                     \
          test-cplus-demangle.bin                \

tools/build/feature/Makefile $(OUTPUT)test-libpython-version.bin: · line 193

Quoted 5 lines
tools/build/feature/Makefile +2 -0
@@ -188,6 +189,9 @@ $(OUTPUT)test-libpython-version.bin:
 $(OUTPUT)test-libbfd.bin:
 	$(BUILD) -DPACKAGE='"perf"' -lbfd -lz -liberty -ldl
 
+$(OUTPUT)test-disassembler.bin:
+	$(BUILD) -lopcodes

This is where it failed for me. Functions bfd_get_arch(), bfd_get_mach() and bfd_big_endian() are not in lib opcodes, so I had to add -lbfd to this line. With this fix, I got the feature being detected normally (or not, if the machine still has the old version of binutils).

Quoted 16 lines
tools/build/feature/Makefile +1 -0
+
 $(OUTPUT)test-liberty.bin:
 	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
 
tools/build/feature/test-disassembler.c new file +5 -0
new file mode 100644
index 000000000000..45ce65cfddf0
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <bfd.h>
+#include <dis-asm.h>
+
+int main(void)

tools/build/feature/test-disassembler.c line 15

Quoted 10 lines
tools/build/feature/test-disassembler.c new file +10 -0
+{
+	bfd *abfd = bfd_openr(NULL, NULL);
+
+	disassembler(bfd_get_arch(abfd),
+		     bfd_big_endian(abfd),
+		     bfd_get_mach(abfd),
+		     abfd);
+
+	return 0;
+}

Overall, looks like a nice fix for compiling bpftool, thanks a lot!

Quentin