bpftool: make libbfd optional
Subsystems (4)
PATCH 1/1 bpftool: make libbfd optional
Make it possible to build bpftool without libbfd. This excludes support for disassembling jit-ted code and prints an error if the user tries to use these features.
Tested by:
cat > FEATURES_DUMP.bpftool <<EOF
feature-libbfd=0
feature-disassembler-four-args=1
feature-reallocarray=0
feature-libelf=1
feature-libelf-mmap=1
feature-bpf=1
EOF
FEATURES_DUMP=$PWD/FEATURES_DUMP.bpftool make
ldd bpftool | grep libbfd
--- tools/bpf/bpftool/Makefile | 13 +++++++++++-- tools/bpf/bpftool/jit_disasm.c | 7 ++++++- tools/bpf/bpftool/main.c | 3 +-- tools/bpf/bpftool/main.h | 11 +++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-)
tools/bpf/bpftool/Makefile +11 -2
@@ -53,7 +53,7 @@ ifneq ($(EXTRA_LDFLAGS),) LDFLAGS += $(EXTRA_LDFLAGS) endif -LIBS = -lelf -lbfd -lopcodes $(LIBBPF) +LIBS = -lelf $(LIBBPF) INSTALL ?= install RM ?= rm -f
@@ -90,7 +90,16 @@ include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool -SRCS = $(wildcard *.c) +BFD_SRCS = jit_disasm.c + +SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) + +ifeq ($(feature-libbfd),1) +CFLAGS += -DHAVE_LIBBFD_SUPPORT +SRCS += $(BFD_SRCS) +LIBS += -lbfd -lopcodes +endif + OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
tools/bpf/bpftool/jit_disasm.c +6 -1
@@ -109,7 +109,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,if (inf) { bfdf->arch_info = inf; }else { -p_err ("No libfd support for %s" , arch); +p_err ("No libbfd support for %s" , arch);return ; } }
@@ -183,3 +183,8 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,bfd_close (bfdf); } + +void disasm_init (void ) +{ +bfd_init (); +}
tools/bpf/bpftool/main.c +1 -2
@@ -31,7 +31,6 @@* SOFTWARE. */ -#include <bfd.h> #include <ctype.h> #include <errno.h> #include <getopt.h>
@@ -399,7 +398,7 @@ int main(int argc, char **argv)if (argc <0 )usage (); -bfd_init (); +disasm_init (); ret =cmd_select (cmds, argc, argv, do_help);
tools/bpf/bpftool/main.h +11 -0
@@ -147,8 +147,19 @@ int prog_parse_fd(int *argc, char ***argv);int map_parse_fd (int *argc,char ***argv);int map_parse_fd_and_info (int *argc,char ***argv,void *info,__u32 *info_len); +#ifdef HAVE_LIBBFD_SUPPORT void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes,const char *arch,const char *disassembler_options); +void disasm_init (void ); +#else +static inline +void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes, +const char *arch,const char *disassembler_options) +{ +p_err ("No libbfd support" ); +} +static inline void disasm_init (void ) {} +#endif void print_data_json (uint8_t *data,size_t len);void print_hex_data_json (uint8_t *data,size_t len);
Signature · 1 line
2.19.1.930.g4563a0d9d0-goog
Quoted 16 lines · Stanislav Fomichev
Make it possible to build bpftool without libbfd. This excludes support for disassembling jit-ted code and prints an error if the user tries to use these features.
Tested by:
cat > FEATURES_DUMP.bpftool <<EOF
feature-libbfd=0
feature-disassembler-four-args=1
feature-reallocarray=0
feature-libelf=1
feature-libelf-mmap=1
feature-bpf=1
EOF
FEATURES_DUMP=$PWD/FEATURES_DUMP.bpftool make
ldd bpftool | grep libbfd
Quoted 1 line
Signed-off-by Stanislav Fomichev <[email protected]>
Would you mind spelling out the motivation?
Quoted 9 lines
tools/bpf/bpftool/main.h +1
-0@@ -147,8 +147,19 @@ int prog_parse_fd(int *argc, char ***argv);int map_parse_fd (int *argc,char ***argv);int map_parse_fd_and_info (int *argc,char ***argv,void *info,__u32 *info_len); +#ifdef HAVE_LIBBFD_SUPPORT
tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 160
Quoted 10 lines
tools/bpf/bpftool/main.h +8
-0void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes,const char *arch,const char *disassembler_options); +void disasm_init (void ); +#else +static inline +void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes, +const char *arch,const char *disassembler_options) +{ +p_err ("No libbfd support" ); +}
I think an error per instruction is a bit much, could we make sure we error out earlier?
tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 164
Quoted 4 lines
tools/bpf/bpftool/main.h +2
-0+static inline void disasm_init (void ) {} +#endif void print_data_json (uint8_t *data,size_t len);void print_hex_data_json (uint8_t *data,size_t len);
Otherwise LGTM.
Quoted 17 lines · Jakub Kicinski
Quoted 16 lines · Stanislav Fomichev
Make it possible to build bpftool without libbfd. This excludes support for disassembling jit-ted code and prints an error if the user tries to use these features.
Tested by:
cat > FEATURES_DUMP.bpftool <<EOF
feature-libbfd=0
feature-disassembler-four-args=1
feature-reallocarray=0
feature-libelf=1
feature-libelf-mmap=1
feature-bpf=1
EOF
FEATURES_DUMP=$PWD/FEATURES_DUMP.bpftool make
ldd bpftool | grep libbfd
Quoted 3 lines
Quoted 1 line
Signed-off-by Stanislav Fomichev <[email protected]>Would you mind spelling out the motivation?
Ack, will do in v2 commit message. tldr - some fleet machines don't have dev/dbg stuff installed; would like an option to have bpftool that works everywhere for introspection.
Quoted 8 lines
Quoted 8 lines
tools/bpf/bpftool/main.h +0
-0@@ -147,8 +147,19 @@ int prog_parse_fd(int *argc, char ***argv);int map_parse_fd (int *argc,char ***argv);int map_parse_fd_and_info (int *argc,char ***argv,void *info,__u32 *info_len);
Quoted 14 lines
tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 160
Quoted 11 lines
tools/bpf/bpftool/main.h +9
-0+#ifdef HAVE_LIBBFD_SUPPORT void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes,const char *arch,const char *disassembler_options); +void disasm_init (void ); +#else +static inline +void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes, +const char *arch,const char *disassembler_options) +{ +p_err ("No libbfd support" ); +}I think an error per instruction is a bit much, could we make sure we error out earlier?
I can return int from disasm_print_insn as an indication to stop/continue. Let me know if you have better ideas, will post v2 later today.
Quoted 6 lines
tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 164
Quoted 4 lines
tools/bpf/bpftool/main.h +2
-0+static inline void disasm_init (void ) {} +#endif void print_data_json (uint8_t *data,size_t len);void print_hex_data_json (uint8_t *data,size_t len);Otherwise LGTM.
Thanks!
Quoted 25 lines · Stanislav Fomichev
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · Stanislav Fomichev
tools/bpf/bpftool/main.h +0
-0@@ -147,8 +147,19 @@ int prog_parse_fd(int *argc, char ***argv);int map_parse_fd (int *argc,char ***argv);int map_parse_fd_and_info (int *argc,char ***argv,void *info,__u32 *info_len);Quoted 14 lines
tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 160
Quoted 11 lines
tools/bpf/bpftool/main.h +9
-0+#ifdef HAVE_LIBBFD_SUPPORT void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes,const char *arch,const char *disassembler_options); +void disasm_init (void ); +#else +static inline +void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes, +const char *arch,const char *disassembler_options) +{ +p_err ("No libbfd support" ); +}I think an error per instruction is a bit much, could we make sure we error out earlier?
Quoted 3 lines
I can return int from
disasm_print_insnas an indication to stop/continue. Let me know if you have better ideas, will post v2 later today.
Why not simply:
tools/bpf/bpftool/prog.c +4 -0
@@ -466,6 +466,10 @@ static int do_dump(int argc, char **argv)int fd;if (is_prefix (*argv,"jited" )) { +#ifndef HAVE_LIBBFD_SUPPORT +p_err ("No libbfd support" ); +return -1 ; +#endif member_len = &info.jited_prog_len; member_ptr = &info.jited_prog_insns; }else if (is_prefix (*argv,"xlated" )) {
Perhaps wrapped into some helper in the header to avoid the yuckiness.
Quoted 26 lines · Jakub Kicinski
Quoted 25 lines · Stanislav Fomichev
Quoted 23 lines · Jakub Kicinski
Quoted 9 lines · Stanislav Fomichev
tools/bpf/bpftool/main.h +1
-0@@ -147,8 +147,19 @@ int prog_parse_fd(int *argc, char ***argv);int map_parse_fd (int *argc,char ***argv);int map_parse_fd_and_info (int *argc,char ***argv,void *info,__u32 *info_len); +#ifdef HAVE_LIBBFD_SUPPORT tools/bpf/bpftool/main.h int prog_parse_fd(int *argc, char ***argv); · line 160
Quoted 10 lines
tools/bpf/bpftool/main.h +8
-0void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes,const char *arch,const char *disassembler_options); +void disasm_init (void ); +#else +static inline +void disasm_print_insn (unsigned char *image,ssize_t len,int opcodes, +const char *arch,const char *disassembler_options) +{ +p_err ("No libbfd support" ); +}I think an error per instruction is a bit much, could we make sure we error out earlier?
Quoted 5 lines
Quoted 3 lines
I can return int from
disasm_print_insnas an indication to stop/continue. Let me know if you have better ideas, will post v2 later today.Why not simply:
Makes sense. I think I can actually repurpose disasm_init for that (the function I added to wrap bfd_init, I think I can move the callsite here without any problems).
Quoted 17 lines
tools/bpf/bpftool/prog.c +4
-0@@ -466,6 +466,10 @@ static int do_dump(int argc, char **argv)int fd;if (is_prefix (*argv,"jited" )) { +#ifndef HAVE_LIBBFD_SUPPORT +p_err ("No libbfd support" ); +return -1 ; +#endif member_len = &info.jited_prog_len; member_ptr = &info.jited_prog_insns; }else if (is_prefix (*argv,"xlated" )) {Perhaps wrapped into some helper in the header to avoid the yuckiness.