xref: /linux-6.15/include/linux/objtool.h (revision 2116b349)
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 ANNOTATE_NOENDBR					\
49 	"986: \n\t"						\
50 	".pushsection .discard.noendbr\n\t"			\
51 	".long 986b\n\t"					\
52 	".popsection\n\t"
53 
54 #define ASM_REACHABLE							\
55 	"998:\n\t"							\
56 	".pushsection .discard.reachable\n\t"				\
57 	".long 998b\n\t"						\
58 	".popsection\n\t"
59 
60 #define ASM_ANNOTATE(type)						\
61 	"911:\n\t"							\
62 	".pushsection .discard.annotate_insn,\"M\",@progbits,8\n\t"	\
63 	".long 911b - .\n\t"						\
64 	".long " __stringify(type) "\n\t"				\
65 	".popsection\n\t"
66 
67 #else /* __ASSEMBLY__ */
68 
69 /*
70  * This macro indicates that the following intra-function call is valid.
71  * Any non-annotated intra-function call will cause objtool to issue a warning.
72  */
73 #define ANNOTATE_INTRA_FUNCTION_CALL				\
74 	999:							\
75 	.pushsection .discard.intra_function_calls;		\
76 	.long 999b;						\
77 	.popsection;
78 
79 /*
80  * In asm, there are two kinds of code: normal C-type callable functions and
81  * the rest.  The normal callable functions can be called by other code, and
82  * don't do anything unusual with the stack.  Such normal callable functions
83  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
84  * category.  In this case, no special debugging annotations are needed because
85  * objtool can automatically generate the ORC data for the ORC unwinder to read
86  * at runtime.
87  *
88  * Anything which doesn't fall into the above category, such as syscall and
89  * interrupt handlers, tends to not be called directly by other functions, and
90  * often does unusual non-C-function-type things with the stack pointer.  Such
91  * code needs to be annotated such that objtool can understand it.  The
92  * following CFI hint macros are for this type of code.
93  *
94  * These macros provide hints to objtool about the state of the stack at each
95  * instruction.  Objtool starts from the hints and follows the code flow,
96  * making automatic CFI adjustments when it sees pushes and pops, filling out
97  * the debuginfo as necessary.  It will also warn if it sees any
98  * inconsistencies.
99  */
100 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
101 .Lhere_\@:
102 	.pushsection .discard.unwind_hints
103 		/* struct unwind_hint */
104 		.long .Lhere_\@ - .
105 		.short \sp_offset
106 		.byte \sp_reg
107 		.byte \type
108 		.byte \signal
109 		.balign 4
110 	.popsection
111 .endm
112 
113 .macro STACK_FRAME_NON_STANDARD func:req
114 	.pushsection .discard.func_stack_frame_non_standard, "aw"
115 	.long \func - .
116 	.popsection
117 .endm
118 
119 .macro STACK_FRAME_NON_STANDARD_FP func:req
120 #ifdef CONFIG_FRAME_POINTER
121 	STACK_FRAME_NON_STANDARD \func
122 #endif
123 .endm
124 
125 .macro ANNOTATE_NOENDBR
126 .Lhere_\@:
127 	.pushsection .discard.noendbr
128 	.long	.Lhere_\@
129 	.popsection
130 .endm
131 
132 /*
133  * Use objtool to validate the entry requirement that all code paths do
134  * VALIDATE_UNRET_END before RET.
135  *
136  * NOTE: The macro must be used at the beginning of a global symbol, otherwise
137  * it will be ignored.
138  */
139 .macro VALIDATE_UNRET_BEGIN
140 #if defined(CONFIG_NOINSTR_VALIDATION) && \
141 	(defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO))
142 .Lhere_\@:
143 	.pushsection .discard.validate_unret
144 	.long	.Lhere_\@ - .
145 	.popsection
146 #endif
147 .endm
148 
149 .macro REACHABLE
150 .Lhere_\@:
151 	.pushsection .discard.reachable
152 	.long	.Lhere_\@
153 	.popsection
154 .endm
155 
156 .macro ANNOTATE type:req
157 .Lhere_\@:
158 	.pushsection .discard.annotate_insn,"M",@progbits,8
159 	.long	.Lhere_\@ - .
160 	.long	\type
161 	.popsection
162 .endm
163 
164 #endif /* __ASSEMBLY__ */
165 
166 #else /* !CONFIG_OBJTOOL */
167 
168 #ifndef __ASSEMBLY__
169 
170 #define UNWIND_HINT(type, sp_reg, sp_offset, signal) "\n\t"
171 #define STACK_FRAME_NON_STANDARD(func)
172 #define STACK_FRAME_NON_STANDARD_FP(func)
173 #define ASM_ANNOTATE(type)
174 #define ANNOTATE_NOENDBR
175 #define ASM_REACHABLE
176 #else
177 #define ANNOTATE_INTRA_FUNCTION_CALL
178 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0
179 .endm
180 .macro STACK_FRAME_NON_STANDARD func:req
181 .endm
182 .macro ANNOTATE_NOENDBR
183 .endm
184 .macro REACHABLE
185 .endm
186 .macro ANNOTATE type:req
187 .endm
188 #endif
189 
190 #endif /* CONFIG_OBJTOOL */
191 
192 #endif /* _LINUX_OBJTOOL_H */
193