xref: /linux-6.15/include/linux/objtool.h (revision d88ebba4)
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, end)	\
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 	".byte " __stringify(end) "\n\t"			\
23 	".balign 4 \n\t"					\
24 	".popsection\n\t"
25 
26 /*
27  * This macro marks the given function's stack frame as "non-standard", which
28  * tells objtool to ignore the function when doing stack metadata validation.
29  * It should only be used in special cases where you're 100% sure it won't
30  * affect the reliability of frame pointers and kernel stack traces.
31  *
32  * For more information, see tools/objtool/Documentation/objtool.txt.
33  */
34 #define STACK_FRAME_NON_STANDARD(func) \
35 	static void __used __section(".discard.func_stack_frame_non_standard") \
36 		*__func_stack_frame_non_standard_##func = func
37 
38 /*
39  * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore
40  * for the case where a function is intentionally missing frame pointer setup,
41  * but otherwise needs objtool/ORC coverage when frame pointers are disabled.
42  */
43 #ifdef CONFIG_FRAME_POINTER
44 #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func)
45 #else
46 #define STACK_FRAME_NON_STANDARD_FP(func)
47 #endif
48 
49 #define ANNOTATE_NOENDBR					\
50 	"986: \n\t"						\
51 	".pushsection .discard.noendbr\n\t"			\
52 	".long 986b - .\n\t"					\
53 	".popsection\n\t"
54 
55 #define ASM_REACHABLE							\
56 	"998:\n\t"							\
57 	".pushsection .discard.reachable\n\t"				\
58 	".long 998b - .\n\t"						\
59 	".popsection\n\t"
60 
61 #else /* __ASSEMBLY__ */
62 
63 /*
64  * This macro indicates that the following intra-function call is valid.
65  * Any non-annotated intra-function call will cause objtool to issue a warning.
66  */
67 #define ANNOTATE_INTRA_FUNCTION_CALL				\
68 	999:							\
69 	.pushsection .discard.intra_function_calls;		\
70 	.long 999b - .;						\
71 	.popsection;
72 
73 /*
74  * In asm, there are two kinds of code: normal C-type callable functions and
75  * the rest.  The normal callable functions can be called by other code, and
76  * don't do anything unusual with the stack.  Such normal callable functions
77  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
78  * category.  In this case, no special debugging annotations are needed because
79  * objtool can automatically generate the ORC data for the ORC unwinder to read
80  * at runtime.
81  *
82  * Anything which doesn't fall into the above category, such as syscall and
83  * interrupt handlers, tends to not be called directly by other functions, and
84  * often does unusual non-C-function-type things with the stack pointer.  Such
85  * code needs to be annotated such that objtool can understand it.  The
86  * following CFI hint macros are for this type of code.
87  *
88  * These macros provide hints to objtool about the state of the stack at each
89  * instruction.  Objtool starts from the hints and follows the code flow,
90  * making automatic CFI adjustments when it sees pushes and pops, filling out
91  * the debuginfo as necessary.  It will also warn if it sees any
92  * inconsistencies.
93  */
94 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0 end=0
95 .Lhere_\@:
96 	.pushsection .discard.unwind_hints
97 		/* struct unwind_hint */
98 		.long .Lhere_\@ - .
99 		.short \sp_offset
100 		.byte \sp_reg
101 		.byte \type
102 		.byte \signal
103 		.byte \end
104 		.balign 4
105 	.popsection
106 .endm
107 
108 .macro STACK_FRAME_NON_STANDARD func:req
109 	.pushsection .discard.func_stack_frame_non_standard, "aw"
110 	.long \func - .
111 	.popsection
112 .endm
113 
114 .macro STACK_FRAME_NON_STANDARD_FP func:req
115 #ifdef CONFIG_FRAME_POINTER
116 	STACK_FRAME_NON_STANDARD \func
117 #endif
118 .endm
119 
120 .macro ANNOTATE_NOENDBR
121 .Lhere_\@:
122 	.pushsection .discard.noendbr
123 	.long	.Lhere_\@ - .
124 	.popsection
125 .endm
126 
127 .macro REACHABLE
128 .Lhere_\@:
129 	.pushsection .discard.reachable
130 	.long	.Lhere_\@ - .
131 	.popsection
132 .endm
133 
134 #endif /* __ASSEMBLY__ */
135 
136 #else /* !CONFIG_OBJTOOL */
137 
138 #ifndef __ASSEMBLY__
139 
140 #define UNWIND_HINT(type, sp_reg, sp_offset, signal, end) "\n\t"
141 #define STACK_FRAME_NON_STANDARD(func)
142 #define STACK_FRAME_NON_STANDARD_FP(func)
143 #define ANNOTATE_NOENDBR
144 #define ASM_REACHABLE
145 #else
146 #define ANNOTATE_INTRA_FUNCTION_CALL
147 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 signal=0 end=0
148 .endm
149 .macro STACK_FRAME_NON_STANDARD func:req
150 .endm
151 .macro ANNOTATE_NOENDBR
152 .endm
153 .macro REACHABLE
154 .endm
155 #endif
156 
157 #endif /* CONFIG_OBJTOOL */
158 
159 #endif /* _LINUX_OBJTOOL_H */
160