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