1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# A Clang switch jump table travels with the function it belongs to. 5# 6# For a dense enough switch Clang emits the targets as a table in 7# .rodata..Lswitch.table.<function>, named after the function but not part of 8# it. klp diff has to associate the two: the patched function indexes into 9# that table, so a clone which does not bring it along jumps through whatever 10# the kernel's copy holds -- which, when the patch changed the switch, is the 11# wrong set of targets. 12# 13# That is an indirect jump to a stale address, not a missing symbol, so nothing 14# reports it at build or load time. 15# 16# objtool has no switch-specific code: the table is carried by the general 17# mechanism for data a cloned function references. So this is a regression 18# test on that mechanism reaching a shape it is easy to get wrong, not a guard 19# on a particular line -- making the table uncorrelated, the nearest sabotage, 20# does not change the outcome. 21# 22# Covers the same ground as corpus/x86_64-llvm-switch-rodata/ 23# clang-switch-rodata-assoc in Joe Lawrence's klp-build unit test corpus. 24 25. "$(dirname "$0")/../lib.sh" 26 27clang_only "only Clang emits switch jump tables in their own section" 28 29setup 30build_pair switch_rodata.c 31 32# The premise: this Clang really did build a table rather than a chain of 33# comparisons, and the added case really did change it. 34tbl=.rodata..Lswitch.table.status_to_string 35has_input_section orig.o "$tbl" || 36 probe_skip "this clang built no jump table for the switch" 37# readelf prefixes each line with "[nn]", which splits into one or two fields 38# depending on the index, so strip it before counting columns. 39tbl_size() 40{ 41 in_sections "$1" | sed 's/^ *\[[ 0-9]*\] *//' | 42 awk -v s="$tbl" '$1 == s { print $5 }' 43} 44[ "$(tbl_size orig.o)" != "$(tbl_size patched.o)" ] || 45 fail "fixture's added case did not change the jump table" 46 47run_diff 48 49assert_patched status_to_string 50assert_section "$tbl" 51assert_reloc_sym .text.status_to_string "$tbl" 52 53pass "Clang switch jump table carried with the function it belongs to" 54