xref: /linux/tools/objtool/tests/generic/test-sympos.sh (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# sympos is what livepatch uses to tell duplicate symbol names apart in the
5# patched object: which "dup_counter" of several the relocation means.  Get it
6# wrong and the patch resolves to the wrong object at load time, silently.
7#
8# klp_find_sympos() reports 0 when a name is unique and a 1-based position when
9# it is not, so both need checking -- always reporting a position, or never,
10# each looks right in one of the two cases.
11#
12# This is the module path, counting symbol table order.  vmlinux is reordered
13# by the final link and goes through .klp.symid instead; that needs a linked
14# vmlinux next to vmlinux.o and is not covered here.
15
16. "$(dirname "$0")/../lib.sh"
17
18setup
19
20# One copy: the name is unique, so there is nothing to disambiguate.
21build_one sympos_dup.c orig.o    -DFUNC_NAME=use_a
22build_one sympos_dup.c patched.o -DFUNC_NAME=use_a -DPATCHED
23run_diff
24
25assert_klp_sympos dup_counter 0
26
27# Two copies: positions, in symbol table order.
28for p in "" "-DPATCHED"; do
29	# shellcheck disable=SC2086
30	build_one sympos_dup.c "a$p.o" -DFUNC_NAME=use_a $p
31	# shellcheck disable=SC2086
32	build_one sympos_dup.c "b$p.o" -DFUNC_NAME=use_b -DNO_MODINFO $p
33done
34partial_link "$workdir/orig.o"    "$workdir/a.o"         "$workdir/b.o" ||
35	probe_skip "partial link unavailable"
36partial_link "$workdir/patched.o" "$workdir/a-DPATCHED.o" "$workdir/b-DPATCHED.o" ||
37	probe_skip "partial link unavailable"
38
39# Without duplicates in the input there is nothing for sympos to number.
40[ "$(count_input_symbols orig.o dup_counter)" = 2 ] ||
41	fail "fixture did not produce two dup_counter symbols"
42
43run_diff
44
45assert_klp_sympos dup_counter 1
46assert_klp_sympos dup_counter 2
47# ... and nothing still claiming the name is unique
48out_symbols | grep -qE '\.klp\.sym\.[^.]+\.dup_counter,0([[:space:]]|$)' &&
49	fail "sympos 0 emitted for a duplicated symbol"
50
51pass "sympos numbers duplicate symbols and stays 0 for unique ones"
52