Skip to contentSkip to conversation

tools build: Use -fzero-init-padding-bits=all

PATCH Superseded Leo Yan 2 participants 3 messages Mar 28, 2025

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

PATCH 1/1 tools build: Use -fzero-init-padding-bits=all

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 neither by any version older than GCC 15 and is also not supported by LLVM, this patch adds the cc-option function to dynamically detect the compiler option.

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

---

This patch was originally from [1]. After consideration, the top level of the tools directory is a better place to accommodate this option rather than perf folder.

[1] https://lore.kernel.org/linux-perf-users/[email protected]/

 tools/scripts/Makefile.include | 13 +++++++++++++
 1 file changed, 13 insertions(+)
tools/scripts/Makefile.include +13 -0
index 0aa4005017c7..e912a10afd89 100644
@@ -40,6 +40,19 @@ EXTRA_WARNINGS += -Wwrite-strings
 EXTRA_WARNINGS += -Wformat
 EXTRA_WARNINGS += -Wno-type-limits
 
+try-run = $(shell set -e;		\
+	if ($(1)) >/dev/null 2>&1;	\
+	then echo "$(2)";		\
+	else echo "$(3)";		\
+	fi)
+
+__cc-option = $(call try-run,\
+	$(1) -Werror $(2) -c -x c /dev/null -o /dev/null,$(2),)
+cc-option = $(call __cc-option, $(CC),$(1))
+
+# Explicitly clear padding bits with the initializer '{ 0 }'
+CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
+
 # Makefiles suck: This macro sets a default value of $(2) for the
 # variable named by $(1), unless the variable has been set by
 # environment or command line. This is necessary for CC and AR
Signature · 1 line
2.34.1
#1 lore raw
Quoted 41 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 neither by any version older than GCC 15 and is also not supported by LLVM, this patch adds the cc-option function to dynamically detect the compiler option.

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

---

This patch was originally from [1]. After consideration, the top level of the tools directory is a better place to accommodate this option rather than perf folder.

[1] https://lore.kernel.org/linux-perf-users/[email protected]/

 tools/scripts/Makefile.include | 13 +++++++++++++
 1 file changed, 13 insertions(+)
tools/scripts/Makefile.include +0 -0
index 0aa4005017c7..e912a10afd89 100644

tools/scripts/Makefile.include EXTRA_WARNINGS += -Wwrite-strings · line 42

Quoted 3 lines
tools/scripts/Makefile.include +0 -0
@@ -40,6 +40,19 @@ EXTRA_WARNINGS += -Wwrite-strings
 EXTRA_WARNINGS += -Wformat
 EXTRA_WARNINGS += -Wno-type-limits
 

It'd be nice to bring in the comment for try-run that's in ./scripts/Makefile.compiler:

```
# 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.
```

tools/scripts/Makefile.include EXTRA_WARNINGS += -Wwrite-strings · line 52

Quoted 10 lines
tools/scripts/Makefile.include +10 -0
+try-run = $(shell set -e;              \
+       if ($(1)) >/dev/null 2>&1;      \
+       then echo "$(2)";               \
+       else echo "$(3)";               \
+       fi)
+
+__cc-option = $(call try-run,\
+       $(1) -Werror $(2) -c -x c /dev/null -o /dev/null,$(2),)
+cc-option = $(call __cc-option, $(CC),$(1))
+

I see differences with the ./scripts/Makefile.compiler version of these functions:

```
# __cc-option
# Usage: MY_CFLAGS += $(call
__cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
__cc-option = $(call try-run,\
       $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))

cc-option = $(call __cc-option, $(CC),\
       $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
```

I'm just wondering why as if we need to update these in the future it'd be easier if the two were identical.

Thanks,
Ian

Quoted 9 lines
tools/scripts/Makefile.include +3 -1
+# Explicitly clear padding bits with the initializer '{ 0 }'
+CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
+
 # Makefiles suck: This macro sets a default value of $(2) for the
 # variable named by $(1), unless the variable has been set by
 # environment or command line. This is necessary for CC and AR
--

2.34.1

author
#2 lore raw

Hi Ian,

On Fri, Mar 28, 2025 at 09:39:41AM -0700, Ian Rogers wrote:

[...]

Quoted 8 lines

It'd be nice to bring in the comment for try-run that's in ./scripts/Makefile.compiler:

```
# 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.
```

It is fine for me to reuse the comment from Makefile.compiler and add a temporary folder for the try result.

Quoted 23 lines

tools/scripts/Makefile.include EXTRA_WARNINGS += -Wwrite-strings · line 52

Quoted 10 lines
tools/scripts/Makefile.include +10 -0
+try-run = $(shell set -e;              \
+       if ($(1)) >/dev/null 2>&1;      \
+       then echo "$(2)";               \
+       else echo "$(3)";               \
+       fi)
+
+__cc-option = $(call try-run,\
+       $(1) -Werror $(2) -c -x c /dev/null -o /dev/null,$(2),)
+cc-option = $(call __cc-option, $(CC),$(1))
+

I see differences with the ./scripts/Makefile.compiler version of these functions:

```
# __cc-option
# Usage: MY_CFLAGS += $(call
__cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
__cc-option = $(call try-run,\
       $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))

cc-option = $(call __cc-option, $(CC),\
       $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
```
Quoted 2 lines

I'm just wondering why as if we need to update these in the future it'd be easier if the two were identical.

Note, I do not see a requirement for passing two options for tools building. In the next spin, I will keep to support only one option.

Thanks for review!

Leo