diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h --- a/llvm/include/llvm/CodeGen/AsmPrinter.h +++ b/llvm/include/llvm/CodeGen/AsmPrinter.h @@ -113,6 +113,9 @@ ProfileSummaryInfo *PSI; + /// The symbol for the entry in __patchable_function_entires. + MCSymbol *CurrentPatchableFunctionEntrySym = nullptr; + /// The symbol for the current function. This is recalculated at the beginning /// of each call to runOnMachineFunction(). MCSymbol *CurrentFnSym = nullptr; @@ -449,6 +452,10 @@ /// instructions in verbose mode. virtual void emitImplicitDef(const MachineInstr *MI) const; + /// Used by -fpatchable-function-entry=_,N where N>0. Insert NOPs before the + /// function entry. + virtual void emitPatchableFunctionPrefix(int N) {} + //===------------------------------------------------------------------===// // Symbol Lowering Routines. //===------------------------------------------------------------------===// diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -706,6 +706,21 @@ } } + // Emit M NOPs for -fpatchable-function-entry=N,M where M>0. We arbitrarily + // place prefix data before NOPs. + unsigned PatchableFunctionPrefix = 0; + (void)F.getFnAttribute("patchable-function-prefix") + .getValueAsString() + .getAsInteger(10, PatchableFunctionPrefix); + if (PatchableFunctionPrefix) { + CurrentPatchableFunctionEntrySym = + OutContext.createLinkerPrivateTempSymbol(); + OutStreamer->EmitLabel(CurrentPatchableFunctionEntrySym); + emitPatchableFunctionPrefix(PatchableFunctionPrefix); + } else { + CurrentPatchableFunctionEntrySym = CurrentFnBegin; + } + // Emit the function descriptor. This is a virtual function to allow targets // to emit their specific function descriptor. if (MAI->needsFunctionDescriptors()) @@ -3222,7 +3237,7 @@ "__patchable_function_entries", ELF::SHT_PROGBITS, Flags)); } EmitAlignment(Align(PointerSize)); - OutStreamer->EmitSymbolValue(CurrentFnBegin, PointerSize); + OutStreamer->EmitSymbolValue(CurrentPatchableFunctionEntrySym, PointerSize); } } diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -1852,6 +1852,17 @@ CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V); } + if (Attrs.hasFnAttribute("patchable-function-prefix")) { + StringRef S0 = Attrs + .getAttribute(AttributeList::FunctionIndex, + "patchable-function-prefix") + .getValueAsString(); + StringRef S = S0; + unsigned N; + if (S.getAsInteger(10, N)) + CheckFailed( + "\"patchable-function-prefix\" takes an unsigned integer: " + S0, V); + } if (Attrs.hasFnAttribute("patchable-function-entry")) { StringRef S0 = Attrs .getAttribute(AttributeList::FunctionIndex, diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp --- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp +++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp @@ -113,6 +113,7 @@ const MachineInstr *MI); void EmitInstruction(const MachineInstr *MI) override; + void emitPatchableFunctionPrefix(int N) override; void getAnalysisUsage(AnalysisUsage &AU) const override { AsmPrinter::getAnalysisUsage(AU); @@ -1297,6 +1298,11 @@ EmitToStreamer(*OutStreamer, TmpInst); } +void AArch64AsmPrinter::emitPatchableFunctionPrefix(int N) { + while (N--) + EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::HINT).addImm(0)); +} + // Force static initialization. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64AsmPrinter() { RegisterAsmPrinter X(getTheAArch64leTarget()); diff --git a/llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll b/llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll --- a/llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll +++ b/llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll @@ -10,6 +10,7 @@ ret i32 0 } +;; -fpatchable-function-entry=1 -mbranch-protection=bti define i32 @f1() "patchable-function-entry"="1" "branch-target-enforcement" { ; CHECK-LABEL: f1: ; CHECK-NEXT: .Lfunc_begin1: @@ -21,3 +22,21 @@ ; CHECK-NEXT: .xword .Lfunc_begin1 ret i32 0 } + +;; -fpatchable-function-entry=2,1 -mbranch-protection=bti +define i32 @f1_1() "patchable-function-entry"="1" "patchable-function-prefix"="1" "branch-target-enforcement" { +; CHECK-LABEL: .type f1_1,@function +; CHECK-NEXT: .Ltmp0: +; CHECK-NEXT: nop +; CHECK-NEXT: f1_1: +; CHECK-NEXT: .Lfunc_begin2: +; CHECK: // %bb.0: +; CHECK-NEXT: hint #34 +; CHECK-NEXT: nop +; CHECK-NEXT: mov w0, wzr +; CHECK: .size f1_1, .Lfunc_end2-f1_1 +; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0 +; CHECK-NEXT: .p2align 3 +; CHECK-NEXT: .xword .Ltmp0 + ret i32 0 +} diff --git a/llvm/test/CodeGen/AArch64/patchable-function-entry.ll b/llvm/test/CodeGen/AArch64/patchable-function-entry.ll --- a/llvm/test/CodeGen/AArch64/patchable-function-entry.ll +++ b/llvm/test/CodeGen/AArch64/patchable-function-entry.ll @@ -63,3 +63,31 @@ %frame = alloca i8, i32 16 ret void } + +;; -fpatchable-function-entry=3,2 +;; "patchable-function-prefix" emits data before the function entry label. +define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" { +; CHECK-LABEL: .type f3_2,@function +; CHECK-NEXT: .Ltmp1: +; CHECK-NEXT: nop +; CHECK-NEXT: nop +; CHECK-NEXT: f3_2: +; CHECK: // %bb.0: +; CHECK-NEXT: nop +; CHECK-NEXT: ret +;; .size does not include the prefix. +; CHECK: .size f3_2, .Lfunc_end5-f3_2 +; NOFSECT .section __patchable_function_entries,"awo",@progbits,f1,unique,0 +; FSECT: .section __patchable_function_entries,"awo",@progbits,f3_2,unique,4 +; CHECK: .p2align 3 +; CHECK-NEXT: .xword .Ltmp1 + ret void +} + +;; When prefix data is used, arbitrarily place NOPs after prefix data. +define void @prefix() "patchable-function-entry"="0" "patchable-function-prefix"="1" prefix i32 1 { +; CHECK-LABEL: .type prefix,@function +; CHECK-NEXT: .word 1 // @prefix +; CHECK: nop + ret void +} diff --git a/llvm/test/Verifier/invalid-patchable-function-entry.ll b/llvm/test/Verifier/invalid-patchable-function-entry.ll --- a/llvm/test/Verifier/invalid-patchable-function-entry.ll +++ b/llvm/test/Verifier/invalid-patchable-function-entry.ll @@ -9,3 +9,13 @@ define void @fa() "patchable-function-entry"="a" { ret void } define void @f_1() "patchable-function-entry"="-1" { ret void } define void @f3comma() "patchable-function-entry"="3," { ret void } + +; CHECK: "patchable-function-prefix" takes an unsigned integer: +; CHECK: "patchable-function-prefix" takes an unsigned integer: a +; CHECK: "patchable-function-prefix" takes an unsigned integer: -1 +; CHECK: "patchable-function-prefix" takes an unsigned integer: 3, + +define void @g() "patchable-function-prefix" { ret void } +define void @ga() "patchable-function-prefix"="a" { ret void } +define void @g_1() "patchable-function-prefix"="-1" { ret void } +define void @g3comma() "patchable-function-prefix"="3," { ret void }