Skip to content

Commit 4fb1cda

Browse files
authored
[PAC][ELF][AArch64] Support signed personality function pointer (#113148)
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 7fa5743 commit 4fb1cda

File tree

16 files changed

+176
-8
lines changed

16 files changed

+176
-8
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/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/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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- AArch64MachineModuleInfo.cpp ---------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// AArch64 Machine Module Info.
11+
///
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "AArch64MachineModuleInfo.h"
16+
#include "llvm/IR/Constants.h"
17+
#include "llvm/IR/Module.h"
18+
19+
namespace llvm {
20+
21+
AArch64MachineModuleInfo::AArch64MachineModuleInfo(const MachineModuleInfo &MMI)
22+
: MachineModuleInfoELF(MMI) {
23+
const Module *M = MMI.getModule();
24+
const auto *Flag = mdconst::extract_or_null<ConstantInt>(
25+
M->getModuleFlag("ptrauth-sign-personality"));
26+
if (Flag && Flag->getZExtValue() == 1)
27+
HasSignedPersonality = true;
28+
else
29+
HasSignedPersonality = false;
30+
}
31+
32+
} // end namespace llvm
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- AArch64MachineModuleInfo.h -----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// AArch64 Machine Module Info.
11+
///
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEMODULEINFO_H
16+
#define LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEMODULEINFO_H
17+
18+
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
19+
20+
namespace llvm {
21+
22+
class AArch64MachineModuleInfo final : public MachineModuleInfoELF {
23+
/// HasSignedPersonality is true if the corresponding IR module has the
24+
/// "ptrauth-sign-personality" flag set to 1.
25+
bool HasSignedPersonality = false;
26+
27+
public:
28+
AArch64MachineModuleInfo(const MachineModuleInfo &);
29+
30+
bool hasSignedPersonality() const { return HasSignedPersonality; }
31+
};
32+
33+
} // end namespace llvm
34+
35+
#endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEMODULEINFO_H

llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AArch64TargetObjectFile.h"
10+
#include "AArch64MachineModuleInfo.h"
1011
#include "AArch64TargetMachine.h"
1112
#include "MCTargetDesc/AArch64MCExpr.h"
13+
#include "MCTargetDesc/AArch64TargetStreamer.h"
1214
#include "llvm/BinaryFormat/Dwarf.h"
1315
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
1416
#include "llvm/IR/Mangler.h"
@@ -28,6 +30,21 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx,
2830
SupportDebugThreadLocalLocation = false;
2931
}
3032

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

0 commit comments

Comments
 (0)