Skip to contentSkip to conversation

tools build: bpf: Append EXTRA_CFLAGS and HOST_EXTRACFLAGS

PATCH Applied Leo Yan 6 participants 18 messages Jun 2 – Jun 15

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems
lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v2 cover tools build: bpf: Append EXTRA_CFLAGS and HOST_EXTRACFLAGS

Append EXTRA_CFLAGS and HOST_EXTRACFLAGS to the BPF build.

This mitigates an issue introduced in GCC 15, where a {0} initializer does not guarantee zeroing the entire union [1].

The common changes under tools to support EXTRA_CFLAGS and HOST_EXTRACFLAGS are sent separately [2]. As suggested, BPF patches would be picked up via the bpf tree, so this series only includes BPF related changes.

Verification on bpf-ci (with tools changes [2]:

https://github.com/kernel-patches/bpf/actions/runs/26815163486

[1] https://gcc.gnu.org/gcc-15/changes.html
[2] https://lore.kernel.org/all/[email protected]/

---
Changes in v2:
- Used strscpy() instead in patch 06 (Ihor).
- Added prefix "bpf-next" in subject (Alexei).
- Added patch 01 to pass host cflags to bootstrap libbpf.
- Added patch 08 to avoid static LLVM linking for cross build.
- Link to v1: https://lore.kernel.org/r/20260323-tools_build_fix_zero_init_bpf_only-v1-0-d1cfad2f4cd1@arm.com

---
Leo Yan (8):
bpftool: Pass host flags to bootstrap libbpf
bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
bpftool: Append extra host flags
libbpf: Initialize CFLAGS before including Makefile.include tools/bpf: build: Append extra cflags selftests/bpf: Initialize operation name before use selftests/bpf: Use common CFLAGS for urandom_read selftests/bpf: Avoid static LLVM linking for cross builds

 tools/bpf/Makefile                                    |  1 +
 tools/bpf/bpftool/Makefile                            | 15 ++++++++++++---
 tools/lib/bpf/Makefile                                | 17 +++++++++--------
 tools/testing/selftests/bpf/Makefile                  | 19 +++++++++++++------
 tools/testing/selftests/bpf/prog_tests/verifier_log.c |  1 +
 5 files changed, 36 insertions(+), 17 deletions(-)

---
base-commit: b93c55b4932dd7e32dca8cf34a3443cc87a02906
change-id: 20260323-tools_build_fix_zero_init_bpf_only-52232f62fb7f

Best regards,

Signature · 1 line
Leo Yan <[email protected]>
author
#1 lore raw

PATCH v2 1/8 bpftool: Pass host flags to bootstrap libbpf

bpftool builds a bootstrap libbpf with HOSTCC, but the libbpf submake can still inherit target build flags through CFLAGS. This can break cross builds when host objects are compiled with target-only options.

Since HOST_CFLAGS contains warning options that are not suitable for building libbpf, use LIBBPF_BOOTSTRAP_CFLAGS with the warning options removed to build the bootstrap libbpf. Clear EXTRA_CFLAGS so target extra flags are not mixed into the host bootstrap libbpf build.

---
 tools/bpf/bpftool/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/bpf/bpftool/Makefile +5 -1
index 0febf60e1b64656035dfe93cd15cba090015587a..1f033e578f90a2d6b93168ab41399de758bdf659 100644
@@ -47,7 +47,8 @@ $(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_
 $(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
 	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
 		DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \
-		ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" $@ install_headers
+		ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
+		CFLAGS="$(LIBBPF_BOOTSTRAP_CFLAGS)" EXTRA_CFLAGS= $@ install_headers
 
 $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
 	$(call QUIET_INSTALL, $@)
@@ -92,6 +93,9 @@ HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
 		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)
 
+# Remove warnings for libbpf bootstrap build
+LIBBPF_BOOTSTRAP_CFLAGS := $(filter-out -W -Wall -Wextra -Wformat -Wformat-signedness,$(HOST_CFLAGS))
+
 INSTALL ?= install
 RM ?= rm -f
 
Signature · 1 line
2.34.1
maintainer Acked-by on v2 1/8
#2 lore raw

Re: [PATCH bpf-next v2 1/8] bpftool: Pass host flags to bootstrap libbpf

2026-06-02 15:47 UTC+0100 ~ Leo Yan <[email protected]>

Quoted 29 lines

bpftool builds a bootstrap libbpf with HOSTCC, but the libbpf submake can still inherit target build flags through CFLAGS. This can break cross builds when host objects are compiled with target-only options.

Since HOST_CFLAGS contains warning options that are not suitable for building libbpf, use LIBBPF_BOOTSTRAP_CFLAGS with the warning options removed to build the bootstrap libbpf. Clear EXTRA_CFLAGS so target extra flags are not mixed into the host bootstrap libbpf build.

---
 tools/bpf/bpftool/Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/bpf/bpftool/Makefile +2 -1
index 0febf60e1b64656035dfe93cd15cba090015587a..1f033e578f90a2d6b93168ab41399de758bdf659 100644
@@ -47,7 +47,8 @@ $(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_
 $(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
 	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
 		DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \
-		ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" $@ install_headers
+		ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
+		CFLAGS="$(LIBBPF_BOOTSTRAP_CFLAGS)" EXTRA_CFLAGS= $@ install_headers
 
 $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
 	$(call QUIET_INSTALL, $@)

tools/bpf/bpftool/Makefile HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\ · line 96

Quoted 4 lines
tools/bpf/bpftool/Makefile +1 -0
@@ -92,6 +93,9 @@ HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
 		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)
 
+# Remove warnings for libbpf bootstrap build

Nitpick: We could expand this comment to briefly explain why. Not sure it's worth a respin, though.

tools/bpf/bpftool/Makefile HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\ · line 97

Quoted 1 line
tools/bpf/bpftool/Makefile +1 -0
+LIBBPF_BOOTSTRAP_CFLAGS := $(filter-out -W -Wall -Wextra -Wformat -Wformat-signedness,$(HOST_CFLAGS))

Do we need to remove all warnings, or would it be sufficient to take out -Wformat and -Wformat-signedness? I don't mind removing them all if necessary, just asking.

Thanks!

author
#3 lore raw

PATCH v2 2/8 bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS

In reply to Leo Yan

Prepare for future changes where EXTRA_CFLAGS may include flags not applicable to the host compiler.

Move the HOST_CFLAGS assignment before appending EXTRA_CFLAGS to CFLAGS so that HOST_CFLAGS does not inherit flags from EXTRA_CFLAGS.

---
 tools/bpf/bpftool/Makefile | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Makefile +6 -2
index 1f033e578f90a2d6b93168ab41399de758bdf659..616c14247e4bb690e16ae4ff8c1992e6d4fc863e 100644
@@ -82,6 +82,12 @@ CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
 ifneq ($(BPFTOOL_VERSION),)
 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
 endif
+
+# This must be done before appending EXTRA_CFLAGS to CFLAGS to avoid
+# including flags that are not applicable to the host compiler.
+HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
 endif
@@ -89,8 +95,6 @@ ifneq ($(EXTRA_LDFLAGS),)
 LDFLAGS += $(EXTRA_LDFLAGS)
 endif
 
-HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
-		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
 HOST_LDFLAGS := $(LDFLAGS)
 
 # Remove warnings for libbpf bootstrap build
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 3/8 bpftool: Append extra host flags

In reply to Leo Yan

Append HOST_EXTRACFLAGS to HOST_CFLAGS so that additional flags can be applied to the host compiler.

---
 tools/bpf/bpftool/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/Makefile +1 -0
index 616c14247e4bb690e16ae4ff8c1992e6d4fc863e..96be5647f0e1370247025d65e813db376b146e52 100644
@@ -87,6 +87,7 @@ endif
 # including flags that are not applicable to the host compiler.
 HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
 		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+HOST_CFLAGS += $(HOST_EXTRACFLAGS)
 
 ifneq ($(EXTRA_CFLAGS),)
 CFLAGS += $(EXTRA_CFLAGS)
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 4/8 libbpf: Initialize CFLAGS before including Makefile.include

In reply to Leo Yan

tools/scripts/Makefile.include may expand EXTRA_CFLAGS in a future change. This could alter the initialization of CFLAGS, as the default options "-g -O2" would never be set once EXTRA_CFLAGS is expanded.

Prepare for this by moving the CFLAGS initialization before including tools/scripts/Makefile.include, so it is not affected by the extended EXTRA_CFLAGS.

Append EXTRA_CFLAGS to CFLAGS only after including Makefile.include and place it last so that the extra flags propagate properly and can override the default options.

tools/scripts/Makefile.include already appends $(CLANG_CROSS_FLAGS) to CFLAGS, the Makefile appends $(CLANG_CROSS_FLAGS) again, remove the redundant append.

---
 tools/lib/bpf/Makefile | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tools/lib/bpf/Makefile +9 -8
index 168140f8e6461bd06db40e23d21a3fb8847ccbf4..eca584fb061e16013e76827e4203f6be0477a73e 100644
@@ -49,6 +49,14 @@ man_dir_SQ = '$(subst ','\'',$(man_dir))'
 export man_dir man_dir_SQ INSTALL
 export DESTDIR DESTDIR_SQ
 
+# Defer assigning EXTRA_CFLAGS to CFLAGS until after including
+# tools/scripts/Makefile.include, as it may add flags to EXTRA_CFLAGS.
+ifdef EXTRA_CFLAGS
+  CFLAGS :=
+else
+  CFLAGS := -g -O2
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 
 # copy a bit from Linux kbuild
@@ -70,13 +78,6 @@ LIB_TARGET	= libbpf.a libbpf.so.$(LIBBPF_VERSION)
 LIB_FILE	= libbpf.a libbpf.so*
 PC_FILE		= libbpf.pc
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -O2
-endif
-
 # Append required CFLAGS
 override CFLAGS += -std=gnu89
 override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
@@ -84,7 +85,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-override CFLAGS += $(CLANG_CROSS_FLAGS)
+override CFLAGS += $(EXTRA_CFLAGS)
 
 # flags specific for shared library
 SHLIB_FLAGS := -DSHARED -fPIC
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH bpf-next v2 4/8] libbpf: Initialize CFLAGS before including Makefile.include

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Removal of `override CFLAGS += $(CLANG_CROSS_FLAGS)` breaks Clang cross-compilation when…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Removal of override CFLAGS += $(CLANG_CROSS_FLAGS) breaks Clang cross-compilation when CFLAGS is overridden on the command line. --

commit 6aa5adddfad905f5f29ccfe28312c5a00d2d1082
Author: Leo Yan <[email protected]>

libbpf: Initialize CFLAGS before including Makefile.include

This commit reorders CFLAGS initialization to happen before including Makefile.include so it is not affected by expanded EXTRA_CFLAGS. It also removes the appending of CLANG_CROSS_FLAGS, citing redundancy with the include file.

Quoted 4 lines
tools/lib/bpf/Makefile +0 -0
index 168140f8e6461..eca584fb061e1 100644

[ ... ]

tools/lib/bpf/Makefile override CFLAGS += -Werror -Wall · line 88

Quoted 6 lines
tools/lib/bpf/Makefile +1 -1
@@ -84,7 +85,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-override CFLAGS += $(CLANG_CROSS_FLAGS)
+override CFLAGS += $(EXTRA_CFLAGS)

[Severity: Medium]
Does removing this override directive break Clang cross-compilation when CFLAGS is overridden on the command line?

While tools/scripts/Makefile.include already appends $(CLANG_CROSS_FLAGS), it uses a standard assignment. According to GNU Make semantics, standard assignments are ignored if a variable is overridden via the command line (e.g., make CFLAGS="-O3").

By removing the override directive here, the build system might fail to unconditionally append cross-compilation flags (like --target= and --sysroot=), causing Clang to fall back to compiling for the host architecture instead of the intended target.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260602-tools_build_fix_zero_init_bpf_only-v2-0-c76e5250ea1c@arm.com?part=4
author
#7 lore raw

PATCH v2 5/8 tools/bpf: build: Append extra cflags

In reply to Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/Makefile +1 -0
index fd2585af1252669c59228af3a77320e449e1a1ab..9c19e81f3c274767c3b9b94c907cbbe3ce9fe78c 100644
@@ -11,6 +11,7 @@ INSTALL ?= install
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi \
 	  -I$(srctree)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 # This will work when bpf is built in tools env. where srctree
 # isn't set and when invoked from selftests build, where srctree
Signature · 1 line
2.34.1
reviewer Acked-by on v2 5/8
#8 lore raw

Re: [PATCH bpf-next v2 5/8] tools/bpf: build: Append extra cflags

Quoted 13 lines · Leo Yan

Append EXTRA_CFLAGS to CFLAGS so that additional flags can be applied to the compiler.

---
 tools/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/Makefile +0 -0
index fd2585af1252669c59228af3a77320e449e1a1ab..9c19e81f3c274767c3b9b94c907cbbe3ce9fe78c 100644

tools/bpf/Makefile INSTALL ?= install · line 14

Quoted 4 lines
tools/bpf/Makefile +1 -0
@@ -11,6 +11,7 @@ INSTALL ?= install
 CFLAGS += -Wall -O2
 CFLAGS += -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi \
 	  -I$(srctree)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)
Quoted 4 lines
tools/bpf/Makefile +0 -0
 # This will work when bpf is built in tools env. where srctree
 # isn't set and when invoked from selftests build, where srctree
 
author
#9 lore raw

PATCH v2 6/8 selftests/bpf: Initialize operation name before use

In reply to Leo Yan

ASAN reports stack-buffer-overflow due to the uninitialized op_name.

Initialize it to fix the issue.

Fixes 054b6c7866c7 ("selftests/bpf: Add verifier log tests for BPF_BTF_LOAD command")
---
 tools/testing/selftests/bpf/prog_tests/verifier_log.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/verifier_log.c +1 -0
index c01c0114af1b7476d27885ce380d90400a4bdc89..4542bb586d723b8fda2faf8261066f16102c60c0 100644
@@ -317,6 +317,7 @@ static void verif_btf_log_subtest(bool bad_btf)
 	res = load_btf(&opts, true);
 	ASSERT_EQ(res, -ENOSPC, "half_log_fd");
 	ASSERT_EQ(strlen(logs.buf), 24, "log_fixed_25");
+	strscpy(op_name, "log_fixed", sizeof(op_name));
 	ASSERT_STRNEQ(logs.buf, logs.reference, 24, op_name);
 
 	/* validate rolling verifier log logic: try all variations of log buf
Signature · 1 line
2.34.1
reviewer Acked-by on v2 6/8
#10 lore raw

Re: [PATCH bpf-next v2 6/8] selftests/bpf: Initialize operation name before use

Quoted 15 lines · Leo Yan

ASAN reports stack-buffer-overflow due to the uninitialized op_name.

Initialize it to fix the issue.

Fixes 054b6c7866c7 ("selftests/bpf: Add verifier log tests for BPF_BTF_LOAD command")
---
 tools/testing/selftests/bpf/prog_tests/verifier_log.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/verifier_log.c +0 -0
index c01c0114af1b7476d27885ce380d90400a4bdc89..4542bb586d723b8fda2faf8261066f16102c60c0 100644

tools/testing/selftests/bpf/prog_tests/verifier_log.c static void verif_btf_log_subtest(bool bad_btf) · line 320

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/verifier_log.c +1 -0
@@ -317,6 +317,7 @@ static void verif_btf_log_subtest(bool bad_btf)
 	res = load_btf(&opts, true);
 	ASSERT_EQ(res, -ENOSPC, "half_log_fd");
 	ASSERT_EQ(strlen(logs.buf), 24, "log_fixed_25");
+	strscpy(op_name, "log_fixed", sizeof(op_name));
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/verifier_log.c +0 -0
 	ASSERT_STRNEQ(logs.buf, logs.reference, 24, op_name);
 
 	/* validate rolling verifier log logic: try all variations of log buf
author
#11 lore raw

PATCH v2 7/8 selftests/bpf: Use common CFLAGS for urandom_read

In reply to Leo Yan

The urandom_read helper and its shared library are built with $(CLANG) directly rather than through the normal selftest $(CC) rules.

The CFLAGS variable can contain specific flags only for $(CC) but might be imcompatible for $(CLANG) and those flags are not necessarily valid for the clang-only urandom_read build.

Split the BPF selftest local flags into COMMON_CFLAGS and append them to CFLAGS for the normal build path. Use COMMON_CFLAGS directly for urandom_read and liburandom_read.so, while still filtering out -static as before.

---
 tools/testing/selftests/bpf/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/Makefile +4 -4
index bc049620c7745ce35976f5e0922eb680e38def13..1e47e29ba4214925c62afc0595a7edb859ace944 100644
@@ -51,7 +51,7 @@ srctree := $(patsubst %/,%,$(dir $(srctree)))
 srctree := $(patsubst %/,%,$(dir $(srctree)))
 endif
 
-CFLAGS += -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
+COMMON_CFLAGS = -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
 	  -Wall -Werror -fno-omit-frame-pointer				\
 	  -Wno-unused-but-set-variable					\
 	  $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)			\
@@ -64,7 +64,7 @@ LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread
 PCAP_CFLAGS	:= $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1")
 PCAP_LIBS	:= $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null)
 LDLIBS += $(PCAP_LIBS)
-CFLAGS += $(PCAP_CFLAGS)
+CFLAGS += $(COMMON_CFLAGS) $(PCAP_CFLAGS)
 
 # Some utility functions use LLVM libraries
 jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS)
@@ -261,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
 	$(call msg,LIB,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \
 		     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
 		     -Wno-unused-command-line-argument \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
@@ -271,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
 	$(call msg,BINARY,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
 		     -Wno-unused-command-line-argument \
 		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH bpf-next v2 7/8] selftests/bpf: Use common CFLAGS for urandom_read

Ian Rogers raised a concern in v4 that COMMON_CFLAGS is being assigned before 'include ../lib.mk' (which happens later around line 165).
Quoted 14 lines
tools/testing/selftests/bpf/Makefile +1 -1
index bc049620c774..1e47e29ba421 100644
@@ -51,7 +51,7 @@ srctree := $(patsubst %/,%,$(dir $(srctree)))
 srctree := $(patsubst %/,%,$(dir $(srctree)))
 endif
 
-CFLAGS += -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
+COMMON_CFLAGS = -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
 	  -Wall -Werror -fno-omit-frame-pointer				\
 	  -Wno-unused-but-set-variable					\
 	  $(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS)			\

tools/testing/selftests/bpf/Makefile LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread · line 67

Quoted 5 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -64,7 +64,7 @@ LDLIBS += $(LIBELF_LIBS) -lz -lrt -lpthread
 PCAP_CFLAGS	:= $(shell $(PKG_CONFIG) --cflags libpcap 2>/dev/null && echo "-DTRAFFIC_MONITOR=1")
 PCAP_LIBS	:= $(shell $(PKG_CONFIG) --libs libpcap 2>/dev/null)
 LDLIBS += $(PCAP_LIBS)
-CFLAGS += $(PCAP_CFLAGS)
+CFLAGS += $(COMMON_CFLAGS) $(PCAP_CFLAGS)

Ian Rogers raised a concern in v4 that COMMON_CFLAGS is being assigned before 'include ../lib.mk' (which happens later around line 165).

The ../lib.mk include appends important flags to CFLAGS, such as -D_GNU_SOURCE=. Since COMMON_CFLAGS uses recursive assignment (=), these flags added by lib.mk should be available when COMMON_CFLAGS expands. However, COMMON_CFLAGS references $(GENFLAGS), $(SAN_CFLAGS), and $(LIBELF_CFLAGS) which are defined before lib.mk, but lib.mk may also modify CFLAGS directly with additional flags.

Will urandom_read miss any flags that lib.mk adds to CFLAGS but that aren't captured in the variables referenced by COMMON_CFLAGS? Ian suggested moving these assignments below the include to ensure all lib.mk-added flags are captured.

Reference:
https://lore.kernel.org/all/CAP-5=fXjd15e5YpCM9KMjEhpjLxvxc6qUk0g7BprCfkzcRZ5_g@mail.gmail.com/

Quoted 3 lines
tools/testing/selftests/bpf/Makefile +0 -0
 # Some utility functions use LLVM libraries
 jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS)

[ ... ]

Quoted 9 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -261,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
 	$(call msg,LIB,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \
 		     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
 		     -Wno-unused-command-line-argument \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \

[ ... ]

tools/testing/selftests/bpf/Makefile $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom · line 277

Quoted 9 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -271,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
 	$(call msg,BINARY,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
 		     -Wno-unused-command-line-argument \
 		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
 		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \

---
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/26828477669

#13 lore raw

Re: [PATCH bpf-next v2 7/8] selftests/bpf: Use common CFLAGS for urandom_read

In reply to Leo Yan
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Replacing CFLAGS with COMMON_CFLAGS for urandom_read breaks cross-compilation with Clang. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Replacing CFLAGS with COMMON_CFLAGS for urandom_read breaks cross-compilation with Clang. --

commit 537d69efefb1121c195758d9362eba3f31c54f36
Author: Leo Yan <[email protected]>

selftests/bpf: Use common CFLAGS for urandom_read

This commit introduces a COMMON_CFLAGS variable to separate general flags from CC-specific ones that might be incompatible with CLANG when building urandom_read and its shared library.

tools/testing/selftests/bpf/Makefile srctree := $(patsubst %/,%,$(dir $(srctree))) · line 54

Quoted 10 lines
tools/testing/selftests/bpf/Makefile +1 -1
index bc049620c7745..1e47e29ba4214 100644
@@ -51,7 +51,7 @@ srctree := $(patsubst %/,%,$(dir $(srctree)))
 srctree := $(patsubst %/,%,$(dir $(srctree)))
 endif
 
-CFLAGS += -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
+COMMON_CFLAGS = -g $(OPT_FLAGS) -rdynamic -std=gnu11				\

[Severity: Low]
Does this change break cross-compilation with Clang?

By defining COMMON_CFLAGS from scratch without including the pre-existing CFLAGS, this drops any earlier contents from the urandom_read build. This strips CLANG_CROSS_FLAGS (which is appended to CFLAGS earlier by tools/scripts/Makefile.include), depriving Clang of essential cross-compilation arguments like --sysroot and --gcc-toolchain.

Does this also inadvertently ignore any user-provided CFLAGS?

[ ... ]

tools/testing/selftests/bpf/Makefile endif · line 264

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -261,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
 	$(call msg,LIB,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \

[Severity: Low]
When replacing CFLAGS with COMMON_CFLAGS here for the shared library build, does this remove the required sysroot and toolchain cross-compilation flags?

[ ... ]

tools/testing/selftests/bpf/Makefile $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom · line 274

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -271,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
 	$(call msg,BINARY,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \

[Severity: Low]
Similarly, does the urandom_read build rule also lose these required cross-compilation flags when using COMMON_CFLAGS?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260602-tools_build_fix_zero_init_bpf_only-v2-0-c76e5250ea1c@arm.com?part=7
reviewer
#14 lore raw

Re: [PATCH bpf-next v2 7/8] selftests/bpf: Use common CFLAGS for urandom_read

Quoted 21 lines · On 6/2/26 10:25 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Replacing CFLAGS with COMMON_CFLAGS for urandom_read breaks cross-compilation with Clang. --

commit 537d69efefb1121c195758d9362eba3f31c54f36
Author: Leo Yan <[email protected]>

selftests/bpf: Use common CFLAGS for urandom_read

This commit introduces a COMMON_CFLAGS variable to separate general flags from CC-specific ones that might be incompatible with CLANG when building urandom_read and its shared library.

Quoted 8 lines
tools/testing/selftests/bpf/Makefile +0 -0
index bc049620c7745..1e47e29ba4214 100644
@@ -51,7 +51,7 @@ srctree := $(patsubst %/,%,$(dir $(srctree)))
 srctree := $(patsubst %/,%,$(dir $(srctree)))
 endif
 
Quoted 5 lines

tools/testing/selftests/bpf/Makefile srctree := $(patsubst %/,%,$(dir $(srctree))) · line 54

Quoted 2 lines
tools/testing/selftests/bpf/Makefile +1 -1
-CFLAGS += -g $(OPT_FLAGS) -rdynamic -std=gnu11				\
+COMMON_CFLAGS = -g $(OPT_FLAGS) -rdynamic -std=gnu11				\

[Severity: Low]
Does this change break cross-compilation with Clang?

Please check this, looks plausible.

Quoted 35 lines

By defining COMMON_CFLAGS from scratch without including the pre-existing CFLAGS, this drops any earlier contents from the urandom_read build. This strips CLANG_CROSS_FLAGS (which is appended to CFLAGS earlier by tools/scripts/Makefile.include), depriving Clang of essential cross-compilation arguments like --sysroot and --gcc-toolchain.

Does this also inadvertently ignore any user-provided CFLAGS?

[ ... ]

tools/testing/selftests/bpf/Makefile endif · line 264

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -261,7 +261,7 @@ endif
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom_read.map
 	$(call msg,LIB,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \

[Severity: Low]
When replacing CFLAGS with COMMON_CFLAGS here for the shared library build, does this remove the required sysroot and toolchain cross-compilation flags?

[ ... ]

tools/testing/selftests/bpf/Makefile $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom · line 274

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1 -1
@@ -271,7 +271,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
 	$(call msg,BINARY,,$@)
 	$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
-		     $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
+		     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \

[Severity: Low]
Similarly, does the urandom_read build rule also lose these required cross-compilation flags when using COMMON_CFLAGS?

author
#15 lore raw

PATCH v2 8/8 selftests/bpf: Avoid static LLVM linking for cross builds

In reply to Leo Yan

The BPF selftests prefer static LLVM linking, which works for native builds but can break cross builds. Its --link-static output may include host-only libraries that are unavailable for the cross compilation, causing link failures.

Avoid static LLVM linking for cross builds and use shared LLVM libraries instead. Native builds keep the existing behavior.

---
 tools/testing/selftests/bpf/Makefile | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +9 -2
index 1e47e29ba4214925c62afc0595a7edb859ace944..ffa90674e86f639d3a9cadb833e57f3693d77bdc 100644
@@ -188,8 +188,15 @@ ifeq ($(feature-llvm),1)
   LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
   # both llvm-config and lib.mk add -D_GNU_SOURCE, which ends up as conflict
   LLVM_CFLAGS  += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
-  # Prefer linking statically if it's available, otherwise fallback to shared
-  ifeq ($(shell $(LLVM_CONFIG) --link-static --libs >/dev/null 2>&1 && echo static),static)
+  # Cross compilation must use dynamic linking to avoid unresolved library
+  # dependencies. For native build, prefer linking statically if it's
+  # available, otherwise fallback to shared.
+  ifneq ($(ARCH), $(HOSTARCH))
+    LLVM_LINK_STATIC :=
+  else
+    LLVM_LINK_STATIC := $(shell $(LLVM_CONFIG) --link-static --libs >/dev/null 2>&1 && echo y)
+  endif
+  ifeq ($(LLVM_LINK_STATIC),y)
     LLVM_LDLIBS  += $(shell $(LLVM_CONFIG) --link-static --libs $(LLVM_CONFIG_LIB_COMPONENTS))
     LLVM_LDLIBS  += $(filter-out -lxml2,$(shell $(LLVM_CONFIG) --link-static --system-libs $(LLVM_CONFIG_LIB_COMPONENTS)))
     LLVM_LDLIBS  += -lstdc++
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH bpf-next v2 8/8] selftests/bpf: Avoid static LLVM linking for cross builds

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Flawed native vs cross-build detection using un-normalized `ARCH` against normalized `HOSTARCH`.…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Flawed native vs cross-build detection using un-normalized ARCH against normalized HOSTARCH. --

commit 9a32865094dbf702cf09a9de14d120a1f3ca8a92
Author: Leo Yan <[email protected]>

selftests/bpf: Avoid static LLVM linking for cross builds

This commit modifies the BPF selftests Makefile to avoid static LLVM linking during cross builds. It intends to use dynamic linking for cross compilation while preserving the existing preference for static linking on native builds.

Quoted 5 lines
tools/testing/selftests/bpf/Makefile +0 -0
index 1e47e29ba4214..ffa90674e86f6 100644

tools/testing/selftests/bpf/Makefile ifeq ($(feature-llvm),1) · line 194

Quoted 9 lines
tools/testing/selftests/bpf/Makefile +4 -2
@@ -188,8 +188,15 @@ ifeq ($(feature-llvm),1)
   LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
   # both llvm-config and lib.mk add -D_GNU_SOURCE, which ends up as conflict
   LLVM_CFLAGS  += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
-  # Prefer linking statically if it's available, otherwise fallback to shared
-  ifeq ($(shell $(LLVM_CONFIG) --link-static --libs >/dev/null 2>&1 && echo static),static)
+  # Cross compilation must use dynamic linking to avoid unresolved library
+  # dependencies. For native build, prefer linking statically if it's
+  # available, otherwise fallback to shared.
+  ifneq ($(ARCH), $(HOSTARCH))

[Severity: Medium]
Will this comparison accurately identify native builds?

The kernel build system normalizes $(HOSTARCH) (for example, "x86_64" becomes "x86"), but $(ARCH) might retain the un-normalized string if passed from the environment or top-level Makefile.

If $(ARCH) is "x86_64" and $(HOSTARCH) is "x86", this check evaluates to true even on a native build, unintentionally forcing shared LLVM libraries instead of keeping the static linking behavior as intended.

Would it be more robust to compare $(SRCARCH) to $(HOSTARCH), or to check if $(CROSS_COMPILE) is empty instead?

Quoted 6 lines
tools/testing/selftests/bpf/Makefile +5 -0
+    LLVM_LINK_STATIC :=
+  else
+    LLVM_LINK_STATIC := $(shell $(LLVM_CONFIG) --link-static --libs >/dev/null 2>&1 && echo y)
+  endif
+  ifeq ($(LLVM_LINK_STATIC),y)
     LLVM_LDLIBS  += $(shell $(LLVM_CONFIG) --link-static --libs $(LLVM_CONFIG_LIB_COMPONENTS))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260602-tools_build_fix_zero_init_bpf_only-v2-0-c76e5250ea1c@arm.com?part=8
#17 lore raw
In reply to Leo Yan
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Tue, 02 Jun 2026 15:47:09 +0100 you wrote:

Append EXTRA_CFLAGS and HOST_EXTRACFLAGS to the BPF build.

This mitigates an issue introduced in GCC 15, where a {0} initializer does not guarantee zeroing the entire union [1].

The common changes under tools to support EXTRA_CFLAGS and HOST_EXTRACFLAGS are sent separately [2]. As suggested, BPF patches would be picked up via the bpf tree, so this series only includes BPF related changes.

[...]

Here is the summary with links:
- [bpf-next,v2,1/8] bpftool: Pass host flags to bootstrap libbpf

    https://git.kernel.org/bpf/bpf-next/c/9080b97689db
  - [bpf-next,v2,2/8] bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
    https://git.kernel.org/bpf/bpf-next/c/956841cbc3d7
  - [bpf-next,v2,3/8] bpftool: Append extra host flags
    https://git.kernel.org/bpf/bpf-next/c/3f2fec5b02b6
  - [bpf-next,v2,4/8] libbpf: Initialize CFLAGS before including Makefile.include
    https://git.kernel.org/bpf/bpf-next/c/f3846b3800a2
  - [bpf-next,v2,5/8] tools/bpf: build: Append extra cflags
    https://git.kernel.org/bpf/bpf-next/c/b40ba139371c
  - [bpf-next,v2,6/8] selftests/bpf: Initialize operation name before use
    https://git.kernel.org/bpf/bpf-next/c/55ffbe8a15b1
  - [bpf-next,v2,7/8] selftests/bpf: Use common CFLAGS for urandom_read
    https://git.kernel.org/bpf/bpf-next/c/584f3b7a3525
  - [bpf-next,v2,8/8] selftests/bpf: Avoid static LLVM linking for cross builds
    https://git.kernel.org/bpf/bpf-next/c/62617d28d9ae

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html