Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_OBJTOOL_H
0003 #define _LINUX_OBJTOOL_H
0004 
0005 #ifndef __ASSEMBLY__
0006 
0007 #include <linux/types.h>
0008 
0009 /*
0010  * This struct is used by asm and inline asm code to manually annotate the
0011  * location of registers on the stack.
0012  */
0013 struct unwind_hint {
0014     u32     ip;
0015     s16     sp_offset;
0016     u8      sp_reg;
0017     u8      type;
0018     u8      end;
0019 };
0020 #endif
0021 
0022 /*
0023  * UNWIND_HINT_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP
0024  * (the caller's SP right before it made the call).  Used for all callable
0025  * functions, i.e. all C code and all callable asm functions.
0026  *
0027  * UNWIND_HINT_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset
0028  * points to a fully populated pt_regs from a syscall, interrupt, or exception.
0029  *
0030  * UNWIND_HINT_TYPE_REGS_PARTIAL: Used in entry code to indicate that
0031  * sp_reg+sp_offset points to the iret return frame.
0032  *
0033  * UNWIND_HINT_FUNC: Generate the unwind metadata of a callable function.
0034  * Useful for code which doesn't have an ELF function annotation.
0035  *
0036  * UNWIND_HINT_ENTRY: machine entry without stack, SYSCALL/SYSENTER etc.
0037  */
0038 #define UNWIND_HINT_TYPE_CALL       0
0039 #define UNWIND_HINT_TYPE_REGS       1
0040 #define UNWIND_HINT_TYPE_REGS_PARTIAL   2
0041 #define UNWIND_HINT_TYPE_FUNC       3
0042 #define UNWIND_HINT_TYPE_ENTRY      4
0043 #define UNWIND_HINT_TYPE_SAVE       5
0044 #define UNWIND_HINT_TYPE_RESTORE    6
0045 
0046 #ifdef CONFIG_OBJTOOL
0047 
0048 #include <asm/asm.h>
0049 
0050 #ifndef __ASSEMBLY__
0051 
0052 #define UNWIND_HINT(sp_reg, sp_offset, type, end)       \
0053     "987: \n\t"                     \
0054     ".pushsection .discard.unwind_hints\n\t"        \
0055     /* struct unwind_hint */                \
0056     ".long 987b - .\n\t"                    \
0057     ".short " __stringify(sp_offset) "\n\t"         \
0058     ".byte " __stringify(sp_reg) "\n\t"         \
0059     ".byte " __stringify(type) "\n\t"           \
0060     ".byte " __stringify(end) "\n\t"            \
0061     ".balign 4 \n\t"                    \
0062     ".popsection\n\t"
0063 
0064 /*
0065  * This macro marks the given function's stack frame as "non-standard", which
0066  * tells objtool to ignore the function when doing stack metadata validation.
0067  * It should only be used in special cases where you're 100% sure it won't
0068  * affect the reliability of frame pointers and kernel stack traces.
0069  *
0070  * For more information, see tools/objtool/Documentation/objtool.txt.
0071  */
0072 #define STACK_FRAME_NON_STANDARD(func) \
0073     static void __used __section(".discard.func_stack_frame_non_standard") \
0074         *__func_stack_frame_non_standard_##func = func
0075 
0076 /*
0077  * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore
0078  * for the case where a function is intentionally missing frame pointer setup,
0079  * but otherwise needs objtool/ORC coverage when frame pointers are disabled.
0080  */
0081 #ifdef CONFIG_FRAME_POINTER
0082 #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func)
0083 #else
0084 #define STACK_FRAME_NON_STANDARD_FP(func)
0085 #endif
0086 
0087 #define ANNOTATE_NOENDBR                    \
0088     "986: \n\t"                     \
0089     ".pushsection .discard.noendbr\n\t"         \
0090     _ASM_PTR " 986b\n\t"                    \
0091     ".popsection\n\t"
0092 
0093 #define ASM_REACHABLE                           \
0094     "998:\n\t"                          \
0095     ".pushsection .discard.reachable\n\t"               \
0096     ".long 998b - .\n\t"                        \
0097     ".popsection\n\t"
0098 
0099 #else /* __ASSEMBLY__ */
0100 
0101 /*
0102  * This macro indicates that the following intra-function call is valid.
0103  * Any non-annotated intra-function call will cause objtool to issue a warning.
0104  */
0105 #define ANNOTATE_INTRA_FUNCTION_CALL                \
0106     999:                            \
0107     .pushsection .discard.intra_function_calls;     \
0108     .long 999b;                     \
0109     .popsection;
0110 
0111 /*
0112  * In asm, there are two kinds of code: normal C-type callable functions and
0113  * the rest.  The normal callable functions can be called by other code, and
0114  * don't do anything unusual with the stack.  Such normal callable functions
0115  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
0116  * category.  In this case, no special debugging annotations are needed because
0117  * objtool can automatically generate the ORC data for the ORC unwinder to read
0118  * at runtime.
0119  *
0120  * Anything which doesn't fall into the above category, such as syscall and
0121  * interrupt handlers, tends to not be called directly by other functions, and
0122  * often does unusual non-C-function-type things with the stack pointer.  Such
0123  * code needs to be annotated such that objtool can understand it.  The
0124  * following CFI hint macros are for this type of code.
0125  *
0126  * These macros provide hints to objtool about the state of the stack at each
0127  * instruction.  Objtool starts from the hints and follows the code flow,
0128  * making automatic CFI adjustments when it sees pushes and pops, filling out
0129  * the debuginfo as necessary.  It will also warn if it sees any
0130  * inconsistencies.
0131  */
0132 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 end=0
0133 .Lunwind_hint_ip_\@:
0134     .pushsection .discard.unwind_hints
0135         /* struct unwind_hint */
0136         .long .Lunwind_hint_ip_\@ - .
0137         .short \sp_offset
0138         .byte \sp_reg
0139         .byte \type
0140         .byte \end
0141         .balign 4
0142     .popsection
0143 .endm
0144 
0145 .macro STACK_FRAME_NON_STANDARD func:req
0146     .pushsection .discard.func_stack_frame_non_standard, "aw"
0147     _ASM_PTR \func
0148     .popsection
0149 .endm
0150 
0151 .macro STACK_FRAME_NON_STANDARD_FP func:req
0152 #ifdef CONFIG_FRAME_POINTER
0153     STACK_FRAME_NON_STANDARD \func
0154 #endif
0155 .endm
0156 
0157 .macro ANNOTATE_NOENDBR
0158 .Lhere_\@:
0159     .pushsection .discard.noendbr
0160     .quad   .Lhere_\@
0161     .popsection
0162 .endm
0163 
0164 .macro REACHABLE
0165 .Lhere_\@:
0166     .pushsection .discard.reachable
0167     .long   .Lhere_\@ - .
0168     .popsection
0169 .endm
0170 
0171 #endif /* __ASSEMBLY__ */
0172 
0173 #else /* !CONFIG_OBJTOOL */
0174 
0175 #ifndef __ASSEMBLY__
0176 
0177 #define UNWIND_HINT(sp_reg, sp_offset, type, end)   \
0178     "\n\t"
0179 #define STACK_FRAME_NON_STANDARD(func)
0180 #define STACK_FRAME_NON_STANDARD_FP(func)
0181 #define ANNOTATE_NOENDBR
0182 #define ASM_REACHABLE
0183 #else
0184 #define ANNOTATE_INTRA_FUNCTION_CALL
0185 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 end=0
0186 .endm
0187 .macro STACK_FRAME_NON_STANDARD func:req
0188 .endm
0189 .macro ANNOTATE_NOENDBR
0190 .endm
0191 .macro REACHABLE
0192 .endm
0193 #endif
0194 
0195 #endif /* CONFIG_OBJTOOL */
0196 
0197 #endif /* _LINUX_OBJTOOL_H */