[PATCH bpf v2] bpftool: Allow to link libbpf dynamically

Toke Høiland-Jørgensen
2019-11-28T14:53:32Z

From: Jiri Olsa <jolsa@kernel.org>

Currently we support only static linking with kernel's libbpf
(tools/lib/bpf). This patch adds LIBBPF_DYNAMIC compile variable
that triggers libbpf detection and bpf dynamic linking:

$ make -C tools/bpf/bpftool make LIBBPF_DYNAMIC=1

If libbpf is not installed, build (with LIBBPF_DYNAMIC=1) stops with:

$ make -C tools/bpf/bpftool LIBBPF_DYNAMIC=1
Auto-detecting system features:
... libbfd: [ on ]
... disassembler-four-args: [ on ]
... zlib: [ on ]
... libbpf: [ OFF ]

Makefile:102: *** Error: No libbpf devel library found, please install-devel or libbpf-dev.

Adding LIBBPF_DIR compile variable to allow linking with
libbpf installed into specific directory:

$ make -C tools/lib/bpf/ prefix=/tmp/libbpf/ install_lib install_headers
$ make -C tools/bpf/bpftool/ LIBBPF_DYNAMIC=1 LIBBPF_DIR=/tmp/libbpf/

It might be needed to clean build tree first because features
framework does not detect the change properly:

$ make -C tools/build/feature clean
$ make -C tools/bpf/bpftool/ clean

Since bpftool uses bits of libbpf that are not exported as public API in
the .so version, we also pass in libbpf.a to the linker, which allows it to
pick up the private functions from the static library without having to
expose them as ABI.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
v2:
- Pass .a file to linker when dynamically linking, so bpftool can use
private functions from libbpf without exposing them as API.

tools/bpf/bpftool/Makefile | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index 39bc6f0f4f0b..397051ed9e41 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -1,6 +1,15 @@
# SPDX-License-Identifier: GPL-2.0-only
+# LIBBPF_DYNAMIC to enable libbpf dynamic linking.
+
include ../../scripts/Makefile.include
include ../../scripts/utilities.mak
+include ../../scripts/Makefile.arch
+
+ifeq ($(LP64), 1)
+ libdir_relative = lib64
+else
+ libdir_relative = lib
+endif

ifeq ($(srctree),)
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
@@ -55,7 +64,7 @@ ifneq ($(EXTRA_LDFLAGS),)
LDFLAGS += $(EXTRA_LDFLAGS)
endif

-LIBS = $(LIBBPF) -lelf -lz
+LIBS = -lelf -lz

INSTALL ?= install
RM ?= rm -f
@@ -63,6 +72,19 @@ RM ?= rm -f
FEATURE_USER = .bpftool
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib
FEATURE_DISPLAY = libbfd disassembler-four-args zlib
+ifdef LIBBPF_DYNAMIC
+ FEATURE_TESTS += libbpf
+ FEATURE_DISPLAY += libbpf
+
+ # for linking with debug library run:
+ # make LIBBPF_DYNAMIC=1 LIBBPF_DIR=/opt/libbpf
+ ifdef LIBBPF_DIR
+ LIBBPF_CFLAGS := -I$(LIBBPF_DIR)/include
+ LIBBPF_LDFLAGS := -L$(LIBBPF_DIR)/$(libdir_relative)
+ FEATURE_CHECK_CFLAGS-libbpf := $(LIBBPF_CFLAGS)
+ FEATURE_CHECK_LDFLAGS-libbpf := $(LIBBPF_LDFLAGS)
+ endif
+endif

check_feat := 1
NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
@@ -88,6 +110,20 @@ ifeq ($(feature-reallocarray), 0)
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
endif

+ifdef LIBBPF_DYNAMIC
+ ifeq ($(feature-libbpf), 1)
+ # bpftool uses non-exported functions from libbpf, so pass both dynamic and
+ # static versions and let the linker figure it out
+ LIBS := -lbpf $(LIBBPF) $(LIBS)
+ CFLAGS += $(LIBBPF_CFLAGS)
+ LDFLAGS += $(LIBBPF_LDFLAGS)
+ else
+ dummy := $(error Error: No libbpf devel library found, please install-devel or libbpf-dev.)
+ endif
+else
+ LIBS := $(LIBBPF) $(LIBS)
+endif
+
include $(wildcard $(OUTPUT)*.d)

all: $(OUTPUT)bpftool
--
2.24.0