xref: /linux/tools/objtool/tests/generic/test-thinlto-local.sh (revision 7fd24723f1601340ee09bca93989bd1fb4515783)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Correlating ThinLTO-promoted locals requires demangling the .llvm.<hash>
5# suffix, and the resulting klp relocation must name the original symbol: that
6# is the one in the running kernel's kallsyms.
7
8. "$(dirname "$0")/../lib.sh"
9
10setup
11clang_only "ThinLTO requires clang"
12
13find_thinlto_toolchain ||
14	probe_skip "no matching clang/lld pair for a ThinLTO link; set THIN_LD to one"
15
16build_thinlto()		# $1 output object, $2 extra flags
17{
18	$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 \
19		-c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_a.o" 2>/dev/null || return 1
20	$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 -DTU_B \
21		-c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_b.o" 2>/dev/null || return 1
22	"$THIN_LD" -r "$workdir/tu_a.o" "$workdir/tu_b.o" -o "$1" 2>/dev/null || return 1
23}
24
25build_thinlto "$workdir/orig.o" "" ||
26	probe_skip "ThinLTO build failed ($THIN_CC, $THIN_LD)"
27build_thinlto "$workdir/patched.o" -DPATCHED ||
28	probe_skip "ThinLTO build failed ($THIN_CC, $THIN_LD)"
29
30orig_sym="$(in_symbols orig.o    | grep -o 'counter\.llvm\.[0-9]*' | head -1)"
31new_sym="$( in_symbols patched.o | grep -o 'counter\.llvm\.[0-9]*' | head -1)"
32
33[ -n "$orig_sym" ] && [ -n "$new_sym" ] ||
34	probe_skip "$THIN_CC did not promote the local symbol"
35
36# Equal hashes would make plain name matching work, testing nothing.
37[ "$orig_sym" != "$new_sym" ] ||
38	probe_skip "$THIN_CC gave the same ThinLTO hash for both builds"
39
40run_diff
41assert_patched target
42
43out_symbols | grep -q "\.klp\.sym\.vmlinux\.$orig_sym," ||
44	fail "expected a klp relocation naming $orig_sym"
45out_symbols | grep -q "\.klp\.sym\.vmlinux\.$new_sym," &&
46	fail "klp relocation names $new_sym, which the running kernel does not have"
47
48pass "ThinLTO-mangled local correlated across differing hashes ($THIN_CC, $THIN_LD)"
49