| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_ANNOTATE_H |
| 3 | #define _LINUX_ANNOTATE_H |
| 4 | |
| 5 | #include <linux/objtool_types.h> |
| 6 | |
| 7 | #ifdef CONFIG_OBJTOOL |
| 8 | |
| 9 | #define __ASM_ANNOTATE(section, label, type) \ |
| 10 | .pushsection section, "M", @progbits, 8; \ |
| 11 | .long label - ., type; \ |
| 12 | .popsection |
| 13 | |
| 14 | #ifndef __ASSEMBLY__ |
| 15 | |
| 16 | #define ASM_ANNOTATE_LABEL(label, type) \ |
| 17 | __stringify(__ASM_ANNOTATE(.discard.annotate_insn, label, type)) |
| 18 | |
| 19 | #define ASM_ANNOTATE(type) \ |
| 20 | "911: " \ |
| 21 | __stringify(__ASM_ANNOTATE(.discard.annotate_insn, 911b, type)) |
| 22 | |
| 23 | #define ASM_ANNOTATE_DATA(type) \ |
| 24 | "912: " \ |
| 25 | __stringify(__ASM_ANNOTATE(.discard.annotate_data, 912b, type)) |
| 26 | |
| 27 | #else /* __ASSEMBLY__ */ |
| 28 | |
| 29 | .macro ANNOTATE type |
| 30 | .Lhere_\@: |
| 31 | __ASM_ANNOTATE(.discard.annotate_insn, .Lhere_\@, \type) |
| 32 | .endm |
| 33 | |
| 34 | .macro ANNOTATE_DATA type |
| 35 | .Lhere_\@: |
| 36 | __ASM_ANNOTATE(.discard.annotate_data, .Lhere_\@, \type) |
| 37 | .endm |
| 38 | |
| 39 | #endif /* __ASSEMBLY__ */ |
| 40 | |
| 41 | #else /* !CONFIG_OBJTOOL */ |
| 42 | #ifndef __ASSEMBLY__ |
| 43 | #define ASM_ANNOTATE_LABEL(label, type) "" |
| 44 | #define ASM_ANNOTATE(type) |
| 45 | #define ASM_ANNOTATE_DATA(type) |
| 46 | #else /* __ASSEMBLY__ */ |
| 47 | .macro ANNOTATE type |
| 48 | .endm |
| 49 | .macro ANNOTATE_DATA type |
| 50 | .endm |
| 51 | #endif /* __ASSEMBLY__ */ |
| 52 | #endif /* !CONFIG_OBJTOOL */ |
| 53 | |
| 54 | #ifndef __ASSEMBLY__ |
| 55 | |
| 56 | /* |
| 57 | * Annotate away the various 'relocation to !ENDBR` complaints; knowing that |
| 58 | * these relocations will never be used for indirect calls. |
| 59 | */ |
| 60 | #define ANNOTATE_NOENDBR ASM_ANNOTATE(ANNOTYPE_NOENDBR) |
| 61 | #define ANNOTATE_NOENDBR_SYM(sym) asm(ASM_ANNOTATE_LABEL(sym, ANNOTYPE_NOENDBR)) |
| 62 | |
| 63 | /* |
| 64 | * This should be used immediately before an indirect jump/call. It tells |
| 65 | * objtool the subsequent indirect jump/call is vouched safe for retpoline |
| 66 | * builds. |
| 67 | */ |
| 68 | #define ANNOTATE_RETPOLINE_SAFE ASM_ANNOTATE(ANNOTYPE_RETPOLINE_SAFE) |
| 69 | /* |
| 70 | * See linux/instrumentation.h |
| 71 | */ |
| 72 | #define ANNOTATE_INSTR_BEGIN(label) ASM_ANNOTATE_LABEL(label, ANNOTYPE_INSTR_BEGIN) |
| 73 | #define ANNOTATE_INSTR_END(label) ASM_ANNOTATE_LABEL(label, ANNOTYPE_INSTR_END) |
| 74 | /* |
| 75 | * objtool annotation to ignore the alternatives and only consider the original |
| 76 | * instruction(s). |
| 77 | */ |
| 78 | #define ANNOTATE_IGNORE_ALTERNATIVE ASM_ANNOTATE(ANNOTYPE_IGNORE_ALTS) |
| 79 | /* |
| 80 | * This macro indicates that the following intra-function call is valid. |
| 81 | * Any non-annotated intra-function call will cause objtool to issue a warning. |
| 82 | */ |
| 83 | #define ANNOTATE_INTRA_FUNCTION_CALL ASM_ANNOTATE(ANNOTYPE_INTRA_FUNCTION_CALL) |
| 84 | /* |
| 85 | * Use objtool to validate the entry requirement that all code paths do |
| 86 | * VALIDATE_UNRET_END before RET. |
| 87 | * |
| 88 | * NOTE: The macro must be used at the beginning of a global symbol, otherwise |
| 89 | * it will be ignored. |
| 90 | */ |
| 91 | #define ANNOTATE_UNRET_BEGIN ASM_ANNOTATE(ANNOTYPE_UNRET_BEGIN) |
| 92 | /* |
| 93 | * This should be used to refer to an instruction that is considered |
| 94 | * terminating, like a noreturn CALL or UD2 when we know they are not -- eg |
| 95 | * WARN using UD2. |
| 96 | */ |
| 97 | #define ANNOTATE_REACHABLE(label) ASM_ANNOTATE_LABEL(label, ANNOTYPE_REACHABLE) |
| 98 | /* |
| 99 | * This should not be used; it annotates away CFI violations. There are a few |
| 100 | * valid use cases like kexec handover to the next kernel image, and there is |
| 101 | * no security concern there. |
| 102 | * |
| 103 | * There are also a few real issues annotated away, like EFI because we can't |
| 104 | * control the EFI code. |
| 105 | */ |
| 106 | #define ANNOTATE_NOCFI_SYM(sym) asm(ASM_ANNOTATE_LABEL(sym, ANNOTYPE_NOCFI)) |
| 107 | |
| 108 | /* |
| 109 | * Annotate a special section entry. This emables livepatch module generation |
| 110 | * to find and extract individual special section entries as needed. |
| 111 | */ |
| 112 | #define ANNOTATE_DATA_SPECIAL ASM_ANNOTATE_DATA(ANNOTYPE_DATA_SPECIAL) |
| 113 | |
| 114 | #else /* __ASSEMBLY__ */ |
| 115 | #define ANNOTATE_NOENDBR ANNOTATE type=ANNOTYPE_NOENDBR |
| 116 | #define ANNOTATE_RETPOLINE_SAFE ANNOTATE type=ANNOTYPE_RETPOLINE_SAFE |
| 117 | /* ANNOTATE_INSTR_BEGIN ANNOTATE type=ANNOTYPE_INSTR_BEGIN */ |
| 118 | /* ANNOTATE_INSTR_END ANNOTATE type=ANNOTYPE_INSTR_END */ |
| 119 | #define ANNOTATE_IGNORE_ALTERNATIVE ANNOTATE type=ANNOTYPE_IGNORE_ALTS |
| 120 | #define ANNOTATE_INTRA_FUNCTION_CALL ANNOTATE type=ANNOTYPE_INTRA_FUNCTION_CALL |
| 121 | #define ANNOTATE_UNRET_BEGIN ANNOTATE type=ANNOTYPE_UNRET_BEGIN |
| 122 | #define ANNOTATE_REACHABLE ANNOTATE type=ANNOTYPE_REACHABLE |
| 123 | #define ANNOTATE_NOCFI_SYM ANNOTATE type=ANNOTYPE_NOCFI |
| 124 | #define ANNOTATE_DATA_SPECIAL ANNOTATE_DATA type=ANNOTYPE_DATA_SPECIAL |
| 125 | #endif /* __ASSEMBLY__ */ |
| 126 | |
| 127 | #endif /* _LINUX_ANNOTATE_H */ |
| 128 | |