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

Ian Rogers
2026-03-18T16:38:56Z

On Wed, Mar 18, 2026 at 2:18 AM Leo Yan <leo.yan@arm.com> wrote:
>
> 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
>
> Acked-by: Quentin Monnet <qmo@kernel.org>
> Acked-by: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
> tools/scripts/Makefile.include | 30 ++++++++++++++++++++++++++++++
> 1 file changed, 30 insertions(+)
>
> diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
> index b5ecf137febcae59f506e107a7f2e2ad72f4bef4..a2397ceae512c1bc54adb15cb1a111ff34e28e43 100644
> --- a/tools/scripts/Makefile.include
> +++ b/tools/scripts/Makefile.include
> @@ -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)

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

> +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
>