objtool.h 5.6 KB

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