Files
linux/tools/objtool/tests/generic/test-cold-function.sh
T
Puranjay Mohan 76e7630d90 objtool/klp: Add test for cold function halves
The compiler splits unlikely code into a separate foo.cold symbol.  Both
halves are the same function and both belong in the livepatch: carrying
only the hot part leaves the cold path branching into unpatched code.

GCC needs -freorder-blocks-and-partition to split reliably.  The flag is
probed rather than assumed, and the test skips when the compiler declines
to split at all.

Signed-off-by: Puranjay Mohan <[email protected]>
Signed-off-by: Song Liu <[email protected]>
Signed-off-by: Josh Poimboeuf <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Assisted-by: Claude:claude-opus-5
Link: https://patch.msgid.link/[email protected]
2026-09-21 11:02:54 +02:00

40 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Both halves of a split function belong to the patch; carrying only the hot
# part leaves the cold path branching into unpatched code.
. "$(dirname "$0")/../lib.sh"
# Clang does not split functions into a cold part at all, so there is nothing
# for this test to look at there. A given gcc may or may not split, which is a
# version property rather than a compiler choice -- that stays a probe below.
gcc_only "clang does not split functions into a cold part"
setup
split_flag=-freorder-blocks-and-partition
cc_supports "$split_flag" || split_flag=
build_pair cold_function.c $split_flag
# Find what the compiler called the cold half -- target.cold, target.cold.0,
# depending on version -- and name it exactly from here on.
cold_sym="$(in_symbols orig.o | awk '$NF ~ /^target\.cold/ { print $NF; exit }')"
[ -n "$cold_sym" ] ||
probe_skip "compiler did not split the function into a cold part"
run_diff
assert_patched target
# Match the name field exactly, and require it to be defined. Had the cold
# half been left behind, the branch to it would appear as an undefined
# .klp.sym.vmlinux.target.cold,0 -- a different name, which happens to contain
# this one. Asking about a column instead of the name would not tell them
# apart: readelf prints SHN_LIVEPATCH as "OS [0xff20]" and llvm-readelf as
# "OS[0xff20]", so the fields either side of the name shift between the two.
out_symbols | awk -v n="$cold_sym" '$NF == n && $(NF - 1) != "UND"' | grep -q . ||
fail "cold half ($cold_sym) was not carried into the patch"
pass "cold half carried into the patch with its parent"