Skip to content

Commit 22467e2

Browse files
committed
Add function attribute "patchable-function-prefix" to support -fpatchable-function-entry=N,M where M>0
Similar to the function attribute `prefix` (prefix data), "patchable-function-prefix" inserts data (M NOPs) before the function entry label. -fpatchable-function-entry=2,1 (1 NOP before entry, 1 NOP after entry) will look like: ``` .type foo,@function .Ltmp0: # @foo nop foo: .Lfunc_begin0: # optional `bti c` (AArch64 Branch Target Identification) or # `endbr64` (Intel Indirect Branch Tracking) nop .section __patchable_function_entries,"awo",@progbits,get,unique,0 .p2align 3 .quad .Ltmp0 ``` -fpatchable-function-entry=N,0 + -mbranch-protection=bti/-fcf-protection=branch has two reasonable placements (https://gcc.gnu.org/ml/gcc-patches/2020-01/msg01185.html): ``` (a) (b) func: func: .Ltmp0: bti c bti c .Ltmp0: nop nop ``` (a) needs no additional code. If the consensus is to go for (b), we will need more code in AArch64BranchTargets.cpp / X86IndirectBranchTracking.cpp . Differential Revision: https://reviews.llvm.org/D73070
1 parent f394d22 commit 22467e2

File tree

8 files changed

+135
-29
lines changed

8 files changed

+135
-29
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class AsmPrinter : public MachineFunctionPass {
113113

114114
ProfileSummaryInfo *PSI;
115115

116+
/// The symbol for the entry in __patchable_function_entires.
117+
MCSymbol *CurrentPatchableFunctionEntrySym = nullptr;
118+
116119
/// The symbol for the current function. This is recalculated at the beginning
117120
/// of each call to runOnMachineFunction().
118121
MCSymbol *CurrentFnSym = nullptr;
@@ -449,6 +452,9 @@ class AsmPrinter : public MachineFunctionPass {
449452
/// instructions in verbose mode.
450453
virtual void emitImplicitDef(const MachineInstr *MI) const;
451454

455+
/// Emit N NOP instructions.
456+
void emitNops(unsigned N);
457+
452458
//===------------------------------------------------------------------===//
453459
// Symbol Lowering Routines.
454460
//===------------------------------------------------------------------===//

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,21 @@ void AsmPrinter::EmitFunctionHeader() {
706706
}
707707
}
708708

709+
// Emit M NOPs for -fpatchable-function-entry=N,M where M>0. We arbitrarily
710+
// place prefix data before NOPs.
711+
unsigned PatchableFunctionPrefix = 0;
712+
(void)F.getFnAttribute("patchable-function-prefix")
713+
.getValueAsString()
714+
.getAsInteger(10, PatchableFunctionPrefix);
715+
if (PatchableFunctionPrefix) {
716+
CurrentPatchableFunctionEntrySym =
717+
OutContext.createLinkerPrivateTempSymbol();
718+
OutStreamer->EmitLabel(CurrentPatchableFunctionEntrySym);
719+
emitNops(PatchableFunctionPrefix);
720+
} else {
721+
CurrentPatchableFunctionEntrySym = CurrentFnBegin;
722+
}
723+
709724
// Emit the function descriptor. This is a virtual function to allow targets
710725
// to emit their specific function descriptor.
711726
if (MAI->needsFunctionDescriptors())
@@ -1157,7 +1172,7 @@ void AsmPrinter::EmitFunctionBody() {
11571172
// unspecified.
11581173
if (Noop.getOpcode()) {
11591174
OutStreamer->AddComment("avoids zero-length function");
1160-
OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
1175+
emitNops(1);
11611176
}
11621177
}
11631178

@@ -2787,6 +2802,13 @@ void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
27872802
OS << Offset;
27882803
}
27892804

2805+
void AsmPrinter::emitNops(unsigned N) {
2806+
MCInst Nop;
2807+
MF->getSubtarget().getInstrInfo()->getNoop(Nop);
2808+
for (; N; --N)
2809+
EmitToStreamer(*OutStreamer, Nop);
2810+
}
2811+
27902812
//===----------------------------------------------------------------------===//
27912813
// Symbol Lowering Routines.
27922814
//===----------------------------------------------------------------------===//
@@ -3189,11 +3211,14 @@ void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI,
31893211

31903212
void AsmPrinter::emitPatchableFunctionEntries() {
31913213
const Function &F = MF->getFunction();
3192-
unsigned PatchableFunctionEntry = 0;
3214+
unsigned PatchableFunctionPrefix = 0, PatchableFunctionEntry = 0;
3215+
(void)F.getFnAttribute("patchable-function-prefix")
3216+
.getValueAsString()
3217+
.getAsInteger(10, PatchableFunctionPrefix);
31933218
(void)F.getFnAttribute("patchable-function-entry")
31943219
.getValueAsString()
31953220
.getAsInteger(10, PatchableFunctionEntry);
3196-
if (!PatchableFunctionEntry)
3221+
if (!PatchableFunctionPrefix && !PatchableFunctionEntry)
31973222
return;
31983223
const unsigned PointerSize = getPointerSize();
31993224
if (TM.getTargetTriple().isOSBinFormatELF()) {
@@ -3222,7 +3247,7 @@ void AsmPrinter::emitPatchableFunctionEntries() {
32223247
"__patchable_function_entries", ELF::SHT_PROGBITS, Flags));
32233248
}
32243249
EmitAlignment(Align(PointerSize));
3225-
OutStreamer->EmitSymbolValue(CurrentFnBegin, PointerSize);
3250+
OutStreamer->EmitSymbolValue(CurrentPatchableFunctionEntrySym, PointerSize);
32263251
}
32273252
}
32283253

llvm/lib/IR/Verifier.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,16 +1852,25 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
18521852
CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
18531853
}
18541854

1855+
if (Attrs.hasFnAttribute("patchable-function-prefix")) {
1856+
StringRef S = Attrs
1857+
.getAttribute(AttributeList::FunctionIndex,
1858+
"patchable-function-prefix")
1859+
.getValueAsString();
1860+
unsigned N;
1861+
if (S.getAsInteger(10, N))
1862+
CheckFailed(
1863+
"\"patchable-function-prefix\" takes an unsigned integer: " + S, V);
1864+
}
18551865
if (Attrs.hasFnAttribute("patchable-function-entry")) {
1856-
StringRef S0 = Attrs
1857-
.getAttribute(AttributeList::FunctionIndex,
1858-
"patchable-function-entry")
1859-
.getValueAsString();
1860-
StringRef S = S0;
1866+
StringRef S = Attrs
1867+
.getAttribute(AttributeList::FunctionIndex,
1868+
"patchable-function-entry")
1869+
.getValueAsString();
18611870
unsigned N;
18621871
if (S.getAsInteger(10, N))
18631872
CheckFailed(
1864-
"\"patchable-function-entry\" takes an unsigned integer: " + S0, V);
1873+
"\"patchable-function-entry\" takes an unsigned integer: " + S, V);
18651874
}
18661875
}
18671876

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ void AArch64AsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI)
250250
.getValueAsString()
251251
.getAsInteger(10, Num))
252252
return;
253-
for (; Num; --Num)
254-
EmitToStreamer(*OutStreamer, MCInstBuilder(AArch64::HINT).addImm(0));
253+
emitNops(Num);
255254
return;
256255
}
257256

llvm/lib/Target/ARM/ARMMCInstLower.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,7 @@ void ARMAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind)
207207
EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc).addImm(20)
208208
.addImm(ARMCC::AL).addReg(0));
209209

210-
MCInst Noop;
211-
Subtarget->getInstrInfo()->getNoop(Noop);
212-
for (int8_t I = 0; I < NoopsInSledCount; I++)
213-
OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
210+
emitNops(NoopsInSledCount);
214211

215212
OutStreamer->EmitLabel(Target);
216213
recordSled(CurSled, MI, Kind);
Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
; RUN: llc -mtriple=aarch64 %s -o - | FileCheck --check-prefixes=CHECK %s
22

3-
define i32 @f0() "patchable-function-entry"="0" "branch-target-enforcement" {
3+
define void @f0() "patchable-function-entry"="0" "branch-target-enforcement" {
44
; CHECK-LABEL: f0:
5-
; CHECK-NEXT: .Lfunc_begin0:
6-
; CHECK: %bb.0:
5+
; CHECK-NEXT: .Lfunc_begin0:
6+
; CHECK: // %bb.0:
77
; CHECK-NEXT: hint #34
8-
; CHECK-NEXT: mov w0, wzr
9-
; CHECK-NOT: .section __patchable_function_entries
10-
ret i32 0
8+
; CHECK-NEXT: ret
9+
; CHECK-NOT: .section __patchable_function_entries
10+
ret void
1111
}
1212

13-
define i32 @f1() "patchable-function-entry"="1" "branch-target-enforcement" {
13+
;; -fpatchable-function-entry=1 -mbranch-protection=bti
14+
define void @f1() "patchable-function-entry"="1" "branch-target-enforcement" {
1415
; CHECK-LABEL: f1:
15-
; CHECK-NEXT: .Lfunc_begin1:
16+
; CHECK-NEXT: .Lfunc_begin1:
1617
; CHECK: hint #34
1718
; CHECK-NEXT: nop
18-
; CHECK-NEXT: mov w0, wzr
19-
; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0
20-
; CHECK-NEXT: .p2align 3
21-
; CHECK-NEXT: .xword .Lfunc_begin1
22-
ret i32 0
19+
; CHECK-NEXT: ret
20+
; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0
21+
; CHECK-NEXT: .p2align 3
22+
; CHECK-NEXT: .xword .Lfunc_begin1
23+
ret void
24+
}
25+
26+
;; -fpatchable-function-entry=2,1 -mbranch-protection=bti
27+
define void @f2_1() "patchable-function-entry"="1" "patchable-function-prefix"="1" "branch-target-enforcement" {
28+
; CHECK-LABEL: .type f2_1,@function
29+
; CHECK-NEXT: .Ltmp0:
30+
; CHECK-NEXT: nop
31+
; CHECK-NEXT: f2_1:
32+
; CHECK-NEXT: .Lfunc_begin2:
33+
; CHECK: // %bb.0:
34+
; CHECK-NEXT: hint #34
35+
; CHECK-NEXT: nop
36+
; CHECK-NEXT: ret
37+
; CHECK: .Lfunc_end2:
38+
; CHECK-NEXT: .size f2_1, .Lfunc_end2-f2_1
39+
; CHECK: .section __patchable_function_entries,"awo",@progbits,f1,unique,0
40+
; CHECK-NEXT: .p2align 3
41+
; CHECK-NEXT: .xword .Ltmp0
42+
ret void
2343
}

llvm/test/CodeGen/AArch64/patchable-function-entry.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ define i32 @f1() "patchable-function-entry"="1" {
2525
ret i32 0
2626
}
2727

28+
;; Without -function-sections, f2 is in the same text section as f1.
29+
;; They share the __patchable_function_entries section.
30+
;; With -function-sections, f1 and f2 are in different text sections.
31+
;; Use separate __patchable_function_entries.
2832
define void @f2() "patchable-function-entry"="2" {
2933
; CHECK-LABEL: f2:
3034
; CHECK-NEXT: .Lfunc_begin2:
@@ -63,3 +67,39 @@ define void @f5() "patchable-function-entry"="5" comdat {
6367
%frame = alloca i8, i32 16
6468
ret void
6569
}
70+
71+
;; -fpatchable-function-entry=3,2
72+
;; "patchable-function-prefix" emits data before the function entry label.
73+
define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
74+
; CHECK-LABEL: .type f3_2,@function
75+
; CHECK-NEXT: .Ltmp1: // @f3_2
76+
; CHECK-NEXT: nop
77+
; CHECK-NEXT: nop
78+
; CHECK-NEXT: f3_2:
79+
; CHECK: // %bb.0:
80+
; CHECK-NEXT: nop
81+
; CHECK-NEXT: ret
82+
;; .size does not include the prefix.
83+
; CHECK: .Lfunc_end5:
84+
; CHECK-NEXT: .size f3_2, .Lfunc_end5-f3_2
85+
; NOFSECT .section __patchable_function_entries,"awo",@progbits,f1,unique,0
86+
; FSECT: .section __patchable_function_entries,"awo",@progbits,f3_2,unique,4
87+
; CHECK: .p2align 3
88+
; CHECK-NEXT: .xword .Ltmp1
89+
ret void
90+
}
91+
92+
;; When prefix data is used, arbitrarily place NOPs after prefix data.
93+
define void @prefix() "patchable-function-entry"="0" "patchable-function-prefix"="1" prefix i32 1 {
94+
; CHECK-LABEL: .type prefix,@function
95+
; CHECK-NEXT: .word 1 // @prefix
96+
; CHECK: .Ltmp2:
97+
; CHECK: nop
98+
; CHECK-NEXT: prefix:
99+
;; Emit a __patchable_function_entries entry even if "patchable-function-entry" is 0.
100+
; NOFSECT .section __patchable_function_entries,"awo",@progbits,prefix,unique,0
101+
; FSECT: .section __patchable_function_entries,"awo",@progbits,prefix,unique,5
102+
; CHECK: .p2align 3
103+
; CHECK-NEXT: .xword .Ltmp2
104+
ret void
105+
}

llvm/test/Verifier/invalid-patchable-function-entry.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ define void @f() "patchable-function-entry" { ret void }
99
define void @fa() "patchable-function-entry"="a" { ret void }
1010
define void @f_1() "patchable-function-entry"="-1" { ret void }
1111
define void @f3comma() "patchable-function-entry"="3," { ret void }
12+
13+
; CHECK: "patchable-function-prefix" takes an unsigned integer:
14+
; CHECK: "patchable-function-prefix" takes an unsigned integer: a
15+
; CHECK: "patchable-function-prefix" takes an unsigned integer: -1
16+
; CHECK: "patchable-function-prefix" takes an unsigned integer: 3,
17+
18+
define void @g() "patchable-function-prefix" { ret void }
19+
define void @ga() "patchable-function-prefix"="a" { ret void }
20+
define void @g_1() "patchable-function-prefix"="-1" { ret void }
21+
define void @g3comma() "patchable-function-prefix"="3," { ret void }

0 commit comments

Comments
 (0)