1cff27d90SPuranjay Mohan# SPDX-License-Identifier: GPL-2.0 2cff27d90SPuranjay Mohan# 3cff27d90SPuranjay Mohan# Helpers for the objtool klp tests. A test builds a fixture twice, as the 4cff27d90SPuranjay Mohan# original and (with -DPATCHED) the patched object, runs both through 5cff27d90SPuranjay Mohan# "klp checksum" and diffs them, then asserts on the result. 6cff27d90SPuranjay Mohan# 7cff27d90SPuranjay Mohan# Assertions check properties rather than compare against recorded output: 8cff27d90SPuranjay Mohan# codegen varies between compilers and golden files would report churn instead 9cff27d90SPuranjay Mohan# of regressions. 10cff27d90SPuranjay Mohan 11cff27d90SPuranjay MohanTESTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 12d76312b8SSong Liu 13d76312b8SSong Liu# Tests live in generic/ or in an architecture directory beside it, and each 14d76312b8SSong Liu# carries its own fixtures. 15d76312b8SSong LiuFIXTURES_DIR="$(cd "$(dirname "$0")/fixtures" 2>/dev/null && pwd)" 16cff27d90SPuranjay Mohan 17c009fabcSSong Liu# The kernel's convention: CROSS_COMPILE is the one knob, with per-tool 18c009fabcSSong Liu# overrides for what it does not cover. objtool itself is always a host binary 19c009fabcSSong Liu# -- it is built with HOSTCC and only reads ELF -- so an arm64 machine can run 20c009fabcSSong Liu# the x86 tests against x86 objects given a compiler that emits them. 21c009fabcSSong Liu# 22c009fabcSSong Liu# readelf reads any target, so it rarely needs overriding, and either GNU 23c009fabcSSong Liu# readelf or llvm-readelf will do: the assertions match on fields rather than 24c009fabcSSong Liu# on columns, and where the two spell something differently -- "OS [0xff20]" 25c009fabcSSong Liu# against "OS[0xff20]" for SHN_LIVEPATCH -- they accept both. BFD's objcopy is 26c009fabcSSong Liu# usually built for the host's target alone, and llvm-objcopy is the 27c009fabcSSong Liu# target-agnostic replacement. 28c009fabcSSong LiuCROSS_COMPILE="${CROSS_COMPILE:-}" 29c009fabcSSong LiuCC="${CC:-${CROSS_COMPILE}gcc}" 30c009fabcSSong LiuLD="${LD:-${CROSS_COMPILE}ld}" 31c009fabcSSong LiuREADELF="${READELF:-${CROSS_COMPILE}readelf}" 32c009fabcSSong LiuOBJCOPY="${OBJCOPY:-${CROSS_COMPILE}objcopy}" 33c009fabcSSong Liu 34cff27d90SPuranjay MohanOBJTOOL="${OBJTOOL:-$TESTS_DIR/../objtool}" 35c009fabcSSong Liu 36c009fabcSSong Liu# klp_preflight 37c009fabcSSong Liu# 38c009fabcSSong Liu# Check the environment once, before any test runs, and report what was found. 39c009fabcSSong Liu# 40c009fabcSSong Liuklp_preflight() 41c009fabcSSong Liu{ 42d76312b8SSong Liu local tmp tool cc_version arch host cc_arch 43c009fabcSSong Liu 44c009fabcSSong Liu bail() { echo "Bail out! $*" >&2; exit 1; } 45c009fabcSSong Liu 46c009fabcSSong Liu # A relative $OBJTOOL is relative to the objtool directory, not tests/. 47c009fabcSSong Liu [ -x "$OBJTOOL" ] || [ ! -x "$TESTS_DIR/../$OBJTOOL" ] || 48c009fabcSSong Liu OBJTOOL="$TESTS_DIR/../$OBJTOOL" 49c009fabcSSong Liu 50c009fabcSSong Liu [ -x "$OBJTOOL" ] || 51c009fabcSSong Liu bail "objtool not found at '$OBJTOOL' -- build it first" 52c009fabcSSong Liu 53c009fabcSSong Liu # run_diff() runs objtool from inside the test's working directory, so 54c009fabcSSong Liu # a relative path would resolve against that instead. 55c009fabcSSong Liu OBJTOOL="$(realpath "$OBJTOOL")" 56c009fabcSSong Liu 57c009fabcSSong Liu "$OBJTOOL" klp 2>&1 | grep -q checksum || 58c009fabcSSong Liu bail "objtool was built without klp support; install libxxhash (>= 0.8) and rebuild" 59c009fabcSSong Liu 60c009fabcSSong Liu command -v "${CC%% *}" >/dev/null || bail "compiler not found: $CC" 61c009fabcSSong Liu 62c009fabcSSong Liu for tool in "$READELF" "$OBJCOPY" "$LD"; do 63c009fabcSSong Liu command -v "${tool%% *}" >/dev/null || bail "$tool not found" 64c009fabcSSong Liu done 65c009fabcSSong Liu 66c009fabcSSong Liu tmp="$(mktemp -d)" || bail "mktemp failed" 67c009fabcSSong Liu echo 'int probe(void) { return 0; }' > "$tmp/probe.c" 68c009fabcSSong Liu $CC -c -o "$tmp/probe.o" "$tmp/probe.c" 2>/dev/null || 69c009fabcSSong Liu { rm -rf "$tmp"; bail "$CC cannot compile a trivial object"; } 70c009fabcSSong Liu 71c009fabcSSong Liu # $CC, $ARCH and objtool have to agree about the target, and cross runs 72c009fabcSSong Liu # are where they stop agreeing: plain "CC=clang ARCH=x86_64" on an arm64 73c009fabcSSong Liu # box selects the x86 tests and then builds arm64 objects, because clang 74c009fabcSSong Liu # needs --target= to emit anything but the host's. 75c009fabcSSong Liu # 76c009fabcSSong Liu # Ask objtool rather than comparing machine names. It rejects an object 77c009fabcSSong Liu # it was not built for -- "unexpected ELF machine type" -- so one check 78c009fabcSSong Liu # covers every way the three can disagree, and says so once instead of 79c009fabcSSong Liu # failing every test for the same reason. 80c009fabcSSong Liu "$OBJTOOL" klp checksum "$tmp/probe.o" >/dev/null 2>&1 || 81c009fabcSSong Liu { rm -rf "$tmp" 82c009fabcSSong Liu bail "objtool rejects an object built by '$CC'; they target" \ 83c009fabcSSong Liu "different architectures (set CROSS_COMPILE, or" \ 84c009fabcSSong Liu "--target= for clang)"; } 85c009fabcSSong Liu 86c009fabcSSong Liu # BFD objcopy is usually built for the host's target alone, and 87c009fabcSSong Liu # checksum_of() needs it to read the object under test. 88c009fabcSSong Liu $OBJCOPY -O binary --only-section=.text "$tmp/probe.o" "$tmp/probe.bin" 2>/dev/null || 89c009fabcSSong Liu { rm -rf "$tmp" 90c009fabcSSong Liu bail "$OBJCOPY cannot read objects built by '$CC'; install" \ 91c009fabcSSong Liu "binutils-multiarch or set OBJCOPY=llvm-objcopy"; } 92c009fabcSSong Liu # $ARCH only chooses which directory of tests runs, so it can disagree 93c009fabcSSong Liu # with what $CC builds without objtool noticing -- and the result is the 94c009fabcSSong Liu # wrong set of tests, quietly. 95c009fabcSSong Liu case "$($READELF -hW "$tmp/probe.o" | sed -n 's/.*Machine: *//p')" in 96c009fabcSSong Liu *X86-64*|*Intel*80386*) cc_arch=x86 ;; 97c009fabcSSong Liu *AArch64*) cc_arch=arm64 ;; 98c009fabcSSong Liu *) cc_arch= ;; 99c009fabcSSong Liu esac 100c009fabcSSong Liu rm -rf "$tmp" 101c009fabcSSong Liu 102d76312b8SSong Liu # Normalize to the kernel's SRCARCH. 103d76312b8SSong Liu case "${ARCH:-$(uname -m)}" in 104d76312b8SSong Liu x86_64|i?86) arch=x86 ;; 105d76312b8SSong Liu aarch64*) arch=arm64 ;; 106d76312b8SSong Liu *) arch="${ARCH:-$(uname -m)}" ;; 107d76312b8SSong Liu esac 108d76312b8SSong Liu 109d76312b8SSong Liu case "$(uname -m)" in 110d76312b8SSong Liu x86_64|i?86) host=x86 ;; 111d76312b8SSong Liu aarch64*) host=arm64 ;; 112d76312b8SSong Liu *) host="$(uname -m)" ;; 113d76312b8SSong Liu esac 114d76312b8SSong Liu 115d76312b8SSong Liu [ -z "$cc_arch" ] || [ "$cc_arch" = "$arch" ] || 116d76312b8SSong Liu bail "ARCH says $arch but '$CC' builds $cc_arch objects;" \ 117d76312b8SSong Liu "the $arch tests would run against the wrong architecture" 118d76312b8SSong Liu 119d76312b8SSong Liu KLP_TEST_ARCH="$arch" 120c009fabcSSong Liu KLP_TEST_PREFLIGHT=done 121d76312b8SSong Liu export OBJTOOL CC KLP_TEST_ARCH KLP_TEST_PREFLIGHT 122c009fabcSSong Liu 123c009fabcSSong Liu cc_version="$($CC --version 2>/dev/null | head -1)" 124c009fabcSSong Liu cat <<EOF 125c009fabcSSong Liu# preflight 126c009fabcSSong Liu# objtool $OBJTOOL (klp: yes) 127c009fabcSSong Liu# compiler $cc_version 128c009fabcSSong Liu# arch $KLP_TEST_ARCH$([ "$arch" = "$host" ] || echo " (host $host, cross)") 129c009fabcSSong LiuEOF 130c009fabcSSong Liu} 131c009fabcSSong Liu 132c009fabcSSong Liu[ -n "${KLP_TEST_PREFLIGHT:-}" ] || klp_preflight 133cff27d90SPuranjay Mohan 134*ae04b6d0SSong Liu# What every fixture is built with. These describe the kernel a fixture stands 135*ae04b6d0SSong Liu# in for; -c is build_one's contract rather than a property of that kernel, so 136*ae04b6d0SSong Liu# it lives at the compile where an override cannot drop it. 137*ae04b6d0SSong Liu# 138*ae04b6d0SSong Liu# -O2 the kernel's default 139*ae04b6d0SSong Liu# -ffunction-sections -fdata-sections klp-build passes these itself, through 140*ae04b6d0SSong Liu# KCFLAGS, whatever the configuration 141*ae04b6d0SSong Liu# -fno-asynchronous-unwind-tables arch/x86/Makefile sets this always, so 142*ae04b6d0SSong Liu# kernel objects carry no .eh_frame 143*ae04b6d0SSong Liu# -fno-common the kernel's Makefile sets it, so an 144*ae04b6d0SSong Liu# uninitialised global there lands in 145*ae04b6d0SSong Liu# .bss rather than being SHN_COMMON, 146*ae04b6d0SSong Liu# which has no section and so no 147*ae04b6d0SSong Liu# checksum 148*ae04b6d0SSong Liu# 149*ae04b6d0SSong Liu# A test overrides it; see tools/objtool/Documentation/klp-write-tests.txt. 150*ae04b6d0SSong LiuFIXTURE_CFLAGS="-O2 -ffunction-sections -fdata-sections -fno-common \ 151*ae04b6d0SSong Liu -fno-asynchronous-unwind-tables" 152cff27d90SPuranjay Mohan 153cff27d90SPuranjay Mohantest_name="$(basename "$0" .sh)" 154cff27d90SPuranjay Mohanworkdir= 155cff27d90SPuranjay Mohan 156*ae04b6d0SSong Liu# The pair run_diff() and the checksum helpers work on. build_pair() names 157*ae04b6d0SSong Liu# them again and make_vmlinux_pair() repoints orig_obj at the image it links, 158*ae04b6d0SSong Liu# but a test which builds its objects itself with build_one() sets neither, so 159*ae04b6d0SSong Liu# the default belongs here. 160*ae04b6d0SSong Liuorig_obj=orig.o 161*ae04b6d0SSong Liupatched_obj=patched.o 162*ae04b6d0SSong Liu 163cff27d90SPuranjay Mohanpass() { echo "ok - $test_name${1:+: $1}"; exit 0; } 164317cec90SSong Liufail() { echo "not ok - $test_name: $1"; exit 1; } 165317cec90SSong Liu 166317cec90SSong Liu# Two kinds of skip, and the runner tells them apart. 167317cec90SSong Liu# 168317cec90SSong Liu# declared_skip the test said in advance it does not apply here, e.g. 169317cec90SSong Liu# gcc_only on a clang run. Expected indefinitely. 170317cec90SSong Liu# probe_skip the construct did not turn up in the built object this 171317cec90SSong Liu# time. Weaker: it may appear on another compiler version, 172317cec90SSong Liu# and one which becomes permanent is a fixture that quietly 173317cec90SSong Liu# stopped testing anything. 174317cec90SSong Liu# 175317cec90SSong Liu# A bare skip() is neither, and the runner counts it as a failure: a test which 176317cec90SSong Liu# gives up for a reason it never declared is a hole, not an outcome. 177317cec90SSong Liudeclared_skip() { echo "ok - $test_name # SKIP (declared) $*"; exit 0; } 178317cec90SSong Liuprobe_skip() { echo "ok - $test_name # SKIP (probe) $*"; exit 0; } 179317cec90SSong Liuskip() { echo "ok - $test_name # SKIP $*"; exit 0; } 180317cec90SSong Liu 181317cec90SSong Liu# TAP directives. A test which is known to fail reports it rather than being 182317cec90SSong Liu# commented out and forgotten, and one which starts passing again says so 183317cec90SSong Liu# instead of quietly going green: the expectation has to be removed by hand, 184317cec90SSong Liu# which is the point. 185317cec90SSong Liuxfail() 186317cec90SSong Liu{ 187317cec90SSong Liu echo "not ok - $test_name${1:+: $1} # TODO known failure" 188317cec90SSong Liu exit 0 189317cec90SSong Liu} 190317cec90SSong Liu 191317cec90SSong Liuxpass() 192317cec90SSong Liu{ 193317cec90SSong Liu echo "ok - $test_name${1:+: $1} # TODO expected failure, but passed" 194317cec90SSong Liu exit 1 195317cec90SSong Liu} 196cff27d90SPuranjay Mohan 197cff27d90SPuranjay Mohancleanup() { [ -n "$workdir" ] && rm -rf "$workdir"; } 198cff27d90SPuranjay Mohan 199cff27d90SPuranjay Mohan# setup [exported symbol...] 200cff27d90SPuranjay Mohansetup() 201cff27d90SPuranjay Mohan{ 202c009fabcSSong Liu # The environment was checked once when this file was sourced, so there 203c009fabcSSong Liu # is nothing to verify here: objtool exists at the resolved path, has 204c009fabcSSong Liu # klp support, and $CC works. 205cff27d90SPuranjay Mohan workdir="$(mktemp -d)" || fail "mktemp failed" 206cff27d90SPuranjay Mohan trap cleanup EXIT 207cff27d90SPuranjay Mohan 208cff27d90SPuranjay Mohan export_syms "$@" 209cff27d90SPuranjay Mohan} 210cff27d90SPuranjay Mohan 211cff27d90SPuranjay Mohan# export_syms [symbol...] 212cff27d90SPuranjay Mohan# 213cff27d90SPuranjay Mohan# Rewrite Module.symvers so exactly these symbols are exported by vmlinux. 214cff27d90SPuranjay Mohan# Whether a symbol is listed decides between an ordinary relocation and a klp 215cff27d90SPuranjay Mohan# relocation, so tests flip it to cover both. 216cff27d90SPuranjay Mohanexport_syms() 217cff27d90SPuranjay Mohan{ 218cff27d90SPuranjay Mohan : > "$workdir/Module.symvers" 219cff27d90SPuranjay Mohan for sym in "$@"; do 220cff27d90SPuranjay Mohan printf '0x00000000\t%s\tvmlinux\tEXPORT_SYMBOL\t\n' \ 221cff27d90SPuranjay Mohan "$sym" >> "$workdir/Module.symvers" 222cff27d90SPuranjay Mohan done 223cff27d90SPuranjay Mohan} 224cff27d90SPuranjay Mohan 225317cec90SSong Liu# gcc_only / clang_only <reason> 226317cec90SSong Liugcc_only() 227317cec90SSong Liu{ 228317cec90SSong Liu case "$($CC --version 2>/dev/null | head -1)" in 229317cec90SSong Liu *[Gg][Cc][Cc]*) return 0 ;; 230317cec90SSong Liu esac 231317cec90SSong Liu declared_skip "gcc only${1:+: $1}" 232317cec90SSong Liu} 233317cec90SSong Liu 234317cec90SSong Liuclang_only() 235317cec90SSong Liu{ 236317cec90SSong Liu case "$($CC --version 2>/dev/null | head -1)" in 237317cec90SSong Liu *clang*) return 0 ;; 238317cec90SSong Liu esac 239317cec90SSong Liu declared_skip "clang only${1:+: $1}" 240317cec90SSong Liu} 241317cec90SSong Liu 242*ae04b6d0SSong Liu# build_one <fixture.c> <output object> [cflags...] 243*ae04b6d0SSong Liubuild_one() 244cff27d90SPuranjay Mohan{ 245*ae04b6d0SSong Liu local fixture out 246*ae04b6d0SSong Liu fixture="$FIXTURES_DIR/$1" 247*ae04b6d0SSong Liu out="$workdir/$2" 248*ae04b6d0SSong Liu shift 2 249cff27d90SPuranjay Mohan 250cff27d90SPuranjay Mohan [ -f "$fixture" ] || fail "missing fixture $fixture" 251cff27d90SPuranjay Mohan 252*ae04b6d0SSong Liu # run_checksum only runs once per workdir. A fresh object has no 253*ae04b6d0SSong Liu # checksums in it, so anything built now needs that to happen again. 254*ae04b6d0SSong Liu rm -f "$workdir/.checksummed" 255*ae04b6d0SSong Liu 256*ae04b6d0SSong Liu $CC -c $FIXTURE_CFLAGS "$@" -o "$out" "$fixture" 2>"$workdir/cc.log" || 257*ae04b6d0SSong Liu fail "$(basename "$fixture") does not build: $(tail -1 "$workdir/cc.log")" 258*ae04b6d0SSong Liu} 259*ae04b6d0SSong Liu 260*ae04b6d0SSong Liu# build_pair <fixture.c> [cflags...] 261*ae04b6d0SSong Liubuild_pair() 262*ae04b6d0SSong Liu{ 263*ae04b6d0SSong Liu local fixture="$1"; shift 264*ae04b6d0SSong Liu 265*ae04b6d0SSong Liu # Name what this builds. A test may run several segments, and 266*ae04b6d0SSong Liu # make_vmlinux_pair() repoints orig_obj at the image it links, so 267*ae04b6d0SSong Liu # without this the next run_diff() would still be reading that. 268*ae04b6d0SSong Liu orig_obj=orig.o 269*ae04b6d0SSong Liu patched_obj=patched.o 270*ae04b6d0SSong Liu 271*ae04b6d0SSong Liu build_one "$fixture" orig.o "$@" 272*ae04b6d0SSong Liu build_one "$fixture" patched.o "$@" -DPATCHED 273*ae04b6d0SSong Liu} 274*ae04b6d0SSong Liu 275*ae04b6d0SSong Liu# run_objtool_check <objtool arguments...> 276*ae04b6d0SSong Liu# 277*ae04b6d0SSong Liu# Run objtool's ordinary check pass over the pair, as the kernel build does. 278*ae04b6d0SSong Liu# 279*ae04b6d0SSong Liu# Some of what klp diff consumes is produced by this pass rather than by the 280*ae04b6d0SSong Liu# compiler: .static_call_sites, .mcount_loc, .ibt_endbr_seal, ORC. 281*ae04b6d0SSong Liu# 282*ae04b6d0SSong Liu# Only module objects see it before klp-build -- with CONFIG_KLP_BUILD the 283*ae04b6d0SSong Liu# per-object pass is deferred, so built-in objects reach klp diff exactly as 284*ae04b6d0SSong Liu# the compiler left them. 285*ae04b6d0SSong Liurun_objtool_check() 286*ae04b6d0SSong Liu{ 287*ae04b6d0SSong Liu local obj 288*ae04b6d0SSong Liu 289*ae04b6d0SSong Liu # This rewrites both objects, so checksums taken before it describe 290*ae04b6d0SSong Liu # something that no longer exists. As in build_one(), drop the marker 291*ae04b6d0SSong Liu # so run_checksum() takes them again. 292*ae04b6d0SSong Liu rm -f "$workdir/.checksummed" 293*ae04b6d0SSong Liu 294*ae04b6d0SSong Liu for obj in "$orig_obj" "$patched_obj"; do 295*ae04b6d0SSong Liu "$OBJTOOL" "$@" "$workdir/$obj" || 296*ae04b6d0SSong Liu fail "objtool $* failed on $obj" 297*ae04b6d0SSong Liu done 298*ae04b6d0SSong Liu} 299*ae04b6d0SSong Liu 300*ae04b6d0SSong Liurun_checksum() 301*ae04b6d0SSong Liu{ 302*ae04b6d0SSong Liu # Checksums live in the objects, and a test may ask for them more than 303*ae04b6d0SSong Liu # once -- diffing the same pair again with a different Module.symvers, 304*ae04b6d0SSong Liu # say. objtool does the right thing when asked twice, leaving the 305*ae04b6d0SSong Liu # object alone, but it says so, and that warning would be most of what 306*ae04b6d0SSong Liu # a passing run prints. Remember instead, and keep quiet. 307*ae04b6d0SSong Liu [ -e "$workdir/.checksummed" ] && return 0 308*ae04b6d0SSong Liu 309*ae04b6d0SSong Liu "$OBJTOOL" klp checksum "$workdir/$orig_obj" || 310*ae04b6d0SSong Liu fail "klp checksum $orig_obj failed" 311*ae04b6d0SSong Liu "$OBJTOOL" klp checksum "$workdir/$patched_obj" || 312*ae04b6d0SSong Liu fail "klp checksum $patched_obj failed" 313*ae04b6d0SSong Liu touch "$workdir/.checksummed" 314cff27d90SPuranjay Mohan} 315cff27d90SPuranjay Mohan 316cff27d90SPuranjay Mohan# run_diff [expected exit status] 317cff27d90SPuranjay Mohanrun_diff() 318cff27d90SPuranjay Mohan{ 319cff27d90SPuranjay Mohan local expect="${1:-0}" rc=0 320cff27d90SPuranjay Mohan 321*ae04b6d0SSong Liu run_checksum 322cff27d90SPuranjay Mohan 323cff27d90SPuranjay Mohan # klp diff looks for Module.symvers relative to the working directory. 324*ae04b6d0SSong Liu ( cd "$workdir" && "$OBJTOOL" klp diff "$orig_obj" "$patched_obj" out.o ) \ 325cff27d90SPuranjay Mohan > "$workdir/diff.log" 2>&1 || rc=$? 326cff27d90SPuranjay Mohan 327cff27d90SPuranjay Mohan [ "$rc" = "$expect" ] || 328cff27d90SPuranjay Mohan fail "klp diff exited $rc, expected $expect: $(tail -2 "$workdir/diff.log")" 329cff27d90SPuranjay Mohan} 330cff27d90SPuranjay Mohan 331cff27d90SPuranjay Mohancc_supports() 332cff27d90SPuranjay Mohan{ 333cff27d90SPuranjay Mohan echo 'int f(void) { return 0; }' > "$workdir/flagtest.c" 334cff27d90SPuranjay Mohan $CC $1 -c "$workdir/flagtest.c" -o "$workdir/flagtest.o" 2>/dev/null 335cff27d90SPuranjay Mohan} 336cff27d90SPuranjay Mohan 337cff27d90SPuranjay Mohan# partial_link <output> <object...> 338cff27d90SPuranjay Mohan# 339cff27d90SPuranjay Mohan# "ld -r" through the compiler driver so the link targets the same 340cff27d90SPuranjay Mohan# architecture as the objects. 341cff27d90SPuranjay Mohanpartial_link() 342cff27d90SPuranjay Mohan{ 343cff27d90SPuranjay Mohan local out="$1"; shift 344cff27d90SPuranjay Mohan 345*ae04b6d0SSong Liu rm -f "$workdir/.checksummed" 346*ae04b6d0SSong Liu 347cff27d90SPuranjay Mohan $CC -r -nostdlib -o "$out" "$@" 2>/dev/null || 348cff27d90SPuranjay Mohan $CC -r -nostdlib -fuse-ld=lld -o "$out" "$@" 2>/dev/null 349cff27d90SPuranjay Mohan} 350cff27d90SPuranjay Mohan 351*ae04b6d0SSong Liu# link_vmlinux <output> <object...> 352*ae04b6d0SSong Liu# 353*ae04b6d0SSong Liu# Link objects into an executable, the way the kernel's final link produces 354*ae04b6d0SSong Liu# vmlinux from vmlinux.o. Entry point 0 and no libc: nothing runs it, it only 355*ae04b6d0SSong Liu# has to be a linked image with resolved addresses. 356*ae04b6d0SSong Liu# 357*ae04b6d0SSong Liu# The sub-sections have to come out in name order rather than object order, 358*ae04b6d0SSong Liu# the way the kernel's linker script gathers .text.unlikely and .data.. apart 359*ae04b6d0SSong Liu# from the rest. That reordering is the entire reason .klp.symid exists: a 360*ae04b6d0SSong Liu# link which preserves order cannot tell a correct sympos from one that merely 361*ae04b6d0SSong Liu# counted, and the caller checks the two orders really did diverge. 362*ae04b6d0SSong Liu# 363*ae04b6d0SSong Liu# A linker script rather than --sort-section=name, because lld accepts that 364*ae04b6d0SSong Liu# option and ignores it -- so on a host where only lld can link the target, the 365*ae04b6d0SSong Liu# test would quietly stop testing the thing it is named for. 366*ae04b6d0SSong Liu# 367*ae04b6d0SSong Liu# Three attempts because a cross run has neither $LD nor the compiler's default 368*ae04b6d0SSong Liu# linker able to touch the target: on an arm64 host linking x86 objects, only 369*ae04b6d0SSong Liu# lld will do it. 370*ae04b6d0SSong Liulink_vmlinux() 371*ae04b6d0SSong Liu{ 372*ae04b6d0SSong Liu local out="$1" lds="$workdir/sort.lds"; shift 373*ae04b6d0SSong Liu 374*ae04b6d0SSong Liu echo 'SECTIONS { .data : { *(SORT_BY_NAME(.data.*)) } }' > "$lds" 375*ae04b6d0SSong Liu 376*ae04b6d0SSong Liu $LD -e 0 -T "$lds" -o "$out" "$@" 2>/dev/null || 377*ae04b6d0SSong Liu $CC -nostdlib -Wl,-e,0 -Wl,-T,"$lds" \ 378*ae04b6d0SSong Liu -o "$out" "$@" 2>/dev/null || 379*ae04b6d0SSong Liu $CC -nostdlib -fuse-ld=lld -Wl,-e,0 -Wl,-T,"$lds" \ 380*ae04b6d0SSong Liu -o "$out" "$@" 2>/dev/null 381*ae04b6d0SSong Liu} 382*ae04b6d0SSong Liu 383*ae04b6d0SSong Liu# make_vmlinux_pair <orig object...> -- <patched object...> 384*ae04b6d0SSong Liu# 385*ae04b6d0SSong Liu# Build the vmlinux.o / vmlinux pair klp diff needs to resolve sympos the way 386*ae04b6d0SSong Liu# it does for built-in code, and point the diff at it. 387*ae04b6d0SSong Liu# 388*ae04b6d0SSong Liu# For a module, sympos is a count in symbol table order, which klp diff can do 389*ae04b6d0SSong Liu# from the object alone. vmlinux is different: the final link reorders 390*ae04b6d0SSong Liu# sub-sections, so the position comes from the linked image, bridged by 391*ae04b6d0SSong Liu# .klp.symid. klp diff only looks for that when the object it was handed is 392*ae04b6d0SSong Liu# called vmlinux.o and a vmlinux sits beside it -- so both the name and the 393*ae04b6d0SSong Liu# linked image matter. 394*ae04b6d0SSong Liumake_vmlinux_pair() 395*ae04b6d0SSong Liu{ 396*ae04b6d0SSong Liu local orig=() patched=() seen= arg 397*ae04b6d0SSong Liu 398*ae04b6d0SSong Liu for arg in "$@"; do 399*ae04b6d0SSong Liu if [ "$arg" = -- ]; then seen=y; continue; fi 400*ae04b6d0SSong Liu if [ -n "$seen" ]; then patched+=( "$arg" ); else orig+=( "$arg" ); fi 401*ae04b6d0SSong Liu done 402*ae04b6d0SSong Liu 403*ae04b6d0SSong Liu # Both sides have to have been named. Without this, forgetting the -- 404*ae04b6d0SSong Liu # leaves one list empty, the link of nothing fails, and the test skips 405*ae04b6d0SSong Liu # saying the toolchain cannot link -- which is a test bug wearing the 406*ae04b6d0SSong Liu # costume of an environment one. 407*ae04b6d0SSong Liu [ "${#orig[@]}" -gt 0 ] && [ "${#patched[@]}" -gt 0 ] || 408*ae04b6d0SSong Liu fail "make_vmlinux_pair needs objects either side of --" 409*ae04b6d0SSong Liu 410*ae04b6d0SSong Liu partial_link "$workdir/vmlinux.o" "${orig[@]}" || 411*ae04b6d0SSong Liu probe_skip "partial link unavailable" 412*ae04b6d0SSong Liu partial_link "$workdir/patched.o" "${patched[@]}" || 413*ae04b6d0SSong Liu probe_skip "partial link unavailable" 414*ae04b6d0SSong Liu 415*ae04b6d0SSong Liu "$OBJTOOL" --klp-symids --link "$workdir/vmlinux.o" || 416*ae04b6d0SSong Liu fail "objtool --klp-symids failed" 417*ae04b6d0SSong Liu 418*ae04b6d0SSong Liu link_vmlinux "$workdir/vmlinux" "$workdir/vmlinux.o" || 419*ae04b6d0SSong Liu probe_skip "cannot link a vmlinux here" 420*ae04b6d0SSong Liu 421*ae04b6d0SSong Liu orig_obj=vmlinux.o 422*ae04b6d0SSong Liu} 423*ae04b6d0SSong Liu 424*ae04b6d0SSong Liu# build_module_pair <fixture.c> <module name> [cflags...] 425*ae04b6d0SSong Liu# 426*ae04b6d0SSong Liu# Build the pair as objects belonging to a module rather than to vmlinux. klp 427*ae04b6d0SSong Liu# diff reads the object's module name from .modinfo, and that decides which 428*ae04b6d0SSong Liu# object a relocation is attributed to and whether a reference counts as 429*ae04b6d0SSong Liu# cross-module, so a good deal of the code has a module path the vmlinux 430*ae04b6d0SSong Liu# fixtures never reach. 431*ae04b6d0SSong Liu# 432*ae04b6d0SSong Liu# The fixture defines its .modinfo name from MODNAME. Passing that through 433*ae04b6d0SSong Liu# -D needs two levels of quoting, which is easy to get wrong at the call site. 434*ae04b6d0SSong Liubuild_module_pair() 435*ae04b6d0SSong Liu{ 436*ae04b6d0SSong Liu local fixture="$1" modname="$2"; shift 2 437*ae04b6d0SSong Liu 438*ae04b6d0SSong Liu build_pair "$fixture" -DMODNAME="\"$modname\"" "$@" 439*ae04b6d0SSong Liu} 440*ae04b6d0SSong Liu 441cff27d90SPuranjay Mohan# find_thinlto_toolchain 442cff27d90SPuranjay Mohan# 443317cec90SSong Liu# Set $THIN_LD to an lld from the same LLVM release as $CC (or THIN_CC). A 444cff27d90SPuranjay Mohan# mismatched pair fails with "Invalid summary version", which reads like a 445cff27d90SPuranjay Mohan# broken test rather than a broken environment. 446317cec90SSong Liu# 447317cec90SSong Liu# ThinLTO is clang-only; callers must use clang_only before calling this. 448317cec90SSong Liu# Only $CC (or an explicit THIN_CC override) is consulted -- the harness does 449317cec90SSong Liu# not search for a second compiler beside a gcc $CC. 450cff27d90SPuranjay Mohanfind_thinlto_toolchain() 451cff27d90SPuranjay Mohan{ 452317cec90SSong Liu local cc ver ld 453cff27d90SPuranjay Mohan 454317cec90SSong Liu for cc in "${THIN_CC:-}" "$CC"; do 455cff27d90SPuranjay Mohan [ -n "$cc" ] || continue 456317cec90SSong Liu command -v "${cc%% *}" >/dev/null 2>&1 || return 1 457cff27d90SPuranjay Mohan 458cff27d90SPuranjay Mohan ver=$($cc -dumpversion 2>/dev/null | cut -d. -f1) 459cff27d90SPuranjay Mohan 460cff27d90SPuranjay Mohan for ld in "${THIN_LD:-}" "ld.lld-$ver" ld.lld; do 461cff27d90SPuranjay Mohan [ -n "$ld" ] || continue 462cff27d90SPuranjay Mohan command -v "$ld" >/dev/null 2>&1 || continue 463cff27d90SPuranjay Mohan 464cff27d90SPuranjay Mohan echo 'int probe(void) { return 0; }' > "$workdir/probe.c" 465cff27d90SPuranjay Mohan $cc -flto=thin -O2 -c "$workdir/probe.c" \ 466cff27d90SPuranjay Mohan -o "$workdir/probe.o" 2>/dev/null || continue 467cff27d90SPuranjay Mohan "$ld" -r "$workdir/probe.o" -o "$workdir/probe.elf" \ 468cff27d90SPuranjay Mohan 2>/dev/null || continue 469cff27d90SPuranjay Mohan 470cff27d90SPuranjay Mohan THIN_CC="$cc" 471cff27d90SPuranjay Mohan THIN_LD="$ld" 472cff27d90SPuranjay Mohan return 0 473cff27d90SPuranjay Mohan done 474cff27d90SPuranjay Mohan done 475cff27d90SPuranjay Mohan 476cff27d90SPuranjay Mohan return 1 477cff27d90SPuranjay Mohan} 478cff27d90SPuranjay Mohan 479c009fabcSSong Liuout_sections() { $READELF -S -W "$workdir/out.o" 2>/dev/null; } 480c009fabcSSong Liuout_relocs() { $READELF -r -W "$workdir/out.o" 2>/dev/null; } 481c009fabcSSong Liuout_symbols() { $READELF -s -W "$workdir/out.o" 2>/dev/null; } 482cff27d90SPuranjay Mohandiff_log() { cat "$workdir/diff.log"; } 483cff27d90SPuranjay Mohan 484cff27d90SPuranjay Mohanassert_section() 485cff27d90SPuranjay Mohan{ 486cff27d90SPuranjay Mohan out_sections | grep -q "[[:space:]]$1[[:space:]]" || 487cff27d90SPuranjay Mohan fail "expected section '$1' in output" 488cff27d90SPuranjay Mohan} 489cff27d90SPuranjay Mohan 490cff27d90SPuranjay Mohanassert_patched() 491cff27d90SPuranjay Mohan{ 492cff27d90SPuranjay Mohan assert_section ".text.$1" 493cff27d90SPuranjay Mohan} 494cff27d90SPuranjay Mohan 495cff27d90SPuranjay Mohanassert_not_patched() 496cff27d90SPuranjay Mohan{ 497cff27d90SPuranjay Mohan out_sections | grep -q "[[:space:]].text.$1[[:space:]]" && 498cff27d90SPuranjay Mohan fail "function '$1' should not have been cloned" 499cff27d90SPuranjay Mohan return 0 500cff27d90SPuranjay Mohan} 501