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