Skip to content

Commit f65a21a

Browse files
authored
[PAC][ELF][AArch64] Support signed personality function pointer (#119361)
Re-apply #113148 after revert in #119331 If function pointer signing is enabled, sign personality function pointer stored in `.DW.ref.__gxx_personality_v0` section with IA key, 0x7EAD = `ptrauth_string_discriminator("personality")` constant discriminator and address diversity enabled.
1 parent 54dac27 commit f65a21a

14 files changed

+156
-10
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,9 @@ void CodeGenModule::Release() {
12181218
getModule().addModuleFlag(llvm::Module::Min, "ptrauth-elf-got", 1);
12191219

12201220
if (getTriple().isOSLinux()) {
1221+
if (LangOpts.PointerAuthCalls)
1222+
getModule().addModuleFlag(llvm::Module::Min, "ptrauth-sign-personality",
1223+
1);
12211224
assert(getTriple().isOSBinFormatELF());
12221225
using namespace llvm::ELF;
12231226
uint64_t PAuthABIVersion =
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=OFF
22
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-elf-got -emit-llvm %s -o - | FileCheck %s --check-prefix=ELFGOT
3+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-calls -emit-llvm %s -o - | FileCheck %s --check-prefix=PERSONALITY
34

45
// ELFGOT: !llvm.module.flags = !{
56
// ELFGOT-SAME: !1
67
// ELFGOT: !1 = !{i32 8, !"ptrauth-elf-got", i32 1}
78

9+
// PERSONALITY: !llvm.module.flags = !{
10+
// PERSONALITY-SAME: !1
11+
// PERSONALITY: !1 = !{i32 8, !"ptrauth-sign-personality", i32 1}
12+
813
// OFF-NOT: "ptrauth-

llvm/include/llvm/CodeGen/MachineModuleInfoImpls.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ class MachineModuleInfoELF : public MachineModuleInfoImpl {
8383
/// extern_weak symbols.
8484
DenseMap<MCSymbol *, const MCExpr *> AuthPtrStubs;
8585

86+
/// HasSignedPersonality is true if the corresponding IR module has the
87+
/// "ptrauth-sign-personality" flag set to 1.
88+
bool HasSignedPersonality = false;
89+
8690
virtual void anchor(); // Out of line virtual method.
8791

8892
public:
89-
MachineModuleInfoELF(const MachineModuleInfo &) {}
93+
MachineModuleInfoELF(const MachineModuleInfo &);
9094

9195
StubValueTy &getGVStubEntry(MCSymbol *Sym) {
9296
assert(Sym && "Key cannot be null");
@@ -105,6 +109,8 @@ class MachineModuleInfoELF : public MachineModuleInfoImpl {
105109
ExprStubListTy getAuthGVStubList() {
106110
return getSortedExprStubs(AuthPtrStubs);
107111
}
112+
113+
bool hasSignedPersonality() const { return HasSignedPersonality; }
108114
};
109115

110116
/// MachineModuleInfoCOFF - This is a MachineModuleInfoImpl implementation

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
5252
void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;
5353

5454
void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL,
55-
const MCSymbol *Sym) const override;
55+
const MCSymbol *Sym,
56+
const MachineModuleInfo *MMI) const override;
57+
58+
virtual void emitPersonalityValueImpl(MCStreamer &Streamer,
59+
const DataLayout &DL,
60+
const MCSymbol *Sym,
61+
const MachineModuleInfo *MMI) const;
5662

5763
/// Given a constant with the SectionKind, return a section that it should be
5864
/// placed in.

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
8282
virtual void Initialize(MCContext &ctx, const TargetMachine &TM);
8383

8484
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
85-
const MCSymbol *Sym) const;
85+
const MCSymbol *Sym,
86+
const MachineModuleInfo *MMI) const;
8687

8788
/// Emit the module-level metadata that the platform cares about.
8889
virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void DwarfCFIException::endModule() {
5050
// Emit indirect reference table for all used personality functions
5151
for (const GlobalValue *Personality : Personalities) {
5252
MCSymbol *Sym = Asm->getSymbol(Personality);
53-
TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
53+
TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym,
54+
Asm->MMI);
5455
}
5556
Personalities.clear();
5657
}

llvm/lib/CodeGen/MachineModuleInfoImpls.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
1515
#include "llvm/ADT/DenseMap.h"
1616
#include "llvm/ADT/STLExtras.h"
17+
#include "llvm/IR/Constants.h"
18+
#include "llvm/IR/Module.h"
1719
#include "llvm/MC/MCSymbol.h"
1820

1921
using namespace llvm;
@@ -59,3 +61,10 @@ MachineModuleInfoImpl::ExprStubListTy MachineModuleInfoImpl::getSortedExprStubs(
5961
ExprStubs.clear();
6062
return List;
6163
}
64+
65+
MachineModuleInfoELF::MachineModuleInfoELF(const MachineModuleInfo &MMI) {
66+
const Module *M = MMI.getModule();
67+
const auto *Flag = mdconst::extract_or_null<ConstantInt>(
68+
M->getModuleFlag("ptrauth-sign-personality"));
69+
HasSignedPersonality = Flag && Flag->getZExtValue() == 1;
70+
}

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
413413
}
414414

415415
void TargetLoweringObjectFileELF::emitPersonalityValue(
416-
MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
416+
MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
417+
const MachineModuleInfo *MMI) const {
417418
SmallString<64> NameData("DW.ref.");
418419
NameData += Sym->getName();
419420
MCSymbolELF *Label =
@@ -431,7 +432,13 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(
431432
Streamer.emitELFSize(Label, E);
432433
Streamer.emitLabel(Label);
433434

434-
Streamer.emitSymbolValue(Sym, Size);
435+
emitPersonalityValueImpl(Streamer, DL, Sym, MMI);
436+
}
437+
438+
void TargetLoweringObjectFileELF::emitPersonalityValueImpl(
439+
MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
440+
const MachineModuleInfo *MMI) const {
441+
Streamer.emitSymbolValue(Sym, DL.getPointerSize());
435442
}
436443

437444
const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(

llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "AArch64TargetObjectFile.h"
1010
#include "AArch64TargetMachine.h"
1111
#include "MCTargetDesc/AArch64MCExpr.h"
12+
#include "MCTargetDesc/AArch64TargetStreamer.h"
1213
#include "llvm/BinaryFormat/Dwarf.h"
1314
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
1415
#include "llvm/IR/Mangler.h"
@@ -28,6 +29,21 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
2829
SupportDebugThreadLocalLocation = false;
2930
}
3031

32+
void AArch64_ELFTargetObjectFile::emitPersonalityValueImpl(
33+
MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym,
34+
const MachineModuleInfo *MMI) const {
35+
if (!MMI->getObjFileInfo<MachineModuleInfoELF>().hasSignedPersonality()) {
36+
TargetLoweringObjectFileELF::emitPersonalityValueImpl(Streamer, DL, Sym,
37+
MMI);
38+
return;
39+
}
40+
auto *TS = static_cast<AArch64TargetStreamer *>(Streamer.getTargetStreamer());
41+
// The value is ptrauth_string_discriminator("personality")
42+
constexpr uint16_t Discriminator = 0x7EAD;
43+
TS->emitAuthValue(MCSymbolRefExpr::create(Sym, getContext()), Discriminator,
44+
AArch64PACKey::IA, /*HasAddressDiversity=*/true);
45+
}
46+
3147
const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
3248
const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
3349
int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {

llvm/lib/Target/AArch64/AArch64TargetObjectFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
3535
MachineModuleInfo *MMI, const MCSymbol *RawSym,
3636
AArch64PACKey::ID Key,
3737
uint16_t Discriminator) const;
38+
39+
void emitPersonalityValueImpl(MCStreamer &Streamer, const DataLayout &DL,
40+
const MCSymbol *Sym,
41+
const MachineModuleInfo *MMI) const override;
3842
};
3943

4044
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.

llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ AArch64TargetStreamer::AArch64TargetStreamer(MCStreamer &S)
3535

3636
AArch64TargetStreamer::~AArch64TargetStreamer() = default;
3737

38+
void AArch64TargetStreamer::emitAuthValue(const MCExpr *Expr,
39+
uint16_t Discriminator,
40+
AArch64PACKey::ID Key,
41+
bool HasAddressDiversity) {
42+
Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
43+
HasAddressDiversity,
44+
Streamer.getContext()),
45+
8);
46+
}
47+
3848
// The constant pool handling is shared by all AArch64TargetStreamer
3949
// implementations.
4050
const MCExpr *AArch64TargetStreamer::addConstantPoolEntry(const MCExpr *Expr,

llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64TARGETSTREAMER_H
1010
#define LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64TARGETSTREAMER_H
1111

12+
#include "AArch64MCExpr.h"
1213
#include "llvm/MC/MCStreamer.h"
1314

1415
namespace {
@@ -38,6 +39,11 @@ class AArch64TargetStreamer : public MCTargetStreamer {
3839
void emitNoteSection(unsigned Flags, uint64_t PAuthABIPlatform = -1,
3940
uint64_t PAuthABIVersion = -1);
4041

42+
/// Callback used to emit AUTH expressions (e.g. signed
43+
/// personality function pointer).
44+
void emitAuthValue(const MCExpr *Expr, uint16_t Discriminator,
45+
AArch64PACKey::ID Key, bool HasAddressDiversity);
46+
4147
/// Callback used to implement the .inst directive.
4248
virtual void emitInst(uint32_t Inst);
4349

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
141141
return TM.getSymbol(GV);
142142
}
143143

144-
void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
145-
const DataLayout &,
146-
const MCSymbol *Sym) const {
147-
}
144+
void TargetLoweringObjectFile::emitPersonalityValue(
145+
MCStreamer &Streamer, const DataLayout &, const MCSymbol *Sym,
146+
const MachineModuleInfo *MMI) const {}
148147

149148
void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer &Streamer,
150149
Module &M) const {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; RUN: rm -rf %t && split-file %s %t && cd %t
2+
; RUN: cat common.ll authflag.ll > auth.ll
3+
; RUN: cat common.ll noauthflag.ll > noauth1.ll
4+
; RUN: cat common.ll > noauth2.ll
5+
6+
; RUN: llc -mtriple=aarch64-linux -filetype=asm auth.ll -o - | \
7+
; RUN: FileCheck --check-prefix=AUTH-ASM %s
8+
; RUN: llc -mtriple=aarch64-linux -filetype=obj auth.ll -o - | \
9+
; RUN: llvm-readelf -r -x .data.DW.ref.__gxx_personality_v0 - | \
10+
; RUN: FileCheck --check-prefix=AUTH-RELOC %s
11+
12+
; AUTH-ASM: DW.ref.__gxx_personality_v0:
13+
; AUTH-ASM-NEXT: .xword __gxx_personality_v0@AUTH(ia,32429,addr)
14+
15+
; AUTH-RELOC: Relocation section '.rela.data.DW.ref.__gxx_personality_v0' at offset 0x2a0 contains 1 entries:
16+
; AUTH-RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
17+
; AUTH-RELOC-NEXT: 0000000000000000 0000000f00000244 R_AARCH64_AUTH_ABS64 0000000000000000 __gxx_personality_v0 + 0
18+
19+
; AUTH-RELOC: Hex dump of section '.data.DW.ref.__gxx_personality_v0':
20+
; AUTH-RELOC-NEXT: 0x00000000 00000000 ad7e0080
21+
; ^^^^ 0x7EAD = discriminator
22+
; ^^ 0b10000000: bit 63 = 1 -> address diversity enabled, bits 61:60 = 0b00 -> key is IA
23+
24+
; RUN: llc -mtriple=aarch64-linux -filetype=asm noauth1.ll -o - | \
25+
; RUN: FileCheck --check-prefix=NOAUTH-ASM %s
26+
; RUN: llc -mtriple=aarch64-linux -filetype=obj noauth1.ll -o - | \
27+
; RUN: llvm-readelf -r -x .data.DW.ref.__gxx_personality_v0 - | \
28+
; RUN: FileCheck --check-prefix=NOAUTH-RELOC %s
29+
30+
; RUN: llc -mtriple=aarch64-linux -filetype=asm noauth2.ll -o - | \
31+
; RUN: FileCheck --check-prefix=NOAUTH-ASM %s
32+
; RUN: llc -mtriple=aarch64-linux -filetype=obj noauth2.ll -o - | \
33+
; RUN: llvm-readelf -r -x .data.DW.ref.__gxx_personality_v0 - | \
34+
; RUN: FileCheck --check-prefix=NOAUTH-RELOC %s
35+
36+
; NOAUTH-ASM: DW.ref.__gxx_personality_v0:
37+
; NOAUTH-ASM-NEXT: .xword __gxx_personality_v0{{$}}
38+
39+
; NOAUTH-RELOC: Relocation section '.rela.data.DW.ref.__gxx_personality_v0' at offset 0x2a0 contains 1 entries:
40+
; NOAUTH-RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
41+
; NOAUTH-RELOC-NEXT: 0000000000000000 0000000f00000101 R_AARCH64_ABS64 0000000000000000 __gxx_personality_v0 + 0
42+
43+
; NOAUTH-RELOC: Hex dump of section '.data.DW.ref.__gxx_personality_v0':
44+
; NOAUTH-RELOC-NEXT: 0x00000000 00000000 00000000
45+
46+
;--- common.ll
47+
@_ZTISt9exception = external constant ptr
48+
49+
define i32 @main() personality ptr @__gxx_personality_v0 {
50+
entry:
51+
invoke void @foo() to label %cont unwind label %lpad
52+
53+
lpad:
54+
%0 = landingpad { ptr, i32 }
55+
catch ptr null
56+
catch ptr @_ZTISt9exception
57+
ret i32 0
58+
59+
cont:
60+
ret i32 0
61+
}
62+
63+
declare i32 @__gxx_personality_v0(...)
64+
65+
declare void @foo()
66+
67+
;--- authflag.ll
68+
!llvm.module.flags = !{!0}
69+
!0 = !{i32 8, !"ptrauth-sign-personality", i32 1}
70+
71+
;--- noauthflag.ll
72+
!llvm.module.flags = !{!0}
73+
!0 = !{i32 8, !"ptrauth-sign-personality", i32 0}

0 commit comments

Comments
 (0)