xref: /linux-6.15/include/linux/objtool.h (revision 112765ca)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_OBJTOOL_H
3 #define _LINUX_OBJTOOL_H
4 
5 #include <linux/objtool_types.h>
6 
7 #ifdef CONFIG_OBJTOOL
8 
9 #include <asm/asm.h>
10 
11 #ifndef __ASSEMBLY__
12 
13 #define UNWIND_HINT(type, sp_reg, sp_offset, signal)	\
14 	"987: \n\t"						\
15 	".pushsection .discard.unwind_hints\n\t"		\
16 	/* struct unwind_hint */				\
17 	".long 987b - .\n\t"					\
18 	".short " __stringify(sp_offset) "\n\t"			\
19 	".byte " __stringify(sp_reg) "\n\t"			\
20 	".byte " __stringify(type) "\n\t"			\
21 	".byte " __stringify(signal) "\n\t"			\
22 	".balign 4 \n\t"					\
23 	".popsection\n\t"
24 
25 /*
26  * This macro marks the given function's stack frame as "non-standard", which
27  * tells objtool to ignore the function when doing stack metadata validation.
28  * It should only be used in special cases where you're 100% sure it won't
29  * affect the reliability of frame pointers and kernel stack traces.
30  *
31  * For more information, see tools/objtool/Documentation/objtool.txt.
32  */
33 #define STACK_FRAME_NON_STANDARD(func) \
34 	static void __used __section(".discard.func_stack_frame_non_standard") \
35 		*__func_stack_frame_non_standard_##func = func
36 
37 /*
38  * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore
39  * for the case where a function is intentionally missing frame pointer setup,
40  * but otherwise needs objtool/ORC coverage when frame pointers are disabled.
41  */
42 #ifdef CONFIG_FRAME_POINTER
43 #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func)
44 #else
45 #define STACK_FRAME_NON_STANDARD_FP(func)
46 #endif
47 
48 #define ASM_REACHABLE							\
49 	"998:\n\t"							\
50 	".pushsection .discard.reachable\n\t"				\
51 	".long 998b\n\t"						\
52 	".popsection\n\t"
53 
54 #define __ASM_BREF(label)	label ## b
55 
56 #define __ASM_ANNOTATE(label, type)					\
57 	".pushsection .discard.annotate_insn,\"M\",@progbits,8\n\t"	\
58 	".long " __stringify(label) " - .\n\t"			\
59 	".long " __stringify(type) "\n\t"				\
60 	".popsection\n\t"
61 
62 #define ASM_ANNOTATE(type)						\
63 	"911:\n\t"						\
64 	__ASM_ANNOTATE(911b, type)
65 
66 #define ANNOTATE_NOENDBR	ASM_ANNOTATE(ANNOTYPE_NOENDBR)
67 
68 #else /* __ASSEMBLY__ */
69 
70 /*
71  * In asm, there are two kinds of code: normal C-type callable functions and
72  * the rest.  The normal callable functions can be called by other code, and
73  * don't do anything unusual with the stack.  Such normal callable functions
74  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
75  * category.  In this case, no special debugging annotations are needed because
76  * objtool can automatically generate the ORC data for the ORC unwinder to read
77  * at runtime.
78  *
79  * Anything which doesn't fall into the above category, such as syscall and
80  * interrupt handlers, tends to not be called directly by other functions, and
81  * often does unusual non-C-function-type things with the stack pointer.  Such
82  * code needs to be annotated such that objtool can understand it.  The
83  * following CFI hint macros are for this type of code.
84  *
85  * These macros provide hints to objtool about the state of the stack at each
86  * instruction.  Objtool starts from the hints and follows the code flow,
87  * making automatic CFI adjustments when it sees pushes and pops, filling out
88  * the debuginfo as necessary.  It will also warn if it sees any
89  * inconsistencies.
90  */
91 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
92 .Lhere_\@:
93 	.pushsection .discard.unwind_hints
94 		/* struct unwind_hint */
95 		.long .Lhere_\@ - .
96 		.short \sp_offset
97 		.byte \sp_reg
98 		.byte \type
99 		.byte \signal
100 		.balign 4
101 	.popsection
102 .endm
103 
104 .macro STACK_FRAME_NON_STANDARD func:req
105 	.pushsection .discard.func_stack_frame_non_standard, "aw"
106 	.long \func - .
107 	.popsection
108 .endm
109 
110 .macro STACK_FRAME_NON_STANDARD_FP func:req
111 #ifdef CONFIG_FRAME_POINTER
112 	STACK_FRAME_NON_STANDARD \func
113 #endif
114 .endm
115 
116 /*
117  * Use objtool to validate the entry requirement that all code paths do
118  * VALIDATE_UNRET_END before RET.
119  *
120  * NOTE: The macro must be used at the beginning of a global symbol, otherwise
121  * it will be ignored.
122  */
123 #if defined(CONFIG_NOINSTR_VALIDATION) && \
124 	(defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO))
125 #define VALIDATE_UNRET_BEGIN	ANNOTATE type=ANNOTYPE_UNRET_BEGIN
126 #else
127 #define VALIDATE_UNRET_BEGIN
128 #endif
129 
130 .macro REACHABLE
131 .Lhere_\@:
132 	.pushsection .discard.reachable
133 	.long	.Lhere_\@
134 	.popsection
135 .endm
136 
137 .macro ANNOTATE type:req
138 .Lhere_\@:
139 	.pushsection .discard.annotate_insn,"M",@progbits,8
140 	.long	.Lhere_\@ - .
141 	.long	\type
142 	.popsection
143 .endm
144 
145 #define ANNOTATE_NOENDBR	ANNOTATE type=ANNOTYPE_NOENDBR
146 
147 /*
148  * This macro indicates that the following intra-function call is valid.
149  * Any non-annotated intra-function call will cause objtool to issue a warning.
150  */
151 #define ANNOTATE_INTRA_FUNCTION_CALL ANNOTATE type=ANNOTYPE_INTRA_FUNCTION_CALL
152 
153 #endif /* __ASSEMBLY__ */
154 
155 #else /* !CONFIG_OBJTOOL */
156 
157 #ifndef __ASSEMBLY__
158 
159 #define UNWIND_HINT(type, sp_reg, sp_offset, signal) "\n\t"
160 #define STACK_FRAME_NON_STANDARD(func)
161 #define STACK_FRAME_NON_STANDARD_FP(func)
162 #define __ASM_ANNOTATE(label, type)
163 #define ASM_ANNOTATE(type)
164 #define ANNOTATE_NOENDBR
165 #define ASM_REACHABLE
166 #else
167 #define ANNOTATE_INTRA_FUNCTION_CALL
168 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
169 .endm
170 .macro STACK_FRAME_NON_STANDARD func:req
171 .endm
172 .macro ANNOTATE_NOENDBR
173 .endm
174 .macro REACHABLE
175 .endm
176 .macro ANNOTATE type:req
177 .endm
178 #endif
179 
180 #endif /* CONFIG_OBJTOOL */
181 
182 #endif /* _LINUX_OBJTOOL_H */
183