scripts: check-function-names: propagate nm failures
PATCH 1/1 scripts: check-function-names: propagate nm failures
From: Hamdy Abou El Anein <[email protected]>
The symbol-name check pipes nm through awk and grep. If nm cannot read an object file, its failure status is lost and the script reports success without checking any symbols.
Capture nm output before filtering it and fail when nm fails. This keeps the existing forbidden-name detection unchanged.
Tested with a malformed object (failure), a valid object (success), and an object defining startup (failure).
--- scripts/check-function-names.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/check-function-names.sh +2 -1
@@ -13,7 +13,8 @@ if [ ! -f "$objfile" ]; then exit 1 fi -bad_symbols=$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | +symbols=$(${NM:-nm} -p "$objfile") || exit 1 +bad_symbols=$(printf '%s\n' "$symbols" | awk '$2 ~ /^[TtWw]$/ {print $3}' | grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') if [ -n "$bad_symbols" ]; then
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] scripts: check-function-names: buffering full vmlinux.o nm output in shell --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] scripts: check-function-names: buffering full vmlinux.o nm output in shell --
commit 329efcc5adb68fe0ddbe8ccd5ee716b893c4188e
Author: Hamdy Abou El Anein <[email protected]>
scripts: check-function-names: propagate nm failures
This commit captures the output of nm in a shell variable before piping it to awk and grep so that check-function-names.sh exits with an error if nm fails on the input object file.
scripts/check-function-names.sh if [ ! -f "$objfile" ]; then · line 18
Quoted 12 lines
scripts/check-function-names.sh +2
-1@@ -13,7 +13,8 @@ if [ ! -f "$objfile" ]; then exit 1 fi -bad_symbols=$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | +symbols=$(${NM:-nm} -p "$objfile") || exit 1 +bad_symbols=$(printf '%s\n' "$symbols" | awk '$2 ~ /^[TtWw]$/ {print $3}' | grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)')
[Severity: Medium]
Does capturing the entire nm output into symbols and expanding it into printf introduce a build-time performance and memory regression when linking vmlinux.o?
In scripts/Makefile.vmlinux_o, rule_ld_vmlinux.o runs this script on vmlinux.o in the serial path of the kernel link:
scripts/Makefile.vmlinux_o:cmd_check_function_names = $(srctree)/scripts/check-function-names.sh $@
define rule_ld_vmlinux.o
$(call cmd_and_savecmd,ld_vmlinux.o) $(call cmd,gen_objtooldep) $(call cmd,check_function_names)
endef
Because vmlinux.o contains the full built-in kernel symbol table (spanning many megabytes and hundreds of thousands of symbols, especially on allmodconfig or allyesconfig builds), previously nm -p streamed directly into awk in parallel and command substitution only buffered the matching bad_symbols output (which is empty in the normal case).
With this change, the shell waits for nm to finish while buffering the entire multi-megabyte symbol table in the symbols variable, and then forks a subshell that expands and copies the entire string as an argument to printf before piping it to awk, working against the serial link optimization in commit c2970d8b52d1 ("kbuild: do not sort nm output where the order is irrelevant").
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 scripts: check-function-names: propagate nm failures
In reply to Hamdy Abou El AneinFrom: Hamdy Abou El Anein <[email protected]>
The symbol-name check pipes nm through awk and grep. If nm cannot read an object file, its failure status is lost and the script reports success without checking any symbols.
Keep nm output streaming while carrying its exit status through the filter. This avoids buffering the full vmlinux.o symbol table in a shell variable during the serial kernel link.
Tested with a malformed object (failure), a valid object (success), an object defining startup (failure), and a synthetic large nm stream (success).
AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.
--- scripts/check-function-names.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/check-function-names.sh +2 -1
@@ -13,7 +13,8 @@ if [ ! -f "$objfile" ]; then exit 1 fi -bad_symbols=$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | +symbols=$(${NM:-nm} -p "$objfile") || exit 1 +bad_symbols=$(printf '%s\n' "$symbols" | awk '$2 ~ /^[TtWw]$/ {print $3}' | grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') if [ -n "$bad_symbols" ]; then
Signature · 1 line
2.56.0
PATCH v3 1/1 scripts: check-function-names: propagate nm failures
From: Hamdy Abou El Anein <[email protected]>
The symbol-name check pipes nm through awk and grep. If nm cannot read an object file, its failure status is lost and the script reports success without checking any symbols.
Keep nm output streaming while carrying its exit status through the filter. This avoids buffering the full vmlinux.o symbol table in a shell variable during the serial kernel link, as the v2 did.
Tested with a malformed object (failure), a valid object (success), an object defining startup (failure), and a synthetic large nm stream (success).
AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.
--- scripts/check-function-names.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
scripts/check-function-names.sh +22 -3
@@ -13,9 +13,28 @@ if [ ! -f "$objfile" ]; then exit 1 fi -bad_symbols=$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | - grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') - +# Keep nm output streaming while preserving its exit status. +bad_symbols=$( + { + ${NM:-nm} -p "$objfile" + nm_status=$? + printf '\n#nm_status %s\n' "$nm_status" + } | + awk ' + $1 == "#nm_status" { + if ($2 != 0) + nm_failed = 1 + next + } + $2 ~ /^[TtWw]$/ && $3 ~ /^(startup|exit|split|unlikely|hot|unknown)(\.|$)/ { + print $3 + } + END { + if (nm_failed) + exit 1 + } + ' +) || exit 1 if [ -n "$bad_symbols" ]; then echo "$bad_symbols" | while read -r sym; do echo "$objfile: error: $sym() function name creates ambiguity with -ffunction-sections" >&2
Signature · 1 line
2.56.0
Nathan Chancellor
maintainerHi Hamdy,
Quoted 5 lines · Hamdy Abou El Anein
From: Hamdy Abou El Anein <[email protected]>
The symbol-name check pipes nm through awk and grep. If nm cannot read an object file, its failure status is lost and the script reports success without checking any symbols.
If nm fails because vmlinux.o is malformed, surely that means that $(LD) is going to choke on it too? This feels like unnecessary complication and hardening for little gain. Were you able to reproduce a situation where $(NM) failed but the rest of the build succeeded?
Quoted 3 lines
Keep nm output streaming while carrying its exit status through the filter. This avoids buffering the full vmlinux.o symbol table in a shell variable during the serial kernel link, as the v2 did.
Just a work of advice for the future, avoid mentioning revisions within the commit message, they won't make sense once the patch is applied. Place differential notes below the '---' after your signoff and make sure the commit message makes sense on its own.
Quoted 51 lines
Tested with a malformed object (failure), a valid object (success), an object defining startup (failure), and a synthetic large nm stream (success).
AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.
Signed-off-by Hamdy Abou El Anein <[email protected]>--- scripts/check-function-names.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)scripts/check-function-names.sh +22
-3@@ -13,9 +13,28 @@ if [ ! -f "$objfile" ]; then exit 1 fi -bad_symbols=$(${NM:-nm} -p "$objfile" | awk '$2 ~ /^[TtWw]$/ {print $3}' | - grep -E '^(startup|exit|split|unlikely|hot|unknown)(\.|$)') - +# Keep nm output streaming while preserving its exit status. +bad_symbols=$( + { + ${NM:-nm} -p "$objfile" + nm_status=$? + printf '\n#nm_status %s\n' "$nm_status" + } | + awk ' + $1 == "#nm_status" { + if ($2 != 0) + nm_failed = 1 + next + } + $2 ~ /^[TtWw]$/ && $3 ~ /^(startup|exit|split|unlikely|hot|unknown)(\.|$)/ { + print $3 + } + END { + if (nm_failed) + exit 1 + } + ' +) || exit 1 if [ -n "$bad_symbols" ]; then echo "$bad_symbols" | while read -r sym; do echo "$objfile: error: $sym() function name creates ambiguity with -ffunction-sections" >&2Signature · 1 line
2.56.0
Signature · 2 lines
Cheers, Nathan
Hi Nathan,
Thanks for the review. I checked the vmlinux.o rule and tested with GNU binutils 2.47.
A valid object linked successfully with ld -r and was read successfully by nm. For malformed and truncated objects, I could not reproduce nm failing while ld accepted the same input: bad-format and malformed symbol-table objects failed in both tools, while truncated objects were rejected by ld.
I also tested a wrapper that runs nm successfully and then returns an error. That confirms the shell pipeline masks a failing command status, but it does not reproduce a real nm failure on a valid vmlinux.o.
I have not found the build scenario you asked for, so I am withdrawing this patch.
Regards,
Hamdy