xref: /linux/tools/objtool/tests/generic/fixtures/static_local_uncorrelated.c (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Static locals of three kinds, in one patched function.
4  *
5  * Most static locals must be correlated, so the patched code keeps using the
6  * running kernel's copy.  Two kinds must not:
7  *
8  *   - anything in .data..once, the flag behind WARN_ONCE and friends.  Sharing
9  *     it would mean a patch inherits "already warned" from before the patch.
10  *   - the well-known names the kernel generates for such things (__warned,
11  *     __key, __func__, ...), which are per-instance by nature.  gcc names them
12  *     <var>.<id> and Clang <func>.<var>, so both spellings have to be caught.
13  */
14 
15 static const char __modinfo[]
16 	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
17 
target(int x)18 int target(int x)
19 {
20 	/*
21 	 * A .data..once variable whose name is *not* on the list below, so
22 	 * only the section can disqualify it.  Naming it __warned would let
23 	 * the name rule catch it and the section rule go untested.
24 	 */
25 	static int once_flag __attribute__((section(".data..once")));
26 	/* a never-correlate name, in an ordinary section */
27 	static int __key;
28 	/* and one that must be correlated */
29 	static int ordinary;
30 
31 	if (!once_flag)
32 		once_flag = 1;
33 	__key += x;
34 	ordinary += x;
35 
36 #ifdef PATCHED
37 	return __key + ordinary + once_flag + 2;
38 #else
39 	return __key + ordinary + once_flag + 1;
40 #endif
41 }
42