xref: /linux-6.15/include/linux/objtool.h (revision bb817006)
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 #else /* __ASSEMBLY__ */
67 
68 /*
69  * In asm, there are two kinds of code: normal C-type callable functions and
70  * the rest.  The normal callable functions can be called by other code, and
71  * don't do anything unusual with the stack.  Such normal callable functions
72  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
73  * category.  In this case, no special debugging annotations are needed because
74  * objtool can automatically generate the ORC data for the ORC unwinder to read
75  * at runtime.
76  *
77  * Anything which doesn't fall into the above category, such as syscall and
78  * interrupt handlers, tends to not be called directly by other functions, and
79  * often does unusual non-C-function-type things with the stack pointer.  Such
80  * code needs to be annotated such that objtool can understand it.  The
81  * following CFI hint macros are for this type of code.
82  *
83  * These macros provide hints to objtool about the state of the stack at each
84  * instruction.  Objtool starts from the hints and follows the code flow,
85  * making automatic CFI adjustments when it sees pushes and pops, filling out
86  * the debuginfo as necessary.  It will also warn if it sees any
87  * inconsistencies.
88  */
89 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
90 .Lhere_\@:
91 	.pushsection .discard.unwind_hints
92 		/* struct unwind_hint */
93 		.long .Lhere_\@ - .
94 		.short \sp_offset
95 		.byte \sp_reg
96 		.byte \type
97 		.byte \signal
98 		.balign 4
99 	.popsection
100 .endm
101 
102 .macro STACK_FRAME_NON_STANDARD func:req
103 	.pushsection .discard.func_stack_frame_non_standard, "aw"
104 	.long \func - .
105 	.popsection
106 .endm
107 
108 .macro STACK_FRAME_NON_STANDARD_FP func:req
109 #ifdef CONFIG_FRAME_POINTER
110 	STACK_FRAME_NON_STANDARD \func
111 #endif
112 .endm
113 
114 
115 .macro REACHABLE
116 .Lhere_\@:
117 	.pushsection .discard.reachable
118 	.long	.Lhere_\@
119 	.popsection
120 .endm
121 
122 .macro ANNOTATE type:req
123 .Lhere_\@:
124 	.pushsection .discard.annotate_insn,"M",@progbits,8
125 	.long	.Lhere_\@ - .
126 	.long	\type
127 	.popsection
128 .endm
129 
130 #endif /* __ASSEMBLY__ */
131 
132 #else /* !CONFIG_OBJTOOL */
133 
134 #ifndef __ASSEMBLY__
135 
136 #define UNWIND_HINT(type, sp_reg, sp_offset, signal) "\n\t"
137 #define STACK_FRAME_NON_STANDARD(func)
138 #define STACK_FRAME_NON_STANDARD_FP(func)
139 #define __ASM_ANNOTATE(label, type)
140 #define ASM_ANNOTATE(type)
141 #define ASM_REACHABLE
142 #else
143 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
144 .endm
145 .macro STACK_FRAME_NON_STANDARD func:req
146 .endm
147 .macro REACHABLE
148 .endm
149 .macro ANNOTATE type:req
150 .endm
151 #endif
152 
153 #endif /* CONFIG_OBJTOOL */
154 
155 #ifndef __ASSEMBLY__
156 /*
157  * Annotate away the various 'relocation to !ENDBR` complaints; knowing that
158  * these relocations will never be used for indirect calls.
159  */
160 #define ANNOTATE_NOENDBR		ASM_ANNOTATE(ANNOTYPE_NOENDBR)
161 /*
162  * This should be used immediately before an indirect jump/call. It tells
163  * objtool the subsequent indirect jump/call is vouched safe for retpoline
164  * builds.
165  */
166 #define ANNOTATE_RETPOLINE_SAFE		ASM_ANNOTATE(ANNOTYPE_RETPOLINE_SAFE)
167 /*
168  * See linux/instrumentation.h
169  */
170 #define ANNOTATE_INSTR_BEGIN(label)	__ASM_ANNOTATE(label, ANNOTYPE_INSTR_BEGIN)
171 #define ANNOTATE_INSTR_END(label)	__ASM_ANNOTATE(label, ANNOTYPE_INSTR_END)
172 /*
173  * objtool annotation to ignore the alternatives and only consider the original
174  * instruction(s).
175  */
176 #define ANNOTATE_IGNORE_ALTERNATIVE	ASM_ANNOTATE(ANNOTYPE_IGNORE_ALTS)
177 /*
178  * This macro indicates that the following intra-function call is valid.
179  * Any non-annotated intra-function call will cause objtool to issue a warning.
180  */
181 #define ANNOTATE_INTRA_FUNCTION_CALL	ASM_ANNOTATE(ANNOTYPE_INTRA_FUNCTION_CALL)
182 /*
183  * Use objtool to validate the entry requirement that all code paths do
184  * VALIDATE_UNRET_END before RET.
185  *
186  * NOTE: The macro must be used at the beginning of a global symbol, otherwise
187  * it will be ignored.
188  */
189 #define ANNOTATE_UNRET_BEGIN		ASM_ANNOTATE(ANNOTYPE_UNRET_BEGIN)
190 
191 #else
192 #define ANNOTATE_NOENDBR		ANNOTATE type=ANNOTYPE_NOENDBR
193 #define ANNOTATE_RETPOLINE_SAFE		ANNOTATE type=ANNOTYPE_RETPOLINE_SAFE
194 /*	ANNOTATE_INSTR_BEGIN		ANNOTATE type=ANNOTYPE_INSTR_BEGIN */
195 /*	ANNOTATE_INSTR_END		ANNOTATE type=ANNOTYPE_INSTR_END */
196 #define ANNOTATE_IGNORE_ALTERNATIVE	ANNOTATE type=ANNOTYPE_IGNORE_ALTS
197 #define ANNOTATE_INTRA_FUNCTION_CALL	ANNOTATE type=ANNOTYPE_INTRA_FUNCTION_CALL
198 #define ANNOTATE_UNRET_BEGIN		ANNOTATE type=ANNOTYPE_UNRET_BEGIN
199 #endif
200 
201 #if defined(CONFIG_NOINSTR_VALIDATION) && \
202 	(defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO))
203 #define VALIDATE_UNRET_BEGIN	ANNOTATE_UNRET_BEGIN
204 #else
205 #define VALIDATE_UNRET_BEGIN
206 #endif
207 
208 #endif /* _LINUX_OBJTOOL_H */
209