xref: /linux/tools/objtool/tests/generic/test-thinlto-ambiguity.sh (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Two ThinLTO-promoted symbols sharing a demangled name must be paired up
5# correctly.
6#
7# A file-local symbol which ThinLTO has to make visible is renamed
8# helper.llvm.<hash>.  With two such helpers in one link the original and the
9# patched object hold two each, all four spelled differently, and demangling
10# gives "helper" for all of them -- so the name is not enough to say which
11# corresponds to which.
12#
13# Getting it wrong is silent and specific: the patch is built against the wrong
14# body, so one call site gets the other helper's arithmetic.  Nothing fails to
15# build and nothing fails to load.
16#
17# test-thinlto-local covers the unambiguous case, one promoted symbol whose
18# hash moved.  This is the case where demangling alone is not an answer.
19#
20# The outcome is asserted, not the machinery: with the clang tested here the
21# pairing succeeds even with the .llvm.<hash> suffix map disabled and with
22# llvm_suffix() stubbed out, so no single-line sabotage distinguishes it.  The
23# tiered matcher this case was written for is not needed for this shape.
24#
25# Covers the same ground as corpus/x86_64-llvm-thinlto/
26# thin-lto-demangled-ambiguity and thin-lto-demangled-global-match in Joe
27# Lawrence's klp-build unit test corpus.
28
29. "$(dirname "$0")/../lib.sh"
30
31setup
32clang_only "ThinLTO requires clang"
33
34find_thinlto_toolchain ||
35	probe_skip "no matching clang/lld pair for a ThinLTO link; set THIN_LD to one"
36
37build_thinlto()		# $1 output object, $2 extra flags
38{
39	local t
40	for t in "" -DTU_B -DTU_C; do
41		$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections \
42			$2 $t -c "$FIXTURES_DIR/thinlto_ambiguity.c" \
43			-o "$workdir/tu$t.o" 2>/dev/null || return 1
44	done
45	"$THIN_LD" -r "$workdir/tu.o" "$workdir/tu-DTU_B.o" "$workdir/tu-DTU_C.o" \
46		-o "$1" 2>/dev/null || return 1
47}
48
49build_thinlto "$workdir/orig.o" "" ||
50	probe_skip "ThinLTO build failed ($THIN_CC, $THIN_LD)"
51build_thinlto "$workdir/patched.o" -DPATCHED ||
52	probe_skip "ThinLTO build failed ($THIN_CC, $THIN_LD)"
53
54# The premise: two promoted helpers per object, and exactly one of them kept
55# its hash -- the one the patch did not touch.  Without that there is nothing
56# to disambiguate.
57orig_syms="$(in_symbols orig.o    | grep -oE 'helper\.llvm\.[0-9]+' | sort -u)"
58new_syms="$( in_symbols patched.o | grep -oE 'helper\.llvm\.[0-9]+' | sort -u)"
59[ "$(echo "$orig_syms" | wc -l)" = 2 ] && [ "$(echo "$new_syms" | wc -l)" = 2 ] ||
60	probe_skip "ThinLTO did not promote two distinct helpers here"
61
62kept="$(comm -12 <(echo "$orig_syms") <(echo "$new_syms"))"
63moved="$(comm -13 <(echo "$orig_syms") <(echo "$new_syms"))"
64[ "$(echo "$kept" | wc -w)" = 1 ] && [ "$(echo "$moved" | wc -w)" = 1 ] ||
65	probe_skip "expected one helper to keep its hash and one to move"
66
67run_diff
68
69# Exactly one helper is cloned, and it is the one whose body changed.  Cloning
70# the other, or both, is what a wrong pairing looks like.
71assert_not_patched "$kept"
72
73n="$(out_sections | grep -cE '[[:space:]]\.text\.helper\.llvm\.[0-9]+[[:space:]]')"
74[ "$n" = 1 ] ||
75	fail "expected 1 cloned helper, found $n"
76
77pass "ThinLTO helpers sharing a demangled name paired up correctly"
78