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