bpftool: Make the LLVM disassembler an optional runtime dependency
v3 cover bpftool: Make the LLVM disassembler an optional runtime dependency
libLLVM (~140 MiB on Fedora) is a hard dependency of bpftool, although it is only used by "bpftool prog dump jited". This series moves the LLVM disassembler to a plugin that bpftool loads on demand, so that distributions can make it optional. Patch 1 fixes a leak found on the way.
Changes in v3:
- Look for the plugin next to the bpftool binary before its install directory, so that bpftool run from its build directory does not pick
an older installed plugin (Sashiko)
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Move the LLVMDisasmDispose() fix to a separate patch (Quentin)
- Look for the plugin next to the bpftool binary when it is not installed, so that bpftool works from its build directory (Quentin, Sashiko). Not in the current directory: bpftool usually runs as root.
- Export a single versioned structure of callbacks from the plugin (Quentin)
- Only report "llvm" in "bpftool version" if the plugin loads (Quentin)
- Handle dlerror() returning NULL (Quentin)
- Rebuild jit_disasm.o if libdir changes between "make" and
"make install" (Quentin)
- uninstall: use QUIET_UNINST, remove $(libdir)/bpftool (Quentin)
- With a static libLLVM, which a static bpftool requires, link the disassembler into bpftool as before (Sashiko). This drops
LLVM_LINK_STATIC: static LLVM archives are never linked into a shared object.
- Commit log and comment fixes (Quentin, Sashiko)
- Rebase on bpf-next
- Link to v1: https://lore.kernel.org/r/[email protected]
Tested on Fedora 44 x86_64, LLVM 22: plugin, built-in and SKIP_LLVM builds; plugin lookup (installed, build directory, missing, wrong ABI); install/uninstall; "prog dump jited" (plain and JSON) on live programs, and disassembly of aarch64 code through the plugin. Not tested: a fully static build (no static libraries available).
The v2 changes were written with the help of an AI assistant (Claude), from the review comments; I reviewed and tested them.
---
Mark Korenberg (2):
bpftool: Fix disposal of the LLVM disassembler context
bpftool: Make the LLVM disassembler an optional runtime dependency
tools/bpf/bpftool/Makefile | 38 +++++++++- tools/bpf/bpftool/jit_disasm.c | 158 ++++++++++++++++++++++++++++------------ tools/bpf/bpftool/llvm_disasm.c | 84 +++++++++++++++++++++ tools/bpf/bpftool/llvm_disasm.h | 41 +++++++++++ tools/bpf/bpftool/main.c | 3 +- tools/bpf/bpftool/main.h | 3 + 6 files changed, 276 insertions(+), 51 deletions(-)
---
base-commit: d80373a8e70d3193151232c3062abbbe367ff5ad
change-id: 20260603-bpftool-plugin-c994bc3e0643
Best regards,
Signature · 1 line
Mark Korenberg <[email protected]>
PATCH v3 1/2 bpftool: Fix disposal of the LLVM disassembler context
destroy_context() frees the disassembler context with LLVMDisposeMessage(), which is a plain free() meant for strings returned by LLVM. The context is therefore never destroyed, leaking about 28 KiB per disassembled function. Use LLVMDisasmDispose() instead.
--- tools/bpf/bpftool/jit_disasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/jit_disasm.c +1 -1
@@ -115,7 +115,7 @@ init_context(disasm_ctx_t *ctx, const char *arch,static void destroy_context (disasm_ctx_t *ctx) { -LLVMDisposeMessage (*ctx); +LLVMDisasmDispose (*ctx); } static int
Signature · 1 line
2.56.0
PATCH v3 2/2 bpftool: Make the LLVM disassembler an optional runtime dependency
In reply to Mark KorenberglibLLVM is only needed for "bpftool prog dump jited", yet it is a hard dependency of bpftool: on Fedora 43, installing the ~730 KiB bpftool pulls in ~140 MiB of llvm20-libs.
Move the LLVM disassembler to a plugin, $(libdir)/bpftool/bpftool-llvm.so, that bpftool loads with dlopen() only when disassembling JITed programs. Distributions can then ship it as an optional package.
The plugin exports a single structure of callbacks, with an ABI version checked by bpftool. bpftool looks for the plugin next to its own binary, then in its install directory, but not in the current directory since it usually runs as root. "bpftool version" reports "llvm" only if the plugin can be loaded.
If libLLVM is static, as required for a static bpftool, the disassembler is linked into bpftool as before.
--- tools/bpf/bpftool/Makefile | 38 +++++++++- tools/bpf/bpftool/jit_disasm.c | 158 ++++++++++++++++++++++++++++------------ tools/bpf/bpftool/llvm_disasm.c | 84 +++++++++++++++++++++ tools/bpf/bpftool/llvm_disasm.h | 41 +++++++++++ tools/bpf/bpftool/main.c | 3 +- tools/bpf/bpftool/main.h | 3 + 6 files changed, 276 insertions(+), 51 deletions(-)
tools/bpf/bpftool/Makefile +35 -3
@@ -63,6 +63,7 @@ $(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT) $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null prefix ?= /usr/local +libdir ?= $(prefix)/lib bash_compdir ?= /usr/share/bash-completion/completions CFLAGS += -O2
@@ -170,7 +171,7 @@ include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool -SRCS := $(wildcard *.c) +SRCS := $(filter-out llvm_disasm.c,$(wildcard *.c)) ifeq ($(feature-llvm),1) ifneq ($(SKIP_LLVM),1)
@@ -186,10 +187,19 @@ ifeq ($(HAS_LLVM),1) # Makefile includes lib.mk which sets -D_GNU_SOURCE) which would cause # compilation error due to redefinition. Let's filter it out here. CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags)) - LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) + LLVM_LIBS := $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static) + # No libLLVM.so dependency to avoid, link the disassembler into bpftool + SRCS += llvm_disasm.c + LIBS += $(LLVM_LIBS) LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS)) LIBS += -lstdc++ + else + # Build the disassembler as a plugin, the only object linked against + # libLLVM, that bpftool loads only to disassemble JITed programs + LLVM_PLUGIN := $(OUTPUT)bpftool-llvm.so + CFLAGS += -DLLVM_PLUGIN_DIR='"$(libdir)/bpftool"' + LIBS += -ldl endif LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) else
@@ -290,6 +300,21 @@ $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP) $(OUTPUT)bpftool: $(OBJS) $(LIBBPF) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ +ifneq ($(LLVM_PLUGIN),) +all: $(LLVM_PLUGIN) + +$(LLVM_PLUGIN): CFLAGS += -fPIC +$(LLVM_PLUGIN): $(OUTPUT)llvm_disasm.o + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -shared $< $(LLVM_LIBS) -o $@ + +# The plugin directory, $(libdir)/bpftool, is compiled into jit_disasm.o. +# Recompile it if libdir changes, e.g. on "make" then "make install libdir=...". +$(OUTPUT)llvm-plugin-dir: FORCE + $(Q)echo '$(libdir)/bpftool' | cmp -s - $@ || echo '$(libdir)/bpftool' > $@ + +$(OUTPUT)jit_disasm.o: $(OUTPUT)llvm-plugin-dir +endif + $(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT) $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
@@ -303,16 +328,21 @@ feature-detect-clean: clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean $(call QUIET_CLEAN, bpftool) $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d + $(Q)$(RM) -- $(OUTPUT)bpftool-llvm.so $(OUTPUT)llvm-plugin-dir $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(call QUIET_CLEAN, core-gen) $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool $(Q)$(RM) -r -- $(OUTPUT)feature/ -install-bin: $(OUTPUT)bpftool +install-bin: $(OUTPUT)bpftool $(LLVM_PLUGIN) $(call QUIET_INSTALL, bpftool) $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool +ifneq ($(LLVM_PLUGIN),) + $(call QUIET_INSTALL, bpftool-llvm.so) + $(Q)$(INSTALL) -m 0755 -D -t $(DESTDIR)$(libdir)/bpftool $(LLVM_PLUGIN) +endif install: install-bin $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
@@ -321,6 +351,8 @@ install: install-bin uninstall: $(call QUIET_UNINST, bpftool) $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool + $(call QUIET_UNINST, bpftool-llvm.so) + $(Q)$(RM) -r -- $(DESTDIR)$(libdir)/bpftool $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool doc:
tools/bpf/bpftool/jit_disasm.c +111 -47
202 lines not on this page · raw message
tools/bpf/bpftool/llvm_disasm.c new file +84 -0
84 lines not on this page · raw message
tools/bpf/bpftool/llvm_disasm.h new file +41 -0
41 lines not on this page · raw message
tools/bpf/bpftool/main.c +2 -1
@@ -124,7 +124,8 @@ static int do_version(int argc, char **argv)const bool has_libbfd = false;#endif #ifdef HAVE_LLVM_SUPPORT -const bool has_llvm = true; +/* The LLVM disassembler may be a plugin that is not installed */ +const bool has_llvm =disasm_llvm_available ();#else const bool has_llvm = false;#endif
tools/bpf/bpftool/main.h +3 -0
@@ -214,6 +214,9 @@ static inline int disasm_init(void) return -1; }#endif +#ifdef HAVE_LLVM_SUPPORT +bool disasm_llvm_available (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.56.0
[email protected]
AI reviewTL;DR: Nits only: the new generated llvm-plugin-dir stamp file may need a .gitignore entry, and the bpftool man page may need updating, since "llvm" in version output now depends on the plugin…
Quoted 1 line
bpftool: Make the LLVM disassembler an optional runtime dependency
TL;DR: Nits only: the new generated llvm-plugin-dir stamp file may need a .gitignore entry, and the bpftool man page may need updating, since "llvm" in version output now depends on the plugin loading at runtime.
Quoted 4 lines
tools/bpf/bpftool/Makefile +0
-0
[ ... ]
Quoted 5 lines
tools/bpf/bpftool/Makefile +1
-0@@ -290,6 +300,21 @@ $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP) $(OUTPUT)bpftool: $(OBJS) $(LIBBPF) $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ +ifneq ($(LLVM_PLUGIN),)
tools/bpf/bpftool/Makefile $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP) · line 313
Quoted 10 lines
tools/bpf/bpftool/Makefile +10
-0+all: $(LLVM_PLUGIN) + +$(LLVM_PLUGIN): CFLAGS += -fPIC +$(LLVM_PLUGIN): $(OUTPUT)llvm_disasm.o + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -shared $< $(LLVM_LIBS) -o $@ + +# The plugin directory, $(libdir)/bpftool, is compiled into jit_disasm.o. +# Recompile it if libdir changes, e.g. on "make" then "make install libdir=...". +$(OUTPUT)llvm-plugin-dir: FORCE + $(Q)echo '$(libdir)/bpftool' | cmp -s - $@ || echo '$(libdir)/bpftool' > $@
^^^^
The new stamp file $(OUTPUT)llvm-plugin-dir is a generated build artifact, but it does not look like any ignore pattern matches it.
bpftool-llvm.so is covered by '*.so' and llvm_disasm.o by '*.o', but 'git check-ignore' finds no pattern for tools/bpf/bpftool/llvm-plugin-dir.
On an in-tree build with a shared libLLVM (make -C tools/bpf/bpftool with no OUTPUT), the stamp is written to tools/bpf/bpftool/llvm-plugin-dir and shows up as an untracked file in 'git status'.
tools/bpf/bpftool/.gitignore already lists the similar generated file FEATURE-DUMP.bpftool. Should this patch add '/llvm-plugin-dir' there as well?
Quoted 4 lines
tools/bpf/bpftool/Makefile +4
-0+ +$(OUTPUT)jit_disasm.o: $(OUTPUT)llvm-plugin-dir +endif +
[ ... ]
tools/bpf/bpftool/main.c static int do_version(int argc, char **argv) · line 128
Quoted 11 lines
tools/bpf/bpftool/main.c +2
-1@@ -124,7 +124,8 @@ static int do_version(int argc, char **argv)const bool has_libbfd = false;#endif #ifdef HAVE_LLVM_SUPPORT -const bool has_llvm = true; +/* The LLVM disassembler may be a plugin that is not installed */ +const bool has_llvm =disasm_llvm_available ();
^^^^
With a shared libLLVM, the "llvm" entry printed by "bpftool version" and "-V" now depends on whether bpftool-llvm.so can be dlopen()ed at runtime rather than on how bpftool was built. The man page text was not updated to match.
tools/bpf/bpftool/Documentation/common_options.rst, which every bpftool man page includes, still says:
Print bpftool's version number (similar to **bpftool version**), the number of the libbpf version in use, and optional features that were included when bpftool was compiled. Optional features include linking against LLVM or libbfd to provide the disassembler for JIT-ted programs (**bpftool prog dump jited**) ...
In this mode bpftool is no longer linked against LLVM at all, and do_version() -> disasm_llvm_available() -> llvm_load(false) can fail in llvm_plugin_find() or dlopen(). has_llvm then comes back false even though bpftool was compiled with HAVE_LLVM_SUPPORT.
Should common_options.rst say that "llvm" is reported only when the disassembler plugin is installed and loadable?
tools/bpf/bpftool/main.c static int do_version(int argc, char **argv) · line 131
Quoted 3 lines
tools/bpf/bpftool/main.c +0
-0#else const bool has_llvm = false;#endif
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/40881490673
Quoted 9 lines · On Thu, Oct 29, 2026 at 06:05 AM Mark Korenberg via B4 Relay…
Move the LLVM disassembler to a plugin, $(libdir)/bpftool/bpftool-llvm.so, that bpftool loads with
dlopen()only when disassembling JITed programs. Distributions can then ship it as an optional package.The plugin exports a single structure of callbacks, with an ABI version checked by bpftool. bpftool looks for the plugin next to its own binary, then in its install directory, but not in the current directory since it usually runs as root. "bpftool version" reports "llvm" only if the plugin can be loaded.
This is overkill.
The plugin wraps 7 llvm-c functions and the target init calls. bpftool can dlopen() libLLVM.so itself and dlsym() them.
perf does that for libcapstone. See
commit b5c9bcde61b8 ("perf capstone: Support for dlopen-ing libcapstone.so").
this patch dlopen(libLLVM.so) ----------------- -------------------------- ------------------ installed files bpftool, bpftool-llvm.so bpftool who finds the lib llvm_plugin_find() ld.so ABI to maintain struct bpftool_llvm_ops none Makefile libdir, llvm-plugin-dir, one -D
install, uninstall rules
v1, v2 and v3 each look for the plugin in a different way. The bpftool that the selftests build still doesn't find it. selftests/bpf, selftests/hid, selftests/sched_ext and tools/sched_ext do:
make -C tools/bpf/bpftool prefix= DESTDIR=<dir>/ install-bin
bpftool <dir>/sbin/bpftool plugin <dir>/lib/bpftool/bpftool-llvm.so bpftool looks in <dir>/sbin, then /lib/bpftool
"prog dump jited" fails there with this patch.
"make install" in selftests/bpf copies only bpftool. The plugin stays behind.
In the diff below llvm-config gives the name of the library at build time. ld.so finds it the same way as today.
The distro package recommends the LLVM libs and doesn't require them. "bpftool version" keeps "llvm" as a compile time feature. That's what common_options.rst says.
LLVM built with BUILD_SHARED_LIBS=ON is not handled.
Something like this (not even compile tested):
tools/bpf/bpftool/Makefile +6 -1
@@ -186,10 +186,15 @@ ifeq ($(HAS_LLVM),1) # Makefile includes lib.mk which sets -D_GNU_SOURCE) which would cause # compilation error due to redefinition. Let's filter it out here. CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags)) - LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static) + LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS)) LIBS += -lstdc++ + else + # Don't link with libLLVM.so. "prog dump jited" loads it with dlopen(). + LIBLLVM_SO := $(shell $(LLVM_CONFIG) --libfiles $(LLVM_CONFIG_LIB_COMPONENTS)) + CFLAGS += -DLIBLLVM_SO='"$(notdir $(realpath $(LIBLLVM_SO)))"' + LIBS += -ldl endif LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) else
tools/bpf/bpftool/jit_disasm.c +85 -0
109 lines not on this page · raw message
Keep patch 1.
Also pls don't respin in 22 minutes only because a bot said something.
pw-bot: cr