xref: /linux/tools/objtool/tests/generic/test-sympos-vmlinux.sh (revision 1a50e18ba3427ce9d22c5ff28499cca242fa7f73)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# sympos for vmlinux, which is resolved differently from sympos for a module.
5#
6# A module's .ko preserves symbol table order, so klp diff can count -- that is
7# what test-sympos covers.  vmlinux cannot be counted: the final link reorders
8# sub-sections, so the order in vmlinux.o is not the order the running kernel
9# has.  klp diff bridges that with .klp.symid, a table of { id, address }
10# emitted into vmlinux.o whose addresses the linker resolves, read back out of
11# the linked vmlinux.
12#
13# Getting it wrong points the relocation at a different symbol of the same
14# name.  Nothing fails to build or load; the patched code uses the wrong
15# object.
16#
17# The fixture is arranged so the two answers differ: the static that comes
18# first in the symbol table is placed at the *higher* address, so counting
19# gives 1 and reading the linked image gives 2.  Without that, both paths agree
20# and the test cannot tell them apart.
21
22. "$(dirname "$0")/../lib.sh"
23
24setup
25
26# use_a's static sorts last by section name, use_b's first.  Only use_a is
27# patched, so exactly one sympos comes out.
28build_one sympos_vmlinux.c orig_a.o    -DFUNC_NAME=use_a -DVARSEC='".data.zzz"'
29build_one sympos_vmlinux.c patched_a.o -DFUNC_NAME=use_a -DVARSEC='".data.zzz"' -DPATCHED
30build_one sympos_vmlinux.c b.o         -DFUNC_NAME=use_b -DVARSEC='".data.aaa"' -DNO_MODINFO
31
32make_vmlinux_pair "$workdir/orig_a.o" "$workdir/b.o" \
33	       -- "$workdir/patched_a.o" "$workdir/b.o"
34
35[ "$(count_input_symbols vmlinux.o dup_counter)" = 2 ] ||
36	fail "fixture did not produce two dup_counter symbols"
37has_input_section vmlinux.o .klp.symid ||
38	fail "objtool --klp-symids emitted no .klp.symid table"
39has_input_section vmlinux .klp.symid ||
40	fail ".klp.symid did not survive the link"
41
42# The premise: symbol table order and address order must disagree, or the test
43# proves nothing.
44first_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2; exit }')"
45low_addr="$(in_symbols vmlinux | awk '$8 == "dup_counter" { print $2 }' | sort | head -1)"
46[ "$first_addr" != "$low_addr" ] ||
47	probe_skip "linker did not reorder the two statics"
48
49assert_input_symbol dup_counter
50run_diff
51
52# Address order says 2.  Counting symbol table order would say 1.
53assert_klp_sympos dup_counter 2
54out_symbols | grep -q 'dup_counter,1' &&
55	fail "sympos 1 emitted: counted symbol table order instead of reading the linked vmlinux"
56
57pass "vmlinux sympos taken from the linked image, not from symbol table order"
58