xref: /linux-6.15/include/linux/objtool.h (revision 4708ea14)
100089c04SJulien Thierry /* SPDX-License-Identifier: GPL-2.0 */
200089c04SJulien Thierry #ifndef _LINUX_OBJTOOL_H
300089c04SJulien Thierry #define _LINUX_OBJTOOL_H
400089c04SJulien Thierry 
5f7515d9fSJosh Poimboeuf #include <linux/objtool_types.h>
6ee819aedSJulien Thierry 
703f16cd0SJosh Poimboeuf #ifdef CONFIG_OBJTOOL
85567c6c3SJulien Thierry 
9e2ef1158SPeter Zijlstra #include <asm/asm.h>
10e2ef1158SPeter Zijlstra 
115567c6c3SJulien Thierry #ifndef __ASSEMBLY__
12ee819aedSJulien Thierry 
13d88ebba4SJosh Poimboeuf #define UNWIND_HINT(type, sp_reg, sp_offset, signal, end)	\
14ee819aedSJulien Thierry 	"987: \n\t"						\
15ee819aedSJulien Thierry 	".pushsection .discard.unwind_hints\n\t"		\
16ee819aedSJulien Thierry 	/* struct unwind_hint */				\
17ee819aedSJulien Thierry 	".long 987b - .\n\t"					\
18ee819aedSJulien Thierry 	".short " __stringify(sp_offset) "\n\t"			\
19ee819aedSJulien Thierry 	".byte " __stringify(sp_reg) "\n\t"			\
20ee819aedSJulien Thierry 	".byte " __stringify(type) "\n\t"			\
21ffb1b4a4SJosh Poimboeuf 	".byte " __stringify(signal) "\n\t"			\
22ee819aedSJulien Thierry 	".byte " __stringify(end) "\n\t"			\
23ee819aedSJulien Thierry 	".balign 4 \n\t"					\
24ee819aedSJulien Thierry 	".popsection\n\t"
25ee819aedSJulien Thierry 
2600089c04SJulien Thierry /*
2700089c04SJulien Thierry  * This macro marks the given function's stack frame as "non-standard", which
2800089c04SJulien Thierry  * tells objtool to ignore the function when doing stack metadata validation.
2900089c04SJulien Thierry  * It should only be used in special cases where you're 100% sure it won't
3000089c04SJulien Thierry  * affect the reliability of frame pointers and kernel stack traces.
3100089c04SJulien Thierry  *
32d6a21f2dSMauro Carvalho Chehab  * For more information, see tools/objtool/Documentation/objtool.txt.
3300089c04SJulien Thierry  */
3400089c04SJulien Thierry #define STACK_FRAME_NON_STANDARD(func) \
3533def849SJoe Perches 	static void __used __section(".discard.func_stack_frame_non_standard") \
3600089c04SJulien Thierry 		*__func_stack_frame_non_standard_##func = func
3700089c04SJulien Thierry 
38e028c4f7SJosh Poimboeuf /*
39e028c4f7SJosh Poimboeuf  * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore
40e028c4f7SJosh Poimboeuf  * for the case where a function is intentionally missing frame pointer setup,
41e028c4f7SJosh Poimboeuf  * but otherwise needs objtool/ORC coverage when frame pointers are disabled.
42e028c4f7SJosh Poimboeuf  */
43e028c4f7SJosh Poimboeuf #ifdef CONFIG_FRAME_POINTER
44e028c4f7SJosh Poimboeuf #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func)
45e028c4f7SJosh Poimboeuf #else
46e028c4f7SJosh Poimboeuf #define STACK_FRAME_NON_STANDARD_FP(func)
47e028c4f7SJosh Poimboeuf #endif
48e028c4f7SJosh Poimboeuf 
49c8c301abSPeter Zijlstra #define ANNOTATE_NOENDBR					\
50c8c301abSPeter Zijlstra 	"986: \n\t"						\
51c8c301abSPeter Zijlstra 	".pushsection .discard.noendbr\n\t"			\
521c0c1fafSJosh Poimboeuf 	".long 986b - .\n\t"					\
53c8c301abSPeter Zijlstra 	".popsection\n\t"
54c8c301abSPeter Zijlstra 
55dca5da2aSPeter Zijlstra #define ASM_REACHABLE							\
56dca5da2aSPeter Zijlstra 	"998:\n\t"							\
57dca5da2aSPeter Zijlstra 	".pushsection .discard.reachable\n\t"				\
58dca5da2aSPeter Zijlstra 	".long 998b - .\n\t"						\
59dca5da2aSPeter Zijlstra 	".popsection\n\t"
60dca5da2aSPeter Zijlstra 
615567c6c3SJulien Thierry #else /* __ASSEMBLY__ */
625567c6c3SJulien Thierry 
6300089c04SJulien Thierry /*
6400089c04SJulien Thierry  * This macro indicates that the following intra-function call is valid.
6500089c04SJulien Thierry  * Any non-annotated intra-function call will cause objtool to issue a warning.
6600089c04SJulien Thierry  */
6700089c04SJulien Thierry #define ANNOTATE_INTRA_FUNCTION_CALL				\
6800089c04SJulien Thierry 	999:							\
6900089c04SJulien Thierry 	.pushsection .discard.intra_function_calls;		\
701c0c1fafSJosh Poimboeuf 	.long 999b - .;						\
7100089c04SJulien Thierry 	.popsection;
7200089c04SJulien Thierry 
73ee819aedSJulien Thierry /*
74ee819aedSJulien Thierry  * In asm, there are two kinds of code: normal C-type callable functions and
75ee819aedSJulien Thierry  * the rest.  The normal callable functions can be called by other code, and
76ee819aedSJulien Thierry  * don't do anything unusual with the stack.  Such normal callable functions
77ee819aedSJulien Thierry  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
78ee819aedSJulien Thierry  * category.  In this case, no special debugging annotations are needed because
79ee819aedSJulien Thierry  * objtool can automatically generate the ORC data for the ORC unwinder to read
80ee819aedSJulien Thierry  * at runtime.
81ee819aedSJulien Thierry  *
82ee819aedSJulien Thierry  * Anything which doesn't fall into the above category, such as syscall and
83ee819aedSJulien Thierry  * interrupt handlers, tends to not be called directly by other functions, and
84ee819aedSJulien Thierry  * often does unusual non-C-function-type things with the stack pointer.  Such
85ee819aedSJulien Thierry  * code needs to be annotated such that objtool can understand it.  The
86ee819aedSJulien Thierry  * following CFI hint macros are for this type of code.
87ee819aedSJulien Thierry  *
88ee819aedSJulien Thierry  * These macros provide hints to objtool about the state of the stack at each
89ee819aedSJulien Thierry  * instruction.  Objtool starts from the hints and follows the code flow,
90ee819aedSJulien Thierry  * making automatic CFI adjustments when it sees pushes and pops, filling out
91ee819aedSJulien Thierry  * the debuginfo as necessary.  It will also warn if it sees any
92ee819aedSJulien Thierry  * inconsistencies.
93ee819aedSJulien Thierry  */
94ffb1b4a4SJosh Poimboeuf .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0 end=0
951c0c1fafSJosh Poimboeuf .Lhere_\@:
96ee819aedSJulien Thierry 	.pushsection .discard.unwind_hints
97ee819aedSJulien Thierry 		/* struct unwind_hint */
981c0c1fafSJosh Poimboeuf 		.long .Lhere_\@ - .
99ee819aedSJulien Thierry 		.short \sp_offset
100ee819aedSJulien Thierry 		.byte \sp_reg
101ee819aedSJulien Thierry 		.byte \type
102ffb1b4a4SJosh Poimboeuf 		.byte \signal
103ee819aedSJulien Thierry 		.byte \end
104ee819aedSJulien Thierry 		.balign 4
105ee819aedSJulien Thierry 	.popsection
106ee819aedSJulien Thierry .endm
107ee819aedSJulien Thierry 
108081df943SJosh Poimboeuf .macro STACK_FRAME_NON_STANDARD func:req
109081df943SJosh Poimboeuf 	.pushsection .discard.func_stack_frame_non_standard, "aw"
1101c0c1fafSJosh Poimboeuf 	.long \func - .
111081df943SJosh Poimboeuf 	.popsection
112081df943SJosh Poimboeuf .endm
113081df943SJosh Poimboeuf 
1147b6c7a87SJosh Poimboeuf .macro STACK_FRAME_NON_STANDARD_FP func:req
1157b6c7a87SJosh Poimboeuf #ifdef CONFIG_FRAME_POINTER
1167b6c7a87SJosh Poimboeuf 	STACK_FRAME_NON_STANDARD \func
1177b6c7a87SJosh Poimboeuf #endif
1187b6c7a87SJosh Poimboeuf .endm
1197b6c7a87SJosh Poimboeuf 
120c8c301abSPeter Zijlstra .macro ANNOTATE_NOENDBR
121c8c301abSPeter Zijlstra .Lhere_\@:
122c8c301abSPeter Zijlstra 	.pushsection .discard.noendbr
1231c0c1fafSJosh Poimboeuf 	.long	.Lhere_\@ - .
124c8c301abSPeter Zijlstra 	.popsection
125c8c301abSPeter Zijlstra .endm
126c8c301abSPeter Zijlstra 
127*4708ea14SJosh Poimboeuf /*
128*4708ea14SJosh Poimboeuf  * Use objtool to validate the entry requirement that all code paths do
129*4708ea14SJosh Poimboeuf  * VALIDATE_UNRET_END before RET.
130*4708ea14SJosh Poimboeuf  *
131*4708ea14SJosh Poimboeuf  * NOTE: The macro must be used at the beginning of a global symbol, otherwise
132*4708ea14SJosh Poimboeuf  * it will be ignored.
133*4708ea14SJosh Poimboeuf  */
134*4708ea14SJosh Poimboeuf .macro VALIDATE_UNRET_BEGIN
135*4708ea14SJosh Poimboeuf #if defined(CONFIG_NOINSTR_VALIDATION) && defined(CONFIG_CPU_UNRET_ENTRY)
136*4708ea14SJosh Poimboeuf .Lhere_\@:
137*4708ea14SJosh Poimboeuf 	.pushsection .discard.validate_unret
138*4708ea14SJosh Poimboeuf 	.long	.Lhere_\@ - .
139*4708ea14SJosh Poimboeuf 	.popsection
140*4708ea14SJosh Poimboeuf #endif
141*4708ea14SJosh Poimboeuf .endm
142*4708ea14SJosh Poimboeuf 
143dca5da2aSPeter Zijlstra .macro REACHABLE
144dca5da2aSPeter Zijlstra .Lhere_\@:
145dca5da2aSPeter Zijlstra 	.pushsection .discard.reachable
146dca5da2aSPeter Zijlstra 	.long	.Lhere_\@ - .
147dca5da2aSPeter Zijlstra 	.popsection
148dca5da2aSPeter Zijlstra .endm
149dca5da2aSPeter Zijlstra 
1505567c6c3SJulien Thierry #endif /* __ASSEMBLY__ */
1515567c6c3SJulien Thierry 
15203f16cd0SJosh Poimboeuf #else /* !CONFIG_OBJTOOL */
15300089c04SJulien Thierry 
154ee819aedSJulien Thierry #ifndef __ASSEMBLY__
155ee819aedSJulien Thierry 
156d88ebba4SJosh Poimboeuf #define UNWIND_HINT(type, sp_reg, sp_offset, signal, end) "\n\t"
15700089c04SJulien Thierry #define STACK_FRAME_NON_STANDARD(func)
158e028c4f7SJosh Poimboeuf #define STACK_FRAME_NON_STANDARD_FP(func)
159c8c301abSPeter Zijlstra #define ANNOTATE_NOENDBR
160dca5da2aSPeter Zijlstra #define ASM_REACHABLE
161ee819aedSJulien Thierry #else
16200089c04SJulien Thierry #define ANNOTATE_INTRA_FUNCTION_CALL
163ffb1b4a4SJosh Poimboeuf .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0 end=0
164ee819aedSJulien Thierry .endm
165081df943SJosh Poimboeuf .macro STACK_FRAME_NON_STANDARD func:req
166081df943SJosh Poimboeuf .endm
167c8c301abSPeter Zijlstra .macro ANNOTATE_NOENDBR
168c8c301abSPeter Zijlstra .endm
169dca5da2aSPeter Zijlstra .macro REACHABLE
170dca5da2aSPeter Zijlstra .endm
171ee819aedSJulien Thierry #endif
17200089c04SJulien Thierry 
17303f16cd0SJosh Poimboeuf #endif /* CONFIG_OBJTOOL */
17400089c04SJulien Thierry 
17500089c04SJulien Thierry #endif /* _LINUX_OBJTOOL_H */
176