[PATCH dwarves 2/2] tests: Add GCC default-BTF optimized parameter coverage

Alan Maguire <[email protected]> Tue, 14 Jul 2026 14:50:35 +0100
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <[email protected]>
Add a regression test for GCC optimized parameters when true_signature is not
enabled.

The test builds one normal externally visible function that must remain in
default BTF, and one static function that GCC optimizes into an .isra clone
with an optimized-out parameter.  Default BTF cannot safely represent the
clone using the original DWARF prototype, so the test verifies it is skipped
while the normal function is still emitted.

Signed-off-by: Alan Maguire <[email protected]>
---
 tests/gcc_optimized_default.sh | 69 ++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 tests/gcc_optimized_default.sh

diff --git a/tests/gcc_optimized_default.sh b/tests/gcc_optimized_default.sh
new file mode 100755
index 0000000..2180666
--- /dev/null
+++ b/tests/gcc_optimized_default.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+
+source test_lib.sh
+
+outdir=$(make_tmpdir)
+
+# Comment this out to save test data.
+trap cleanup EXIT
+
+title_log "Validation of GCC optimized parameters in default BTF."
+
+CC=${CC:-gcc}
+if ! command -v "$CC" >/dev/null 2>&1; then
+	info_log "skip: gcc not available"
+	test_skip
+fi
+
+src=${outdir}/gcc_optimized_default.c
+obj=${outdir}/gcc_optimized_default
+btf=${outdir}/gcc_optimized_default.btf
+log=${outdir}/gcc_optimized_default.log
+
+cat > "$src" << EOF
+__attribute__((noinline)) int bar(int a, int b)
+{
+	return a + 1;
+}
+
+__attribute__((noinline)) static int foo(int a, int b, int c)
+{
+	return a * b - a - b;
+}
+
+int a, b, c;
+int main(void)
+{
+	return bar(a, c) + foo(a, b, c);
+}
+EOF
+
+"$CC" -g -O2 -o "$obj" "$src"
+if [[ $? -ne 0 ]]; then
+	error_log "Could not compile $src"
+	test_fail
+fi
+
+LLVM_OBJCOPY=objcopy pahole -J --btf_features=default --btf_encode_detached="$btf" --verbose "$obj" > "$log"
+if [[ $? -ne 0 ]]; then
+	error_log "Could not encode BTF for $obj"
+	test_fail
+fi
+
+if ! grep -q "foo : skipping BTF encoding of function due to optimized parameters" "$log"; then
+	error_log "foo() should be skipped for default BTF due to optimized parameters"
+	test_fail
+fi
+
+if pfunct --all --format_path=btf "$btf" | grep -Fq "int foo("; then
+	error_log "foo() should be absent from default BTF"
+	test_fail
+fi
+
+if ! pfunct --all --format_path=btf "$btf" | grep -Fq "int bar(int a, int b);"; then
+	error_log "bar() is missing from default BTF"
+	test_fail
+fi
+
+test_pass
-- 
2.43.5