Skip to contentSkip to conversation

tools build: Append -fzero-init-padding-bits=all option

PATCH Superseded Leo Yan 5 participants 34 messages Mar 18 – Aug 13

Subsystems (21)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v5 cover tools build: Append -fzero-init-padding-bits=all option

Thank you for reviewing and commenting on v4.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the BPF related patches have been split out and will be sent separately (note that I have kept the bpftool patches in this series, as I have gathered Quentin's Acked-by tags).

The changes are organized into three parts:

Patches 01 – 05: Preparation before adding the new compiler option.
Fix typos, adjust Makefiles to ensure the newly introduced option does not cause regressions.
Patch 06: Propagate -fzero-init-padding-bits=all to
EXTRA_CFLAGS and HOST_EXTRACFLAGS for the CC and HOSTCC compilers, respectively.
Patches 07 – 26: Apply EXTRA_CFLAGS and HOST_EXTRACFLAGS in
project Makefiles.

The change has been verified:

Test 1: Cross compiling perf

	host: gcc 15.2.0 (x86_64), target: aarch64-linux-gnu-gcc

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LDFLAGS="-static" \
-C tools/perf VF=1 CORESIGHT=1 NO_JEVENTS=1 DEBUG=1

Test 2: Cross compiling selftest bpf and sched_ext

	host: gcc 15.2.0 (x86_64), target: aarch64-linux-gnu-gcc

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make -C tools/testing/selftests/ TARGETS="bpf sched_ext" SKIP_TARGETS=""

Test 3: Native compiling selftest bpf and sched_ext

	host: gcc 15.2.0 (x86_64)

make -C tools/testing/selftests/ TARGETS="bpf sched_ext" SKIP_TARGETS=""

---
Changes in v5:
- Gathered Acked tags (thanks Daniel, Namhyung, Benjamin!)
- Appended HOST_EXTRACFLAGS after KBUILD_HOSTCFLAGS (Ian)
- Appended CFLAGS for libc-test (Thomas)
- Left out BPF patches and will send out separately (Alexei)
- Rebase on v7.0.0-rc4
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Placed EXTRA_CFLAGS last so it can override the default options (Ian).
- Cached evaluated values to avoid invoking cc-option when each
reference EXTRA_CFLAGS/HOST_EXTRACFLAGS (Ian).
- Dropped useless patches for feature, verification and nolibc
(Gabriele / Ian / Thomas).
- Fixed typos for thermal (Ian).
- Removed duplicate $(CLANG_CROSS_FLAGS) in libbpf (Ian).
- Collected maintainers' reviewed and ACK tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

---
Leo Yan (26):
tools: lib: thermal: Fix typo
tools/thermal: Fix typo
tools: lib: thermal: Initialize CFLAGS before including Makefile.include tools/thermal: Initialize CFLAGS before including Makefile.include
bpftool: Avoid adding EXTRA_CFLAGS to HOST_CFLAGS
tools build: Append -fzero-init-padding-bits=all to extra cflags
bpftool: Append extra host flags
perf build: Append extra host flags
tools build: Append extra host cflags
tools: bootconfig: Append extra cflags
tools: counter: Append extra cflags
tools: dma: Append extra cflags
tools: gpio: Append extra cflags
tools: hv: Append extra cflags
tools: iio: Append extra cflags
tools: mm: Append extra cflags
tools: objtool: Append extra host cflags
tools: power: acpi: Append extra cflags
tools: power: x86/intel-speed-select: Append extra cflags
tools: sched_ext: Append extra cflags
tools: spi: Append extra cflags
tools: tracing: Append extra cflags
tools: usb: Append extra cflags
selftests/hid: Append extra cflags
selftests/nolibc: Append extra cflags
selftests/sched_ext: Append extra cflags

 tools/bootconfig/Makefile                   |  1 +
 tools/bpf/bpftool/Makefile                  |  9 +++++++--
 tools/build/Makefile                        |  6 ++++--
 tools/counter/Makefile                      |  1 +
 tools/dma/Makefile                          |  1 +
 tools/gpio/Makefile                         |  1 +
 tools/hv/Makefile                           |  1 +
 tools/iio/Makefile                          |  1 +
 tools/lib/thermal/Makefile                  | 20 ++++++++++---------
 tools/mm/Makefile                           |  1 +
 tools/objtool/Makefile                      |  2 ++
 tools/perf/Makefile.config                  |  2 +-
 tools/power/acpi/Makefile.config            |  1 +
 tools/power/x86/intel-speed-select/Makefile |  1 +
 tools/sched_ext/Makefile                    |  1 +
 tools/scripts/Makefile.include              | 30 +++++++++++++++++++++++++++++
 tools/spi/Makefile                          |  1 +
 tools/testing/selftests/hid/Makefile        |  1 +
 tools/testing/selftests/nolibc/Makefile     |  7 +++----
 tools/testing/selftests/sched_ext/Makefile  |  1 +
 tools/thermal/lib/Makefile                  | 20 ++++++++++---------
 tools/tracing/latency/Makefile              |  1 +
 tools/usb/Makefile                          |  1 +
 23 files changed, 84 insertions(+), 27 deletions(-)

---
base-commit: a989fde763f4f24209e4702f50a45be572340e68
change-id: 20260224-tools_build_fix_zero_init-dc5261bd8b8b

Best regards,

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

PATCH v5 1/26 tools: lib: thermal: Fix typo

s/CFGLAS/CFLAGS

Fixes 47c4b0de080a ("tools/lib/thermal: Add a thermal library")
---
 tools/lib/thermal/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/thermal/Makefile +2 -2
index 41aa7a324ff4d76351b89d7d7cf382df3fc14052..09d8f4ba6a0ab7ab9c99ac7f64d52d0268bee340 100644
@@ -66,8 +66,8 @@ override CFLAGS += -fPIC
 override CFLAGS += $(NL3_CFLAGS)
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
-override CFGLAS += -Wl,-L.
-override CFGLAS += -Wl,-lthermal
+override CFLAGS += -Wl,-L.
+override CFLAGS += -Wl,-lthermal
 
 all:
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v5 2/26 tools/thermal: Fix typo

In reply to Leo Yan

s/CFGLAS/CFLAGS

Fixes 3b7c5e8adf9c ("tools/thermal: Add util library")
---
 tools/thermal/lib/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/thermal/lib/Makefile +2 -2
index 056d212f25cf51cd8c02260fbe2ef28dda5e4acb..8c9a37f999b826855dad11c0d9574b41d5b24244 100644
@@ -60,8 +60,8 @@ override CFLAGS += $(EXTRA_WARNINGS)
 override CFLAGS += -Werror -Wall
 override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
-override CFGLAS += -Wl,-L.
-override CFGLAS += -Wl,-lthermal
+override CFLAGS += -Wl,-L.
+override CFLAGS += -Wl,-lthermal
 
 all:
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v5 3/26 tools: lib: thermal: 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 in the thermal Makefile, as the default options "-g -Wall" 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/lib/thermal/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/lib/thermal/Makefile +9 -7
index 09d8f4ba6a0ab7ab9c99ac7f64d52d0268bee340..b55fa42cefa47628b7f2fac32fc08fcd82580a0e 100644
@@ -23,6 +23,14 @@ INSTALL = install
 DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
+# 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 -Wall
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 include $(srctree)/tools/scripts/Makefile.arch
 
@@ -39,13 +47,6 @@ libdir = $(prefix)/$(libdir_relative)
 libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -Wall
-endif
-
 NL3_CFLAGS = $(shell pkg-config --cflags libnl-3.0 2>/dev/null)
 ifeq ($(NL3_CFLAGS),)
 NL3_CFLAGS = -I/usr/include/libnl3
@@ -68,6 +69,7 @@ override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -Wl,-L.
 override CFLAGS += -Wl,-lthermal
+override CFLAGS += $(EXTRA_CFLAGS)
 
 all:
 
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v5 4/26 tools/thermal: 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 in the thermal Makefile, as the default options "-g -Wall" 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/thermal/lib/Makefile | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/thermal/lib/Makefile +9 -7
index 8c9a37f999b826855dad11c0d9574b41d5b24244..333b711bc7f0f9610e165ff7dc72c42980c59c99 100644
@@ -23,6 +23,14 @@ INSTALL = install
 DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
+# 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 -Wall
+endif
+
 include $(srctree)/tools/scripts/Makefile.include
 include $(srctree)/tools/scripts/Makefile.arch
 
@@ -39,13 +47,6 @@ libdir = $(prefix)/$(libdir_relative)
 libdir_SQ = $(subst ','\'',$(libdir))
 libdir_relative_SQ = $(subst ','\'',$(libdir_relative))
 
-# Set compile option CFLAGS
-ifdef EXTRA_CFLAGS
-  CFLAGS := $(EXTRA_CFLAGS)
-else
-  CFLAGS := -g -Wall
-endif
-
 INCLUDES = \
 -I/usr/include/libnl3 \
 -I$(srctree)/tools/lib/thermal/include \
@@ -62,6 +63,7 @@ override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -Wl,-L.
 override CFLAGS += -Wl,-lthermal
+override CFLAGS += $(EXTRA_CFLAGS)
 
 all:
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v5 5/26 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 519ea5cb8ab1c0ee31acc67fc5f96b40e21005c2..3e7d8359e1b2a81a29a47544be8539e3b191a0e8 100644
@@ -81,6 +81,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
@@ -88,8 +94,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)
 
 INSTALL ?= install
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v5 6/26 tools build: Append -fzero-init-padding-bits=all to extra cflags

In reply to Leo Yan

GCC-15 release claims [1]:

{0} initializer in C or C++ for unions no longer guarantees clearing of the whole union (except for static storage duration initialization), it just initializes the first union member to zero. If initialization of the whole union including padding bits is desirable, use {} (valid in C23 or C++) or use -fzero-init-padding-bits=unions option to restore old GCC behavior.

As a result, this new behaviour might cause unexpected data when we initialize a union with using the '{ 0 }' initializer.

Since commit dce4aab8441d ("kbuild: Use -fzero-init-padding-bits=all"), the kernel has enabled -fzero-init-padding-bits=all to zero padding bits in unions and structures. This commit applies the same option for tools building.

The option is not supported by any version older than GCC 15, nor is it supported by LLVM. This patch adds the cc-option and host-cc-option functions to dynamically detect compiler option and append it to the EXTRA_CFLAGS and HOST_EXTRACFLAGS respectively.

[1] https://gcc.gnu.org/gcc-15/changes.html

---
 tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/scripts/Makefile.include +30 -0
index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;		\
+	TMP=$(TMPOUT)/tmp;		\
+	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi)
+
+# cc-option
+# Usage: CFLAGS += $(call cc-option,-march=winchip-c6,-march=i586)
+cc-option = $(call try-run, \
+	$(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+host-cc-option = $(call try-run, \
+	$(HOSTCC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)
+HOST_FLAG_ZERO_INIT := $(call host-cc-option,-fzero-init-padding-bits=all)
+override HOST_EXTRACFLAGS += $(HOST_FLAG_ZERO_INIT)
+
 ifneq ($(findstring $(MAKEFLAGS), w),w)
 PRINT_DIR = --no-print-directory
 else
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v5 7/26 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 3e7d8359e1b2a81a29a47544be8539e3b191a0e8..762fcf0e62a189625053798dddeaf17d409fe248 100644
@@ -86,6 +86,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
#11 lore raw

PATCH v5 8/26 perf build: 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/perf/Makefile.config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/Makefile.config +1 -1
index 15fbba9f4ca8926e4512f0b021d9c9b2ec83847e..c9034aeaf6ce27752fd44432847b52074c4e0f1e 100644
@@ -17,7 +17,7 @@ detected     = $(shell echo "$(1)=y"       >> $(OUTPUT).config-detected)
 detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected)
 
 CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
-HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
+HOSTCFLAGS := $(HOST_EXTRACFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
 
 # This is required because the kernel is built with this and some of the code
 # borrowed from kernel headers depends on it, e.g. put_unaligned_*().
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH v5 9/26 tools build: Append extra host cflags

In reply to Leo Yan

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

---
 tools/build/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/build/Makefile +4 -2
index 3a5a3808ab2a1dedd40f35ea322913e8a0788130..44e50a7c182484e18ff80f4205c49a77e8c8abd9 100644
@@ -40,14 +40,16 @@ endif
 FIXDEP		:= $(OUTPUT)fixdep
 FIXDEP_IN	:= $(OUTPUT)fixdep-in.o
 
+HOSTCFLAGS	:= $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS)
+
 # To track fixdep's dependencies properly, fixdep needs to run on itself.
 # Build it twice the first time.
 $(FIXDEP_IN): FORCE
 	$(Q)if [ ! -f $(FIXDEP) ]; then						\
-		$(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)";	\
+		$(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)";		\
 		rm -f $(FIXDEP).o;						\
 	fi
-	$(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(KBUILD_HOSTCFLAGS)"
+	$(Q)$(MAKE) $(build)=fixdep HOSTCFLAGS="$(HOSTCFLAGS)"
 
 
 $(FIXDEP): $(FIXDEP_IN)
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v5 10/26 tools: bootconfig: Append extra cflags

In reply to Leo Yan

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

---
 tools/bootconfig/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/bootconfig/Makefile +1 -0
index 90eb47c9d8de67b0ccc74a8de0af3a7e0b97feeb..4490df707585acf356153b78cb87760f622a6190 100644
@@ -11,6 +11,7 @@ endif
 
 LIBSRC = $(srctree)/lib/bootconfig.c $(srctree)/include/linux/bootconfig.h
 override CFLAGS += -Wall -g -I$(CURDIR)/include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := bootconfig
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v5 11/26 tools: counter: Append extra cflags

In reply to Leo Yan

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

---
 tools/counter/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/counter/Makefile +1 -0
index d82d35a520f610260abb43f6ae28e56d73268231..1495c0e03d9c00653a24bc92b24d83340dbb4909 100644
@@ -14,6 +14,7 @@ MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include \
 	 -I$(srctree)/tools/include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := counter_example counter_watch_events
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH v5 12/26 tools: dma: Append extra cflags

In reply to Leo Yan

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

---
 tools/dma/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/dma/Makefile +1 -0
index e4abf37bf020ca613b6dca340299198cb887126a..86d3d2e361110b664b885c0e9423660a2d5e8221 100644
@@ -17,6 +17,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := dma_map_benchmark
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#16 lore raw

PATCH v5 13/26 tools: gpio: Append extra cflags

In reply to Leo Yan

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

---
 tools/gpio/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/gpio/Makefile +1 -0
index 342e056c8c665ac075041ff7c7ca7ba94c691187..05a0057ec361626f41ca87e5295691364f03d2cb 100644
@@ -17,6 +17,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon gpio-watch
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#17 lore raw

PATCH v5 14/26 tools: hv: Append extra cflags

In reply to Leo Yan

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

---
 tools/hv/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/hv/Makefile +1 -0
index 34ffcec264ab0fdf7075d25456ef012cb866796b..a85f2208a8e3c9532b0d2cc750fc2a872ab4bb07 100644
@@ -18,6 +18,7 @@ MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
 override CFLAGS += -Wno-address-of-packed-member
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := hv_kvp_daemon hv_vss_daemon
 ifneq ($(ARCH), aarch64)
Signature · 1 line
2.34.1
author
#18 lore raw

PATCH v5 15/26 tools: iio: Append extra cflags

In reply to Leo Yan

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

---
 tools/iio/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/iio/Makefile +1 -0
index 3bcce0b7d10f42208765be83dac047faa2e0ceb1..f0428421792d1cbe5ff71b01b67f23eb908fa714 100644
@@ -13,6 +13,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+override CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := iio_event_monitor lsiio iio_generic_buffer
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#19 lore raw

PATCH v5 16/26 tools: mm: Append extra cflags

In reply to Leo Yan

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

---
 tools/mm/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/mm/Makefile +1 -0
index f5725b5c23aa261994b5b42f37c443bee2edebf2..d48491da2edc95fca57c0707c42961fde7b02072 100644
@@ -10,6 +10,7 @@ LIB_DIR = ../lib/api
 LIBS = $(LIB_DIR)/libapi.a
 
 CFLAGS += -Wall -Wextra -I../lib/ -pthread
+CFLAGS += $(EXTRA_CFLAGS)
 LDFLAGS += $(LIBS) -pthread
 
 all: $(BUILD_TARGETS)
Signature · 1 line
2.34.1
author
#20 lore raw

PATCH v5 17/26 tools: objtool: Append extra host cflags

In reply to Leo Yan

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

---
 tools/objtool/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tools/objtool/Makefile +2 -0
index b71d1886022e9b3d9fde52bf73bd502aa20d173e..a41fb2a46217bb14fcd8d0941c99d102945c3f2d 100644
@@ -60,6 +60,8 @@ INCLUDES := -I$(srctree)/tools/include \
 	    -I$(srctree)/tools/objtool/arch/$(SRCARCH)/include \
 	    -I$(LIBSUBCMD_OUTPUT)/include
 
+HOSTCFLAGS      += $(HOST_EXTRACFLAGS)
+
 OBJTOOL_CFLAGS  := -std=gnu11 -fomit-frame-pointer -O2 -g $(WARNINGS)	\
 		   $(INCLUDES) $(LIBELF_FLAGS) $(LIBXXHASH_CFLAGS) $(HOSTCFLAGS)
 
Signature · 1 line
2.34.1
author
#21 lore raw

PATCH v5 18/26 tools: power: acpi: Append extra cflags

In reply to Leo Yan

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

---
 tools/power/acpi/Makefile.config | 1 +
 1 file changed, 1 insertion(+)
tools/power/acpi/Makefile.config +1 -0
index cd7106876a5f39dfda38e286c54c3a7c268b34a2..11088deab42ee5a793f978feb9f4f7bb7024c48c 100644
@@ -69,6 +69,7 @@ KERNEL_INCLUDE := $(OUTPUT)include
 ACPICA_INCLUDE := $(srctree)/../../../drivers/acpi/acpica
 CFLAGS += -D_LINUX -I$(KERNEL_INCLUDE) -I$(ACPICA_INCLUDE)
 CFLAGS += $(WARNINGS)
+CFLAGS += $(EXTRA_CFLAGS)
 MKDIR = mkdir
 
 ifeq ($(strip $(V)),false)
Signature · 1 line
2.34.1
author
#22 lore raw

PATCH v5 19/26 tools: power: x86/intel-speed-select: Append extra cflags

In reply to Leo Yan

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

---
 tools/power/x86/intel-speed-select/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/power/x86/intel-speed-select/Makefile +1 -0
index 6b299aae2ded89c3a70a3649996e258b98001ee7..5947afce9fde7c054647fa3ee0c661b60b863f29 100644
@@ -20,6 +20,7 @@ NL3_CFLAGS = -I/usr/include/libnl3
 endif
 
 override CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include $(NL3_CFLAGS)
+override CFLAGS += $(EXTRA_CFLAGS)
 override LDFLAGS += -lnl-genl-3 -lnl-3
 
 ALL_TARGETS := intel-speed-select
Signature · 1 line
2.34.1
author
#23 lore raw

PATCH v5 20/26 tools: sched_ext: Append extra cflags

In reply to Leo Yan

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

---
 tools/sched_ext/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/sched_ext/Makefile +1 -0
index 21554f0896923fa9c2e5b98c85d65d964152e592..e0d705b2fe0ca763fece5137ea43ccb776d18012 100644
@@ -92,6 +92,7 @@ endif
 CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS)			\
 	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)				\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 # Silence some warnings when compiled with clang
 ifneq ($(LLVM),)
Signature · 1 line
2.34.1
author
#24 lore raw

PATCH v5 21/26 tools: spi: Append extra cflags

In reply to Leo Yan

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

---
 tools/spi/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/spi/Makefile +1 -0
index 7fccd245a53515ab019529c8e5e91a044eaaa68b..82f9bc6365911b962422633147b108fabc70a2db 100644
@@ -13,6 +13,7 @@ endif
 MAKEFLAGS += -r
 
 CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
+CFLAGS += $(EXTRA_CFLAGS)
 
 ALL_TARGETS := spidev_test spidev_fdx
 ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS))
Signature · 1 line
2.34.1
author
#25 lore raw

PATCH v5 22/26 tools: tracing: Append extra cflags

In reply to Leo Yan

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

---
 tools/tracing/latency/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/tracing/latency/Makefile +1 -0
index 257a56b1899f23837de533353e9c2cebdb6035bd..d32a6a13753035355f0e3a05bf88870b8aa55f64 100644
@@ -57,6 +57,7 @@ ifeq ($(config),1)
 endif
 
 CFLAGS		+= $(INCLUDES) $(LIB_INCLUDES)
+CFLAGS		+= $(EXTRA_CFLAGS)
 
 export CFLAGS OUTPUT srctree
 
Signature · 1 line
2.34.1
author
#26 lore raw

PATCH v5 23/26 tools: usb: Append extra cflags

In reply to Leo Yan

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

---
 tools/usb/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/usb/Makefile +1 -0
index c6235667dd46223b646bbab5c82c810221750182..42362dcdf2743dace62a5385a7d2707c42bdd3c0 100644
@@ -14,6 +14,7 @@ endif
 MAKEFLAGS += -r
 
 override CFLAGS += -O2 -Wall -Wextra -g -D_GNU_SOURCE -I$(OUTPUT)include -I$(srctree)/tools/include
+override CFLAGS += $(EXTRA_CFLAGS)
 override LDFLAGS += -lpthread
 
 ALL_TARGETS := testusb ffs-test
Signature · 1 line
2.34.1
author
#27 lore raw

PATCH v5 24/26 selftests/hid: Append extra cflags

In reply to Leo Yan

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

---
 tools/testing/selftests/hid/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/hid/Makefile +1 -0
index 50ec9e0406abaf316fba232c5da1967e27d0fa9b..686c5f79ca592c2ce94e37b39ab5c3ebe8483f79 100644
@@ -26,6 +26,7 @@ HOSTPKG_CONFIG := pkg-config
 
 CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(OUTPUT)
 CFLAGS += -I$(OUTPUT)/tools/include
+CFLAGS += $(EXTRA_CFLAGS)
 
 LDLIBS += -lelf -lz -lrt -lpthread
 
Signature · 1 line
2.34.1
author
#28 lore raw

PATCH v5 25/26 selftests/nolibc: Append extra cflags

In reply to Leo Yan

Include tools/scripts/Makefile.include, which may provide additional flags in EXTRA_CFLAGS, and append to CFLAGS.

Since Makefile.include already defines cc-option, remove the redundant definition and include.

---
 tools/testing/selftests/nolibc/Makefile | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/nolibc/Makefile +3 -4
index 0370489d938b540721a4fb814ebb68c463760e73..7b65e6f9840b035c1d8580574fc3145318b278b4 100644
@@ -3,18 +3,17 @@
 TEST_GEN_PROGS := nolibc-test libc-test
 
 include ../lib.mk
-include $(top_srcdir)/scripts/Makefile.compiler
-
-cc-option = $(call __cc-option, $(CC),,$(1),$(2))
+include $(top_srcdir)/tools/scripts/Makefile.include
 
 include Makefile.include
 
 $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
 	 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
-	 $(CFLAGS_NOLIBC_TEST)
+	 $(CFLAGS_NOLIBC_TEST) $(EXTRA_CFLAGS)
 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
 $(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers
 
+$(OUTPUT)/libc-test: CFLAGS += $(EXTRA_CFLAGS)
 $(OUTPUT)/libc-test: nolibc-test.c nolibc-test-linkage.c
 	$(call msg,CC,,$@)
 	$(Q)$(LINK.c) $^ -o $@
Signature · 1 line
2.34.1
author
#29 lore raw

PATCH v5 26/26 selftests/sched_ext: Append extra cflags

In reply to Leo Yan

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

---
 tools/testing/selftests/sched_ext/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/sched_ext/Makefile +1 -0
index 006300ac6dff88a48104977398d8517753e7e385..2c8fbc9f8666ec4d6f75dbdd7602bd4f2b502aed 100644
@@ -56,6 +56,7 @@ endif
 CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS)			\
 	  -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)				\
 	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(CURDIR)/include -I$(SCXTOOLSINCDIR)
+CFLAGS += $(EXTRA_CFLAGS)
 
 # Silence some warnings when compiled with clang
 ifneq ($(LLVM),)
Signature · 1 line
2.34.1
maintainer
#30 lore raw
In reply to Leo Yan
Quoted 4 lines · Leo Yan

Thank you for reviewing and commenting on v4.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

So why can't you just copy-paste that yourself, and save me from having to go find a browser and copy/paste the stupid link, just to find something that should have been here to begin with?!

maintainer
#32 lore raw
In reply to Leo Yan

2026-03-18 09:16 UTC+0000 ~ Leo Yan <[email protected]>

Quoted 8 lines

Thank you for reviewing and commenting on v4.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

In this version, the BPF related patches have been split out and will be sent separately (note that I have kept the bpftool patches in this series, as I have gathered Quentin's Acked-by tags).

Hi Leo, if it's all the same to you, could bpftool patches go through the bpf-next tree as well, please? (You can keep my tags, of course.)

Thanks,
Quentin

author
#33 lore raw
Quoted 6 lines · Quentin Monnet

2026-03-18 09:16 UTC+0000 ~ Leo Yan <[email protected]>

Quoted 5 lines

Thank you for reviewing and commenting on v4.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

Quoted 7 lines
Quoted 3 lines

In this version, the BPF related patches have been split out and will be sent separately (note that I have kept the bpftool patches in this series, as I have gathered Quentin's Acked-by tags).

Hi Leo, if it's all the same to you, could bpftool patches go through the bpf-next tree as well, please? (You can keep my tags, of course.)

Sure!

I need to look into BPF CI failures, seems the extra cflags causes regressions in eBPF selftest [1][2]. After make things clear, I will send patch series separately (a common one and a BPF one).

Thanks,
Leo

[1] https://github.com/kernel-patches/bpf/actions/runs/23238743267/job/67551317220
[2] https://github.com/kernel-patches/bpf/actions/runs/23238743267/job/67551683133

author
#31 lore raw
In reply to Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Leo Yan

Thank you for reviewing and commenting on v4.

For anyone new to the series, the reason for appending this compiler option is described in v3 (see "Link to v3" below).

So why can't you just copy-paste that yourself, and save me from having to go find a browser and copy/paste the stupid link, just to find something that should have been here to begin with?!

Sorry about that!

I didn't expect a link to cause trouble. I'll include the information instead later.

reviewer
#7 lore raw

Re: [PATCH v5 06/26] tools build: Append -fzero-init-padding-bits=all to extra cflags

In reply to Leo Yan
Quoted 57 lines · Leo Yan

GCC-15 release claims [1]:

{0} initializer in C or C++ for unions no longer guarantees clearing of the whole union (except for static storage duration initialization), it just initializes the first union member to zero. If initialization of the whole union including padding bits is desirable, use {} (valid in C23 or C++) or use -fzero-init-padding-bits=unions option to restore old GCC behavior.

As a result, this new behaviour might cause unexpected data when we initialize a union with using the '{ 0 }' initializer.

Since commit dce4aab8441d ("kbuild: Use -fzero-init-padding-bits=all"), the kernel has enabled -fzero-init-padding-bits=all to zero padding bits in unions and structures. This commit applies the same option for tools building.

The option is not supported by any version older than GCC 15, nor is it supported by LLVM. This patch adds the cc-option and host-cc-option functions to dynamically detect compiler option and append it to the EXTRA_CFLAGS and HOST_EXTRACFLAGS respectively.

[1] https://gcc.gnu.org/gcc-15/changes.html

---
 tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/scripts/Makefile.include +17 -0
index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;              \
+       TMP=$(TMPOUT)/tmp;              \
+       trap "rm -rf $(TMPOUT)" EXIT;   \
+       mkdir -p $(TMPOUT);             \
+       if ($(1)) >/dev/null 2>&1;      \
+       then echo "$(2)";               \
+       else echo "$(3)";               \
+       fi)
+
+# cc-option

tools/scripts/Makefile.include else · line 166

Quoted 10 lines
tools/scripts/Makefile.include +10 -0
+# Usage: CFLAGS += $(call cc-option,-march=winchip-c6,-march=i586)
+cc-option = $(call try-run, \
+       $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+host-cc-option = $(call try-run, \
+       $(HOSTCC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)

Sashiko [1] noted a possibly unintended consequence of this change for libbpf:

Does this override unintentionally drop the default compiler flags for libbpf?

Looking at tools/lib/bpf/Makefile, the default compiler flags like -g and -O2 are only applied if EXTRA_CFLAGS is undefined:

tools/lib/bpf/Makefile:
ifndef EXTRA_CFLAGS
EXTRA_CFLAGS := -g -O2
endif

Since tools/scripts/Makefile.include unconditionally appends to EXTRA_CFLAGS here, the variable will be defined before tools/lib/bpf/Makefile evaluates it.

This causes libbpf to silently drop the default -g and -O2 flags.

While later commits in this series fix this exact issue for tools/lib/thermal/Makefile, it appears tools/lib/bpf/Makefile was missed.

Thanks,
Ian

[1] https://sashiko.dev/#/patchset/20260318-tools_build_fix_zero_init-v5-0-bbeffd8da199%40arm.com

Quoted 10 lines
tools/scripts/Makefile.include +3 -1
+HOST_FLAG_ZERO_INIT := $(call host-cc-option,-fzero-init-padding-bits=all)
+override HOST_EXTRACFLAGS += $(HOST_FLAG_ZERO_INIT)
+
 ifneq ($(findstring $(MAKEFLAGS), w),w)
 PRINT_DIR = --no-print-directory
 else
 
--

2.34.1

author
#8 lore raw

Re: [PATCH v5 06/26] tools build: Append -fzero-init-padding-bits=all to extra cflags

Hi Ian,

On Wed, Mar 18, 2026 at 09:38:43AM -0700, Ian Rogers wrote:

[...]

Quoted 51 lines
Quoted 28 lines
 tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/scripts/Makefile.include +17 -0
index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;              \
+       TMP=$(TMPOUT)/tmp;              \
+       trap "rm -rf $(TMPOUT)" EXIT;   \
+       mkdir -p $(TMPOUT);             \
+       if ($(1)) >/dev/null 2>&1;      \
+       then echo "$(2)";               \
+       else echo "$(3)";               \
+       fi)
+
+# cc-option

tools/scripts/Makefile.include else · line 166

Quoted 10 lines
tools/scripts/Makefile.include +10 -0
+# Usage: CFLAGS += $(call cc-option,-march=winchip-c6,-march=i586)
+cc-option = $(call try-run, \
+       $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+host-cc-option = $(call try-run, \
+       $(HOSTCC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)

Sashiko [1] noted a possibly unintended consequence of this change for libbpf:

Does this override unintentionally drop the default compiler flags for libbpf?

Looking at tools/lib/bpf/Makefile, the default compiler flags like -g and -O2 are only applied if EXTRA_CFLAGS is undefined:

tools/lib/bpf/Makefile:
ifndef EXTRA_CFLAGS
EXTRA_CFLAGS := -g -O2
endif

Quoted 7 lines

Since tools/scripts/Makefile.include unconditionally appends to EXTRA_CFLAGS here, the variable will be defined before tools/lib/bpf/Makefile evaluates it.

This causes libbpf to silently drop the default -g and -O2 flags.

While later commits in this series fix this exact issue for tools/lib/thermal/Makefile, it appears tools/lib/bpf/Makefile was missed.

This is expected. As Alexei suggested, BPF patches should go via the bpf tree and be sent separately [2]. As a result, the AI review has no full context and misses the BPF pieces.

I will send a new series and a BPF/bpftool series separately. If AI still complains this, I'd suggest we simply ignore this false positive.

Thanks,
Leo

[2] https://lore.kernel.org/linux-perf-users/CAADnVQLN6bYHnusnC5QnDoGGUO-A=qnM6fP9bhEVXEHD6_y9EQ@mail.gmail.com/#t

#9 lore raw

Re: [PATCH v5 06/26] tools build: Append -fzero-init-padding-bits=all to extra cflags

In reply to Leo Yan
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Unconditional appending to `EXTRA_CFLAGS` breaks default optimization and debug flags for…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Unconditional appending to EXTRA_CFLAGS breaks default optimization and debug flags for libbpf.
- [High] The try-run macro attempts to write to the source tree during Makefile parsing, breaking out-of-tree builds on read-only filesystems.
- [Medium] The compiler flag -fzero-init-padding-bits=all is appended to HOST_EXTRACFLAGS, which is never evaluated by the tools build framework, resulting in silent omission of the flag for host programs like bpftool.
- [Medium] The try-run macro uses a predictable temporary directory name (.tmp_$$), exposing the build process to a local symlink attack (CWE-377).
--

--- Patch [6]: [PATCH v5 06/26] tools build: Append -fzero-init-padding-bits=all to extra cflags --- commit 526bce400539d6fdc8b0869f05e77b5305dcbce2
Author: Leo Yan <[email protected]>

tools build: Append -fzero-init-padding-bits=all to extra cflags

This commit adds -fzero-init-padding-bits=all for tools building to mitigate a new GCC-15 union initialization behavior. It implements cc-option and host-cc-option macros to dynamically detect support for this flag.

Quoted 4 lines
tools/scripts/Makefile.include +0 -0
index b5ecf137febca..a2397ceae512c 100644

[ ... ]

tools/scripts/Makefile.include else · line 148

Quoted 13 lines
tools/scripts/Makefile.include +9 -0
@@ -137,6 +137,36 @@ else
 EXTRA_WARNINGS += -Wshadow
 endif
 
+# output directory for tests below
+TMPOUT = .tmp_$$$$
+
+# try-run
+# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
+# Exit code chooses option. "$$TMP" serves as a temporary file and is
+# automatically cleaned up.
+try-run = $(shell set -e;		\
+	TMP=$(TMPOUT)/tmp;		\

[Severity: Medium]
Is this predictable temporary directory safe against local symlink attacks?

If a user builds the kernel tools in a shared directory, an attacker could pre-create .tmp_<PID> directories containing a symlink named tmp pointing to a target file. Because mkdir -p succeeds on existing directories, the compiler command overwrites the attacker's symlink target.

tools/scripts/Makefile.include else · line 150

Quoted 2 lines
tools/scripts/Makefile.include +2 -0
+	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\

[Severity: High]
Does this directory creation break out-of-tree builds on read-only trees?

Unlike Kbuild, tools Makefiles evaluate shell commands in the source tree directory. This attempts to write to the source tree instead of the designated output directory. The mkdir fails on a read-only filesystem, and set -e aborts the snippet, dropping the compiler flag check.

Quoted 4 lines
tools/scripts/Makefile.include +4 -0
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi)

[ ... ]

tools/scripts/Makefile.include else · line 166

Quoted 3 lines
tools/scripts/Makefile.include +3 -0
+# Explicitly clear padding bits with the initializer '{ 0 }'
+FLAG_ZERO_INIT := $(call cc-option,-fzero-init-padding-bits=all)
+override EXTRA_CFLAGS += $(FLAG_ZERO_INIT)

[Severity: High]
Will unconditionally appending to EXTRA_CFLAGS break the default optimization and debug flags for libraries like libbpf?

Because Makefile.include is included before libbpf evaluates its flags, EXTRA_CFLAGS is always defined. This forces libbpf to bypass its fallback logic:

tools/lib/bpf/Makefile:
ifdef EXTRA_CFLAGS

    CFLAGS := $(EXTRA_CFLAGS)
  else
    CFLAGS := -g -O2

endif

and builds the library without debug symbols and optimizations.

tools/scripts/Makefile.include else · line 168

Quoted 2 lines
tools/scripts/Makefile.include +2 -0
+HOST_FLAG_ZERO_INIT := $(call host-cc-option,-fzero-init-padding-bits=all)
+override HOST_EXTRACFLAGS += $(HOST_FLAG_ZERO_INIT)

[Severity: Medium]
Is HOST_EXTRACFLAGS evaluated by the standalone tools build framework?

The tools build framework relies exclusively on HOSTCFLAGS for host programs, so appending to HOST_EXTRACFLAGS causes the flag to be ignored for tools like bpftool.

Quoted 4 lines

+
ifneq ($(findstring $(MAKEFLAGS), w),w)
PRINT_DIR = --no-print-directory
else

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6