xref: /linux/tools/objtool/tests/generic/test-checksum-position.sh (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# A function's checksum must not depend on where the function sits.
5#
6# A jump or call without a relocation encodes its target as an offset from the
7# instruction.  Hashing those bytes makes the checksum change whenever anything
8# ahead of the function changes size -- so an unrelated edit elsewhere in the
9# file reports this function as changed too, and the patch grows to include it
10# and everything it references.  Nothing fails; the livepatch is just larger and
11# riskier than the patch it came from.
12#
13# Here the "patch" moves target() away from callee() and changes nothing else:
14# both are aligned to 64 in the patched build, which shifts them apart without
15# touching a byte of either.  See the fixture for why it is done that way.
16
17. "$(dirname "$0")/../lib.sh"
18
19setup
20
21# -fno-function-sections, or each function is at offset 0 of its own section
22# and target() never moves.
23build_pair checksum_position.c -fno-function-sections
24
25assert_input_symbol target
26
27# The fixture is only meaningful if the displacement target's calls encode
28# actually changed, and that is the distance to callee() -- not target's own
29# offset.  A compiler which shifted the two by the same amount would move
30# target and leave the distance alone, and then the bytes are identical and
31# the checksum matches for the uninteresting reason.  Ask about the distance.
32sym_off()   # $1 object, $2 symbol
33{
34	in_symbols "$1" | awk -v n="$2" '$NF == n { print $2; exit }'
35}
36
37orig_t="$(sym_off orig.o target)";    orig_c="$(sym_off orig.o callee)"
38new_t="$(sym_off patched.o target)";  new_c="$(sym_off patched.o callee)"
39
40[ -n "$orig_t" ] && [ -n "$orig_c" ] && [ -n "$new_t" ] && [ -n "$new_c" ] ||
41	fail "target or callee missing from one of the objects"
42
43orig_gap=$(( 16#$orig_t - 16#$orig_c ))
44new_gap=$((  16#$new_t  - 16#$new_c ))
45[ "$orig_gap" != "$new_gap" ] ||
46	probe_skip "this compiler kept target() and callee() the same distance" \
47		   "apart; the call displacement did not change"
48
49assert_checksum_matches target
50
51pass "checksum unchanged when the function only moves"
52