[llvm] [PAC][ELF] Place AUTH constants in .data.rel.ro for position dependent code (PR #150418)
Daniil Kovalev via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 14 13:24:50 PST 2026
https://github.com/kovdan01 updated https://github.com/llvm/llvm-project/pull/150418
>From a91b0eeed5681efdc27bb84d4c208b7bb18f1950 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Thu, 24 Jul 2025 16:21:02 +0300
Subject: [PATCH 1/9] [PAC][ELF] Place AUTH constants in .data.rel.ro for
position dependent code
For AArch64 target machine, effective relocation model on Windows and
Darwin is always PIC, while for ELF targets Static is used when DynamicNoPIC
is requested (see `getEffectiveRelocModel` in AArch64TargetMachine.cpp).
This resulted in using .rodata section for AUTH constants, which is
wrong since these are filled with AUTH dynamic relocs and require the
section to be writeable during dynamic relocation resolving.
This patch adds a check ensuring if the constant itself or one of the nested
constants are AUTH ones. If so, use .data.rel.ro section.
---
llvm/lib/Target/TargetLoweringObjectFile.cpp | 22 ++++++--
.../CodeGen/AArch64/ptrauth-global-no-pic.ll | 51 +++++++++++++++++++
2 files changed, 70 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index 9b03e85ca45bf..675d74adee6d4 100644
--- a/llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -17,6 +17,7 @@
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
@@ -220,6 +221,20 @@ void TargetLoweringObjectFile::emitPseudoProbeDescMetadata(MCStreamer &Streamer,
}
}
+static bool containsConstantPtrAuth(const Constant *C) {
+ if (isa<ConstantPtrAuth>(C))
+ return true;
+
+ if (isa<BlockAddress>(C) || isa<GlobalValue>(C))
+ return false;
+
+ for (const Value *Op : C->operands())
+ if (containsConstantPtrAuth(cast<Constant>(Op)))
+ return true;
+
+ return false;
+}
+
/// getKindForGlobal - This is a top-level target-independent classifier for
/// a global object. Given a global variable and information from the TM, this
/// function classifies the global in a target independent manner. This function
@@ -327,9 +342,10 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
// mergable section, because the linker doesn't take relocations into
// consideration when it tries to merge entries in the section.
Reloc::Model ReloModel = TM.getRelocationModel();
- if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
- ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
- !C->needsDynamicRelocation())
+ if ((ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
+ ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
+ !C->needsDynamicRelocation()) &&
+ !containsConstantPtrAuth(C))
return SectionKind::getReadOnly();
// Otherwise, the dynamic linker needs to fix it up, put it in the
diff --git a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
new file mode 100644
index 0000000000000..651f384ef6497
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
@@ -0,0 +1,51 @@
+; RUN: llc -mtriple aarch64-elf --relocation-model=static -mattr=+pauth -filetype=asm -o - %s | FileCheck %s
+; RUN: llc -mtriple aarch64-elf --relocation-model=dynamic-no-pic -mattr=+pauth -filetype=asm -o - %s | FileCheck %s
+
+;; A constant value, use .rodata
+; CHECK: .section .rodata,"a", at progbits
+; CHECK: .globl Const
+; CHECK: Const:
+; CHECK: .xword 37
+
+;; An AUTH reloc is needed, use .data.rel.ro
+; CHECK: .section .data.rel.ro,"aw", at progbits
+; CHECK: .globl PtrAuthExtern
+; CHECK: PtrAuthExtern:
+; CHECK: .xword ConstExtern at AUTH(da,0)
+
+; CHECK: .globl PtrAuth
+; CHECK: PtrAuth:
+; CHECK: .xword Const at AUTH(da,0)
+
+; CHECK: .globl PtrAuthExternNested1
+; CHECK: PtrAuthExternNested1:
+; CHECK: .xword ConstExtern at AUTH(da,0)
+
+;; The address could be filled statically, use .rodata
+; CHECK: .section .rodata,"a", at progbits
+; CHECK: .globl PtrAuthExternNested2
+; CHECK: PtrAuthExternNested2:
+; CHECK: .xword PtrAuthExtern
+
+;; An AUTH reloc is needed, use .data.rel.ro
+; CHECK: .section .data.rel.ro,"aw", at progbits
+; CHECK: .globl PtrAuthNested1
+; CHECK: PtrAuthNested1:
+; CHECK: .xword Const at AUTH(da,0)
+
+;; The address could be filled statically, use .rodata
+; CHECK: .section .rodata,"a", at progbits
+; CHECK: .globl PtrAuthNested2
+; CHECK: PtrAuthNested2:
+; CHECK: .xword PtrAuth
+
+ at ConstExtern = external global i64
+ at Const = constant i64 37
+
+ at PtrAuthExtern = constant ptr ptrauth (ptr @ConstExtern, i32 2)
+ at PtrAuth = constant ptr ptrauth (ptr @Const, i32 2)
+
+ at PtrAuthExternNested1 = constant { ptr } { ptr ptrauth (ptr @ConstExtern, i32 2) }
+ at PtrAuthExternNested2 = constant { ptr } { ptr @PtrAuthExtern }
+ at PtrAuthNested1 = constant { ptr } { ptr ptrauth (ptr @Const, i32 2) }
+ at PtrAuthNested2 = constant { ptr } { ptr @PtrAuth }
>From d85f8cfd737cbd5e3b1777c4ae1a6317a1436370 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Fri, 5 Sep 2025 10:54:12 +0300
Subject: [PATCH 2/9] Address review comments
---
llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
index 651f384ef6497..fbcbcb2a84b54 100644
--- a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
+++ b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
@@ -1,5 +1,5 @@
-; RUN: llc -mtriple aarch64-elf --relocation-model=static -mattr=+pauth -filetype=asm -o - %s | FileCheck %s
-; RUN: llc -mtriple aarch64-elf --relocation-model=dynamic-no-pic -mattr=+pauth -filetype=asm -o - %s | FileCheck %s
+; RUN: llc -mtriple aarch64-elf --relocation-model=static -mattr=+pauth < %s | FileCheck %s
+; RUN: llc -mtriple aarch64-elf --relocation-model=dynamic-no-pic -mattr=+pauth < %s | FileCheck %s
;; A constant value, use .rodata
; CHECK: .section .rodata,"a", at progbits
>From cafc9df143ea6d302994111a7e1882f346540d80 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Sun, 19 Oct 2025 22:24:57 +0300
Subject: [PATCH 3/9] Apply Anatoly's suggestion
---
llvm/lib/Target/TargetLoweringObjectFile.cpp | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index 83e0c56fcb48d..ae31cd90b37ab 100644
--- a/llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -342,16 +342,19 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
}
} else {
+ // The dynamic linker always needs to fix PtrAuth relocations up.
+ if (containsConstantPtrAuth(C))
+ return SectionKind::getReadOnlyWithRel();
+
// In static, ROPI and RWPI relocation models, the linker will resolve
// all addresses, so the relocation entries will actually be constants by
// the time the app starts up. However, we can't put this into a
// mergable section, because the linker doesn't take relocations into
// consideration when it tries to merge entries in the section.
Reloc::Model ReloModel = TM.getRelocationModel();
- if ((ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
- ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
- !C->needsDynamicRelocation()) &&
- !containsConstantPtrAuth(C))
+ if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
+ ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
+ !C->needsDynamicRelocation())
return SectionKind::getReadOnly();
// Otherwise, the dynamic linker needs to fix it up, put it in the
>From 115e2b18b081827b236b23f2dd9f16f645e91573 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Fri, 19 Dec 2025 17:50:22 +0300
Subject: [PATCH 4/9] Keep pauth constants in .rodata when using IRelative
---
.../llvm/Target/TargetLoweringObjectFile.h | 4 +++
llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp | 27 ++++---------------
.../AArch64/AArch64TargetObjectFile.cpp | 23 ++++++++++++++++
.../Target/AArch64/AArch64TargetObjectFile.h | 2 ++
llvm/lib/Target/TargetLoweringObjectFile.cpp | 12 +++++----
5 files changed, 41 insertions(+), 27 deletions(-)
diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 06508bf4d50af..3266e3f23e713 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -305,6 +305,10 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
return nullptr;
}
+ virtual bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const {
+ return false;
+ }
+
protected:
virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
SectionKind Kind,
diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index e11bf436b84fa..e1c2c4713c4fd 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -2419,16 +2419,6 @@ void AArch64AsmPrinter::emitAddress(MCRegister Reg, const MCExpr *Expr,
}
}
-static bool targetSupportsIRelativeRelocation(const Triple &TT) {
- // IFUNCs are ELF-only.
- if (!TT.isOSBinFormatELF())
- return false;
-
- // IFUNCs are supported on glibc, bionic, and some but not all of the BSDs.
- return TT.isOSGlibc() || TT.isAndroid() || TT.isOSFreeBSD() ||
- TT.isOSDragonFly() || TT.isOSNetBSD();
-}
-
// Emit an ifunc resolver that returns a signed pointer to the specified target,
// and return a FUNCINIT reference to the resolver. In the linked binary, this
// function becomes the target of an IRELATIVE relocation. This resolver is used
@@ -2485,14 +2475,6 @@ const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr) {
const Triple &TT = TM.getTargetTriple();
- // We only emit an IRELATIVE relocation if the target supports IRELATIVE.
- if (!targetSupportsIRelativeRelocation(TT))
- return nullptr;
-
- // For now, only the DA key is supported.
- if (KeyID != AArch64PACKey::DA)
- return nullptr;
-
std::unique_ptr<MCSubtargetInfo> STI(
TM.getTarget().createMCSubtargetInfo(TT, "", ""));
assert(STI && "Unable to create subtarget info");
@@ -2617,10 +2599,11 @@ AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
uint64_t Disc = CPA.getDiscriminator()->getZExtValue();
// Check if we can represent this with an IRELATIVE and emit it if so.
- if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
- Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
- BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
- return IFuncSym;
+ if (TM.getObjFileLowering()->canEmitConstantPtrAuthAsIRelative(&CPA))
+ if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
+ Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
+ BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
+ return IFuncSym;
if (!isUInt<16>(Disc)) {
CPA.getContext().emitError("AArch64 PAC Discriminator '" + Twine(Disc) +
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 85de2d5010286..807ab23af6344 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -186,3 +186,26 @@ MCSection *AArch64_ELFTargetObjectFile::SelectSectionForGlobal(
return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
}
+
+static bool targetSupportsIRelativeRelocation(const Triple &TT) {
+ // IFUNCs are ELF-only.
+ if (!TT.isOSBinFormatELF())
+ return false;
+
+ // IFUNCs are supported on glibc, bionic, and some but not all of the BSDs.
+ return TT.isOSGlibc() || TT.isAndroid() || TT.isOSFreeBSD() ||
+ TT.isOSDragonFly() || TT.isOSNetBSD();
+}
+
+bool AArch64_ELFTargetObjectFile::canEmitConstantPtrAuthAsIRelative(
+ const Constant *CPA) const {
+ // We only emit an IRELATIVE relocation if the target supports IRELATIVE.
+ if (!targetSupportsIRelativeRelocation(TM->getTargetTriple()))
+ return false;
+
+ // For now, only the DA key is supported.
+ if (cast<ConstantPtrAuth>(CPA)->getKey()->getZExtValue() != AArch64PACKey::DA)
+ return false;
+
+ return true;
+}
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index 6b3381452c70b..8c36014cd7f9b 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -40,6 +40,8 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
const TargetMachine &TM) const override;
+
+ bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const override;
};
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index ae31cd90b37ab..c247b42647a9f 100644
--- a/llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -227,15 +227,17 @@ void TargetLoweringObjectFile::emitPseudoProbeDescMetadata(
}
}
-static bool containsConstantPtrAuth(const Constant *C) {
- if (isa<ConstantPtrAuth>(C))
- return true;
+static bool needsPtrAuthRelocation(const Constant *C,
+ const TargetLoweringObjectFile *TLOF) {
+ if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C))
+ if (!TLOF->canEmitConstantPtrAuthAsIRelative(CPA))
+ return true;
if (isa<BlockAddress>(C) || isa<GlobalValue>(C))
return false;
for (const Value *Op : C->operands())
- if (containsConstantPtrAuth(cast<Constant>(Op)))
+ if (needsPtrAuthRelocation(cast<Constant>(Op), TLOF))
return true;
return false;
@@ -343,7 +345,7 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
} else {
// The dynamic linker always needs to fix PtrAuth relocations up.
- if (containsConstantPtrAuth(C))
+ if (needsPtrAuthRelocation(C, TM.getObjFileLowering()))
return SectionKind::getReadOnlyWithRel();
// In static, ROPI and RWPI relocation models, the linker will resolve
>From f048dd750a56db8ef5275d42cf56bbf35dd3975c Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Fri, 19 Dec 2025 18:06:09 +0300
Subject: [PATCH 5/9] Address review comments
---
llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll | 2 ++
1 file changed, 2 insertions(+)
diff --git a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
index fbcbcb2a84b54..77250a25f3f7c 100644
--- a/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
+++ b/llvm/test/CodeGen/AArch64/ptrauth-global-no-pic.ll
@@ -13,10 +13,12 @@
; CHECK: PtrAuthExtern:
; CHECK: .xword ConstExtern at AUTH(da,0)
+; CHECK-NOT: .section
; CHECK: .globl PtrAuth
; CHECK: PtrAuth:
; CHECK: .xword Const at AUTH(da,0)
+; CHECK-NOT: .section
; CHECK: .globl PtrAuthExternNested1
; CHECK: PtrAuthExternNested1:
; CHECK: .xword ConstExtern at AUTH(da,0)
>From f18ee95da377eec2569c50e2d20a8dcca88ac124 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Tue, 23 Dec 2025 23:53:01 +0300
Subject: [PATCH 6/9] Address review comments
---
llvm/include/llvm/Target/TargetLoweringObjectFile.h | 4 +++-
llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp | 4 ++--
llvm/lib/Target/AArch64/AArch64TargetObjectFile.h | 3 ++-
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 3266e3f23e713..8c6f29d6c0d75 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -24,6 +24,7 @@ namespace llvm {
struct Align;
struct MachineJumpTableEntry;
class Constant;
+class ConstantPtrAuth;
class DataLayout;
class Function;
class GlobalObject;
@@ -305,7 +306,8 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
return nullptr;
}
- virtual bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const {
+ virtual bool
+ canEmitConstantPtrAuthAsIRelative(const ConstantPtrAuth *CPA) const {
return false;
}
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 807ab23af6344..54736b596a0a5 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -198,13 +198,13 @@ static bool targetSupportsIRelativeRelocation(const Triple &TT) {
}
bool AArch64_ELFTargetObjectFile::canEmitConstantPtrAuthAsIRelative(
- const Constant *CPA) const {
+ const ConstantPtrAuth *CPA) const {
// We only emit an IRELATIVE relocation if the target supports IRELATIVE.
if (!targetSupportsIRelativeRelocation(TM->getTargetTriple()))
return false;
// For now, only the DA key is supported.
- if (cast<ConstantPtrAuth>(CPA)->getKey()->getZExtValue() != AArch64PACKey::DA)
+ if (CPA->getKey()->getZExtValue() != AArch64PACKey::DA)
return false;
return true;
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index 8c36014cd7f9b..b77bcb9305e2b 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -41,7 +41,8 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
const TargetMachine &TM) const override;
- bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const override;
+ bool
+ canEmitConstantPtrAuthAsIRelative(const ConstantPtrAuth *CPA) const override;
};
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
>From 025a8fbf1c05de6861464fd40071b2d373674b89 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Thu, 15 Jan 2026 00:01:32 +0300
Subject: [PATCH 7/9] Revert "Address review comments"
This reverts commit f18ee95da377eec2569c50e2d20a8dcca88ac124.
---
llvm/include/llvm/Target/TargetLoweringObjectFile.h | 4 +---
llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp | 4 ++--
llvm/lib/Target/AArch64/AArch64TargetObjectFile.h | 3 +--
3 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 8c6f29d6c0d75..3266e3f23e713 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -24,7 +24,6 @@ namespace llvm {
struct Align;
struct MachineJumpTableEntry;
class Constant;
-class ConstantPtrAuth;
class DataLayout;
class Function;
class GlobalObject;
@@ -306,8 +305,7 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
return nullptr;
}
- virtual bool
- canEmitConstantPtrAuthAsIRelative(const ConstantPtrAuth *CPA) const {
+ virtual bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const {
return false;
}
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 54736b596a0a5..807ab23af6344 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -198,13 +198,13 @@ static bool targetSupportsIRelativeRelocation(const Triple &TT) {
}
bool AArch64_ELFTargetObjectFile::canEmitConstantPtrAuthAsIRelative(
- const ConstantPtrAuth *CPA) const {
+ const Constant *CPA) const {
// We only emit an IRELATIVE relocation if the target supports IRELATIVE.
if (!targetSupportsIRelativeRelocation(TM->getTargetTriple()))
return false;
// For now, only the DA key is supported.
- if (CPA->getKey()->getZExtValue() != AArch64PACKey::DA)
+ if (cast<ConstantPtrAuth>(CPA)->getKey()->getZExtValue() != AArch64PACKey::DA)
return false;
return true;
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index b77bcb9305e2b..8c36014cd7f9b 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -41,8 +41,7 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
const TargetMachine &TM) const override;
- bool
- canEmitConstantPtrAuthAsIRelative(const ConstantPtrAuth *CPA) const override;
+ bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const override;
};
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
>From 998f4a13854bf36dec5f299de32b622266d6e892 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Thu, 15 Jan 2026 00:10:16 +0300
Subject: [PATCH 8/9] Revert "Keep pauth constants in .rodata when using
IRelative"
This reverts commit 115e2b18b081827b236b23f2dd9f16f645e91573.
---
.../llvm/Target/TargetLoweringObjectFile.h | 4 ---
llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp | 27 +++++++++++++++----
.../AArch64/AArch64TargetObjectFile.cpp | 23 ----------------
.../Target/AArch64/AArch64TargetObjectFile.h | 2 --
llvm/lib/Target/TargetLoweringObjectFile.cpp | 12 ++++-----
5 files changed, 27 insertions(+), 41 deletions(-)
diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 3266e3f23e713..06508bf4d50af 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -305,10 +305,6 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
return nullptr;
}
- virtual bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const {
- return false;
- }
-
protected:
virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
SectionKind Kind,
diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index 6ae1dbef51377..cb7570af13f23 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -2438,6 +2438,16 @@ void AArch64AsmPrinter::emitAddress(MCRegister Reg, const MCExpr *Expr,
}
}
+static bool targetSupportsIRelativeRelocation(const Triple &TT) {
+ // IFUNCs are ELF-only.
+ if (!TT.isOSBinFormatELF())
+ return false;
+
+ // IFUNCs are supported on glibc, bionic, and some but not all of the BSDs.
+ return TT.isOSGlibc() || TT.isAndroid() || TT.isOSFreeBSD() ||
+ TT.isOSDragonFly() || TT.isOSNetBSD();
+}
+
// Emit an ifunc resolver that returns a signed pointer to the specified target,
// and return a FUNCINIT reference to the resolver. In the linked binary, this
// function becomes the target of an IRELATIVE relocation. This resolver is used
@@ -2494,6 +2504,14 @@ const MCExpr *AArch64AsmPrinter::emitPAuthRelocationAsIRelative(
bool HasAddressDiversity, bool IsDSOLocal, const MCExpr *DSExpr) {
const Triple &TT = TM.getTargetTriple();
+ // We only emit an IRELATIVE relocation if the target supports IRELATIVE.
+ if (!targetSupportsIRelativeRelocation(TT))
+ return nullptr;
+
+ // For now, only the DA key is supported.
+ if (KeyID != AArch64PACKey::DA)
+ return nullptr;
+
AArch64Subtarget STI(TT, TM.getTargetCPU(), TM.getTargetCPU(),
TM.getTargetFeatureString(), TM, true);
this->STI = &STI;
@@ -2617,11 +2635,10 @@ AArch64AsmPrinter::lowerConstantPtrAuth(const ConstantPtrAuth &CPA) {
uint64_t Disc = CPA.getDiscriminator()->getZExtValue();
// Check if we can represent this with an IRELATIVE and emit it if so.
- if (TM.getObjFileLowering()->canEmitConstantPtrAuthAsIRelative(&CPA))
- if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
- Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
- BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
- return IFuncSym;
+ if (auto *IFuncSym = emitPAuthRelocationAsIRelative(
+ Sym, Disc, AArch64PACKey::ID(KeyID), CPA.hasAddressDiscriminator(),
+ BaseGVB && BaseGVB->isDSOLocal(), DSExpr))
+ return IFuncSym;
if (!isUInt<16>(Disc)) {
CPA.getContext().emitError("AArch64 PAC Discriminator '" + Twine(Disc) +
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 807ab23af6344..85de2d5010286 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -186,26 +186,3 @@ MCSection *AArch64_ELFTargetObjectFile::SelectSectionForGlobal(
return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
}
-
-static bool targetSupportsIRelativeRelocation(const Triple &TT) {
- // IFUNCs are ELF-only.
- if (!TT.isOSBinFormatELF())
- return false;
-
- // IFUNCs are supported on glibc, bionic, and some but not all of the BSDs.
- return TT.isOSGlibc() || TT.isAndroid() || TT.isOSFreeBSD() ||
- TT.isOSDragonFly() || TT.isOSNetBSD();
-}
-
-bool AArch64_ELFTargetObjectFile::canEmitConstantPtrAuthAsIRelative(
- const Constant *CPA) const {
- // We only emit an IRELATIVE relocation if the target supports IRELATIVE.
- if (!targetSupportsIRelativeRelocation(TM->getTargetTriple()))
- return false;
-
- // For now, only the DA key is supported.
- if (cast<ConstantPtrAuth>(CPA)->getKey()->getZExtValue() != AArch64PACKey::DA)
- return false;
-
- return true;
-}
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
index 8c36014cd7f9b..6b3381452c70b 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.h
@@ -40,8 +40,6 @@ class AArch64_ELFTargetObjectFile : public TargetLoweringObjectFileELF {
MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
const TargetMachine &TM) const override;
-
- bool canEmitConstantPtrAuthAsIRelative(const Constant *CPA) const override;
};
/// AArch64_MachoTargetObjectFile - This TLOF implementation is used for Darwin.
diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index c247b42647a9f..ae31cd90b37ab 100644
--- a/llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -227,17 +227,15 @@ void TargetLoweringObjectFile::emitPseudoProbeDescMetadata(
}
}
-static bool needsPtrAuthRelocation(const Constant *C,
- const TargetLoweringObjectFile *TLOF) {
- if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C))
- if (!TLOF->canEmitConstantPtrAuthAsIRelative(CPA))
- return true;
+static bool containsConstantPtrAuth(const Constant *C) {
+ if (isa<ConstantPtrAuth>(C))
+ return true;
if (isa<BlockAddress>(C) || isa<GlobalValue>(C))
return false;
for (const Value *Op : C->operands())
- if (needsPtrAuthRelocation(cast<Constant>(Op), TLOF))
+ if (containsConstantPtrAuth(cast<Constant>(Op)))
return true;
return false;
@@ -345,7 +343,7 @@ SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
} else {
// The dynamic linker always needs to fix PtrAuth relocations up.
- if (needsPtrAuthRelocation(C, TM.getObjFileLowering()))
+ if (containsConstantPtrAuth(C))
return SectionKind::getReadOnlyWithRel();
// In static, ROPI and RWPI relocation models, the linker will resolve
>From 2a5421f4a00f326c2cfc895e3a8d90fe2f6e2783 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Thu, 15 Jan 2026 00:23:03 +0300
Subject: [PATCH 9/9] Fix ptrauth-irelative.ll test
---
llvm/test/CodeGen/AArch64/ptrauth-irelative.ll | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll b/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll
index 1a367bf0d6046..1e0af68dd7ab8 100644
--- a/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll
+++ b/llvm/test/CodeGen/AArch64/ptrauth-irelative.ll
@@ -21,7 +21,7 @@
; CHECK-NEXT: add x0, x0, :lo12:dsolocal
; CHECK-NEXT: mov x1, #2
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@dsolocalref = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null), align 8
@@ -38,7 +38,7 @@
; CHECK-NEXT: .reloc [[LABEL]], R_AARCH64_PATCHINST, ds
; CHECK-NEXT: b __emupac_pacda
; CHECK-NEXT: ret
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@dsolocalrefds = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 2, ptr null, ptr @ds), align 8
@@ -50,7 +50,7 @@
; CHECK-NEXT: add x0, x0, :lo12:dsolocal+8
; CHECK-NEXT: mov x1, #3
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@dsolocalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @dsolocal, i64 8), i32 2, i64 3, ptr null), align 8
@@ -63,7 +63,7 @@
; CHECK-NEXT: adrp x1, [[PLACE]]
; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@disc = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 0, ptr @disc), align 8
@@ -76,7 +76,7 @@
; CHECK-NEXT: adrp x1, [[PLACE]]+65536
; CHECK-NEXT: add x1, x1, :lo12:[[PLACE]]+65536
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@disc65536 = constant ptr ptrauth (ptr @dsolocal, i32 2, i64 65536, ptr @disc), align 8
@@ -90,7 +90,7 @@
; CHECK-NEXT: ldr x0, [x0, :got_lo12:global]
; CHECK-NEXT: mov x1, #4
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@globalref = constant ptr ptrauth (ptr @global, i32 2, i64 4, ptr null), align 8
@@ -103,7 +103,7 @@
; CHECK-NEXT: add x0, x0, #8
; CHECK-NEXT: mov x1, #5
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@globalref8 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 8), i32 2, i64 5, ptr null), align 8
@@ -118,7 +118,7 @@
; CHECK-NEXT: add x0, x0, x16
; CHECK-NEXT: mov x1, #5
; CHECK-NEXT: b __emupac_pacda
-; CHECK-NEXT: .section .rodata
+; CHECK-NEXT: .section .data.rel.ro
; CHECK-NEXT: .xword [[FUNC]]@FUNCINIT
@globalref16777216 = constant ptr ptrauth (ptr getelementptr (i8, ptr @global, i64 16777216), i32 2, i64 5, ptr null), align 8
More information about the llvm-commits
mailing list