Commit 29a9c10e authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Alexei Starovoitov

bpftool: make libbfd optional

Make it possible to build bpftool without libbfd. libbfd and libopcodes are
typically provided in dev/dbg packages (binutils-dev in debian) which we
usually don't have installed on the fleet machines and we'd like a way to have
bpftool version that works without installing any additional packages.
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
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Acked-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent ae9435f6
...@@ -53,7 +53,7 @@ ifneq ($(EXTRA_LDFLAGS),) ...@@ -53,7 +53,7 @@ ifneq ($(EXTRA_LDFLAGS),)
LDFLAGS += $(EXTRA_LDFLAGS) LDFLAGS += $(EXTRA_LDFLAGS)
endif endif
LIBS = -lelf -lbfd -lopcodes $(LIBBPF) LIBS = -lelf $(LIBBPF)
INSTALL ?= install INSTALL ?= install
RM ?= rm -f RM ?= rm -f
...@@ -90,7 +90,16 @@ include $(wildcard $(OUTPUT)*.d) ...@@ -90,7 +90,16 @@ include $(wildcard $(OUTPUT)*.d)
all: $(OUTPUT)bpftool 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 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
......
...@@ -109,7 +109,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, ...@@ -109,7 +109,7 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
if (inf) { if (inf) {
bfdf->arch_info = inf; bfdf->arch_info = inf;
} else { } else {
p_err("No libfd support for %s", arch); p_err("No libbfd support for %s", arch);
return; return;
} }
} }
...@@ -183,3 +183,9 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, ...@@ -183,3 +183,9 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
bfd_close(bfdf); bfd_close(bfdf);
} }
int disasm_init(void)
{
bfd_init();
return 0;
}
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <bfd.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
...@@ -399,8 +398,6 @@ int main(int argc, char **argv) ...@@ -399,8 +398,6 @@ int main(int argc, char **argv)
if (argc < 0) if (argc < 0)
usage(); usage();
bfd_init();
ret = cmd_select(cmds, argc, argv, do_help); ret = cmd_select(cmds, argc, argv, do_help);
if (json_output) if (json_output)
......
...@@ -147,8 +147,22 @@ int prog_parse_fd(int *argc, char ***argv); ...@@ -147,8 +147,22 @@ int prog_parse_fd(int *argc, char ***argv);
int map_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); 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, void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
const char *arch, const char *disassembler_options); const char *arch, const char *disassembler_options);
int 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)
{
}
static inline int disasm_init(void)
{
p_err("No libbfd support");
return -1;
}
#endif
void print_data_json(uint8_t *data, size_t len); void print_data_json(uint8_t *data, size_t len);
void print_hex_data_json(uint8_t *data, size_t len); void print_hex_data_json(uint8_t *data, size_t len);
......
...@@ -467,6 +467,9 @@ static int do_dump(int argc, char **argv) ...@@ -467,6 +467,9 @@ static int do_dump(int argc, char **argv)
int fd; int fd;
if (is_prefix(*argv, "jited")) { if (is_prefix(*argv, "jited")) {
if (disasm_init())
return -1;
member_len = &info.jited_prog_len; member_len = &info.jited_prog_len;
member_ptr = &info.jited_prog_insns; member_ptr = &info.jited_prog_insns;
} else if (is_prefix(*argv, "xlated")) { } else if (is_prefix(*argv, "xlated")) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment