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