Skip to content

Commit 9d7f4eb

Browse files
committed
[PAC][ELF][AArch64] Support signed personality function pointer
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 8e4423e commit 9d7f4eb

14 files changed

+126
-9
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

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

12191219
if (getTriple().isOSLinux()) {
1220+
if (LangOpts.PointerAuthCalls)
1221+
getModule().addModuleFlag(llvm::Module::Min, "ptrauth-sign-personality",
1222+
1);
12201223
assert(getTriple().isOSBinFormatELF());
12211224
using namespace llvm::ELF;
12221225
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: 12 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,13 @@ 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+
if (Flag && Flag->getZExtValue() == 1)
70+
HasSignedPersonality = true;
71+
else
72+
HasSignedPersonality = false;
73+
}

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 9 additions & 1 deletion
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,6 +432,13 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(
431432
Streamer.emitELFSize(Label, E);
432433
Streamer.emitLabel(Label);
433434

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+
unsigned Size = DL.getPointerSize();
434442
Streamer.emitSymbolValue(Sym, Size);
435443
}
436444

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, true, getContext());
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+
MCContext &Ctx) {
43+
Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
44+
HasAddressDiversity, Ctx),
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: 7 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,12 @@ 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+
MCContext &Ctx);
47+
4148
/// Callback used to implement the .inst directive.
4249
virtual void emitInst(uint32_t Inst);
4350

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: llc -mtriple=aarch64-linux -filetype=asm %s -o - | FileCheck %s
2+
; RUN: llc -mtriple=aarch64-linux -filetype=obj %s -o - | \
3+
; RUN: llvm-readelf -r -x .data.DW.ref.__gxx_personality_v0 - | \
4+
; RUN: FileCheck --check-prefix=RELOC %s
5+
6+
@_ZTISt9exception = external constant ptr
7+
8+
define i32 @main() personality ptr @__gxx_personality_v0 {
9+
entry:
10+
invoke void @foo() to label %cont unwind label %lpad
11+
12+
lpad:
13+
%0 = landingpad { ptr, i32 }
14+
catch ptr null
15+
catch ptr @_ZTISt9exception
16+
ret i32 0
17+
18+
cont:
19+
ret i32 0
20+
}
21+
22+
declare i32 @__gxx_personality_v0(...)
23+
24+
declare void @foo()
25+
26+
!llvm.module.flags = !{!0}
27+
!0 = !{i32 8, !"ptrauth-sign-personality", i32 1}
28+
29+
; CHECK: DW.ref.__gxx_personality_v0:
30+
; CHECK-NEXT: .xword __gxx_personality_v0@AUTH(ia,32429,addr)
31+
32+
; RELOC: Relocation section '.rela.data.DW.ref.__gxx_personality_v0' at offset 0x2a0 contains 1 entries:
33+
; RELOC-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
34+
; RELOC-NEXT: 0000000000000000 0000000f00000244 R_AARCH64_AUTH_ABS64 0000000000000000 __gxx_personality_v0 + 0
35+
36+
; RELOC: Hex dump of section '.data.DW.ref.__gxx_personality_v0':
37+
; RELOC-NEXT: 0x00000000 00000000 ad7e0080
38+
; ^^^^ 0x7EAD = discriminator
39+
; ^^ 0b10000000: bit 63 = 1 -> address diversity enabled, bits 61:60 = 0b00 -> key is IA

0 commit comments

Comments
 (0)