[llvm] [SystemZ] Implement ctor/dtor emission via @@SQINIT and .xtor sections (PR #171476)
Amy Kwan via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 21 16:56:07 PST 2026
https://github.com/amy-kwan updated https://github.com/llvm/llvm-project/pull/171476
>From bf3e5112e5d76f4872b78499b2fa0ca9019005f9 Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Fri, 21 Nov 2025 13:54:48 -0500
Subject: [PATCH 1/6] [SystemZ] Implement ctor/dtor emission via @@SQINIT and
.xtor sections
This patch implements support for constructors/destructors by introducing the
@@SQINIT section and emitting .xtor.<priority> sections within the SystemZ
AsmPrinter and in the GOFF object lowering layer. Improvements to ADA descriptor
handling is also done within this change.
---
.../CodeGen/TargetLoweringObjectFileImpl.h | 4 ++
llvm/include/llvm/MC/MCGOFFAttributes.h | 1 +
llvm/include/llvm/MC/MCSectionGOFF.h | 11 ++++
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 49 +++++++++++++++
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 60 +++++++++++++++++++
llvm/lib/Target/SystemZ/SystemZAsmPrinter.h | 5 +-
llvm/test/CodeGen/SystemZ/zos_sinit.ll | 36 +++++++++++
7 files changed, 165 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/SystemZ/zos_sinit.ll
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index fe450b3c1a3a2..8f1f20f7ead0c 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -331,6 +331,10 @@ class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
const TargetMachine &TM) const override;
MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
const TargetMachine &TM) const override;
+ MCSection *getStaticCtorSection(unsigned Priority,
+ const MCSymbol *KeySym) const override;
+ MCSection *getStaticDtorSection(unsigned Priority,
+ const MCSymbol *KeySym) const override;
};
} // end namespace llvm
diff --git a/llvm/include/llvm/MC/MCGOFFAttributes.h b/llvm/include/llvm/MC/MCGOFFAttributes.h
index e771f36f35346..b1c6d73e41f9f 100644
--- a/llvm/include/llvm/MC/MCGOFFAttributes.h
+++ b/llvm/include/llvm/MC/MCGOFFAttributes.h
@@ -94,6 +94,7 @@ constexpr StringLiteral CLASS_CODE = "C_CODE64";
constexpr StringLiteral CLASS_WSA = "C_WSA64";
constexpr StringLiteral CLASS_DATA = "C_DATA64";
constexpr StringLiteral CLASS_PPA2 = "C_@@QPPA2";
+constexpr StringLiteral CLASS_SINIT = "C_@@SQINIT";
} // namespace GOFF
} // namespace llvm
diff --git a/llvm/include/llvm/MC/MCSectionGOFF.h b/llvm/include/llvm/MC/MCSectionGOFF.h
index 2136148368fbd..192996551c28b 100644
--- a/llvm/include/llvm/MC/MCSectionGOFF.h
+++ b/llvm/include/llvm/MC/MCSectionGOFF.h
@@ -27,6 +27,13 @@ namespace llvm {
class MCExpr;
class LLVM_ABI MCSectionGOFF final : public MCSection {
+public:
+ enum XtorType { NotXtor, CtorType, DtorType };
+
+private:
+ // ctor/dtor section related - i.e. C_@@S[Q]INIT
+ enum XtorType XtorSectionType = NotXtor;
+
// Parent of this section. Implies that the parent is emitted first.
MCSectionGOFF *Parent;
@@ -115,6 +122,10 @@ class LLVM_ABI MCSectionGOFF final : public MCSection {
bool requiresNonZeroLength() const { return RequiresNonZeroLength; }
void setName(StringRef SectionName) { Name = SectionName; }
+
+ bool isXtorSection() const { return XtorSectionType != NotXtor; }
+ void setXtorSectionType(XtorType Value) { XtorSectionType = Value; }
+ XtorType getXtorSectionType() const { return XtorSectionType; }
};
} // end namespace llvm
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 5edbc4caf3fae..4ede966702335 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2794,6 +2794,10 @@ void TargetLoweringObjectFileGOFF::getModuleMetadata(Module &M) {
TextLD->setWeak(false);
TextLD->setADA(ADAPR);
TextSection->setBeginSymbol(TextLD);
+ // Initialize the label for the ADA section.
+ MCSymbolGOFF *ADASym = static_cast<MCSymbolGOFF *>(
+ getContext().getOrCreateSymbol(ADAPR->getName()));
+ ADAPR->setBeginSymbol(ADASym);
}
MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
@@ -2857,3 +2861,48 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
}
return TextSection;
}
+
+static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
+ const MCSection *TextSection,
+ bool IsCtor,
+ unsigned Priority) {
+ // xl compilers on z/OS support priorities from min-int to max-int, with
+ // sinit as source priority 0. For clang, sinit has source priority 65535.
+ // For GOFF, the priority sortkey field is an unsigned value. So, we
+ // add min-int to get sorting to work properly but also subtract the
+ // clang sinit (65535) value so internally xl sinit and clang sinit have
+ // the same unsigned GOFF priority sortkey field value (i.e. 0x80000000).
+ static constexpr const uint32_t ClangDefaultSinitPriority = 65535;
+ uint32_t Prio = Priority + (0x80000000 - ClangDefaultSinitPriority);
+
+ std::string Name(".xtor");
+ if (Priority != ClangDefaultSinitPriority)
+ Name = llvm::Twine(Name).concat(".").concat(llvm::utostr(Priority)).str();
+
+ MCSectionGOFF *SInit = Ctx.getGOFFSection(
+ SectionKind::getMetadata(), GOFF::CLASS_SINIT,
+ GOFF::EDAttr{false, GOFF::ESD_RMODE_64, GOFF::ESD_NS_Parts,
+ GOFF::ESD_TS_ByteOriented, GOFF::ESD_BA_Merge,
+ GOFF::ESD_LB_Initial, GOFF::ESD_RQ_0,
+ GOFF::ESD_ALIGN_Doubleword},
+ static_cast<const MCSectionGOFF *>(TextSection)->getParent());
+
+ MCSectionGOFF *Xtor = Ctx.getGOFFSection(
+ SectionKind::getData(), Name,
+ GOFF::PRAttr{true, GOFF::ESD_EXE_DATA, GOFF::ESD_LT_XPLink,
+ GOFF::ESD_BSC_Section, Prio},
+ SInit);
+ return Xtor;
+}
+
+MCSection *TargetLoweringObjectFileGOFF::getStaticCtorSection(
+ unsigned Priority, const MCSymbol *KeySym) const {
+ return getStaticStructorSectionGOFF(getContext(), TextSection, true,
+ Priority);
+}
+
+MCSection *TargetLoweringObjectFileGOFF::getStaticDtorSection(
+ unsigned Priority, const MCSymbol *KeySym) const {
+ return getStaticStructorSectionGOFF(getContext(), TextSection, false,
+ Priority);
+}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index a5fe29095503b..0633732a4f498 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -31,6 +31,7 @@
#include "llvm/MC/MCInstBuilder.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSymbolGOFF.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/Compiler.h"
@@ -189,6 +190,10 @@ uint32_t SystemZAsmPrinter::AssociatedDataAreaTable::insert(const MCSymbol *Sym,
break;
}
+ // Language Environment DLL logic requires function descriptors, for
+ // imported functions, that are placed in the ADA to be 8 byte aligned.
+ if (SlotKind == SystemZII::MO_ADA_DIRECT_FUNC_DESC)
+ NextDisplacement = alignTo(NextDisplacement, 8);
uint32_t Displacement = NextDisplacement;
Displacements[std::make_pair(Sym, SlotKind)] = NextDisplacement;
NextDisplacement += Length;
@@ -1012,6 +1017,59 @@ void SystemZAsmPrinter::emitMachineConstantPoolValue(
OutStreamer->emitValue(Expr, Size);
}
+// Emit the ctor or dtor list taking into account the init priority.
+void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
+ const Constant *List, bool IsCtor) {
+ if (TM.getTargetTriple().isOSBinFormatGOFF())
+ AsmPrinter::emitXXStructorList(DL, List, IsCtor);
+
+ SmallVector<Structor, 8> Structors;
+ preprocessXXStructorList(DL, List, Structors);
+ if (Structors.empty())
+ return;
+
+ const Align Align = llvm::Align(4);
+ const TargetLoweringObjectFile &Obj = getObjFileLowering();
+ for (Structor &S : Structors) {
+ MCSection *OutputSection =
+ (IsCtor ? Obj.getStaticCtorSection(S.Priority, nullptr)
+ : Obj.getStaticDtorSection(S.Priority, nullptr));
+ OutStreamer->switchSection(OutputSection);
+ if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
+ emitAlignment(Align);
+
+ const MCSectionGOFF *Section =
+ static_cast<const MCSectionGOFF *>(getCurrentSection());
+ uint32_t XtorPriority = Section->getPRAttributes().SortKey;
+
+ const GlobalValue *GV = dyn_cast<GlobalValue>(S.Func->stripPointerCasts());
+ assert(GV && "C++ xxtor pointer was not a GlobalValue!");
+ MCSymbolGOFF *Symbol = static_cast<MCSymbolGOFF *>(getSymbol(GV));
+
+ // @@SQINIT entry: { unsigned prio; void (*ctor)(); void (*dtor)(); }
+
+ unsigned PointerSizeInBytes = DL.getPointerSize();
+
+ auto &Ctx = OutStreamer->getContext();
+ const MCExpr *ADAFuncRefExpr;
+ unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC;
+ ADAFuncRefExpr = MCBinaryExpr::createAdd(
+ MCSpecifierExpr::create(MCSymbolRefExpr::create(ADASym, OutContext),
+ SystemZ::S_QCon, OutContext),
+ MCConstantExpr::create(ADATable.insert(Symbol, SlotKind).second, Ctx),
+ Ctx);
+
+ emitInt32(XtorPriority);
+ if (IsCtor) {
+ OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
+ OutStreamer->emitIntValue(0, PointerSizeInBytes);
+ } else {
+ OutStreamer->emitIntValue(0, PointerSizeInBytes);
+ OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
+ }
+ }
+}
+
static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo,
raw_ostream &OS) {
const char *RegName;
@@ -1587,6 +1645,8 @@ void SystemZAsmPrinter::emitStartOfAsmFile(Module &M) {
}
void SystemZAsmPrinter::emitPPA2(Module &M) {
+ ADASym = getObjFileLowering().getADASection()->getBeginSymbol();
+
OutStreamer->pushSection();
OutStreamer->switchSection(getObjFileLowering().getTextSection(),
GOFF::SK_PPA2);
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
index cb101e472824f..42066f65d6416 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
@@ -31,6 +31,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
MCSymbol *CurrentFnPPA1Sym; // PPA1 Symbol.
MCSymbol *CurrentFnEPMarkerSym; // Entry Point Marker.
MCSymbol *PPA2Sym;
+ MCSymbol *ADASym;
SystemZTargetStreamer *getTargetStreamer() {
MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
@@ -101,13 +102,15 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
public:
SystemZAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
: AsmPrinter(TM, std::move(Streamer), ID), CurrentFnPPA1Sym(nullptr),
- CurrentFnEPMarkerSym(nullptr), PPA2Sym(nullptr),
+ CurrentFnEPMarkerSym(nullptr), PPA2Sym(nullptr), ADASym(nullptr),
ADATable(TM.getPointerSize(0)) {}
// Override AsmPrinter.
StringRef getPassName() const override { return "SystemZ Assembly Printer"; }
void emitInstruction(const MachineInstr *MI) override;
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
+ void emitXXStructorList(const DataLayout &DL, const Constant *List,
+ bool IsCtor) override;
void emitEndOfAsmFile(Module &M) override;
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
const char *ExtraCode, raw_ostream &OS) override;
diff --git a/llvm/test/CodeGen/SystemZ/zos_sinit.ll b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
new file mode 100644
index 0000000000000..036ec71466079
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
@@ -0,0 +1,36 @@
+; RUN: llc -emit-gnuas-syntax-on-zos=false < %s -mtriple=s390x-ibm-zos | \
+; RUN: FileCheck --check-prefixes=CHECK %s
+
+ at llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 22, ptr @cfuncctor, ptr null }]
+ at llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 22, ptr @cfuncdtor, ptr null }]
+
+; Check for presence of C_@@SQINIT:
+; CHECK: .xtor.22
+; CHECK: DC AD(cfuncctor)
+; CHECK: DC XL4'7FFF0017'
+; Check direct relocation and low bit on ctor.
+; CHECK: DC AD(QD(stdin#S)+XL8'0')
+; CHECK: DC XL8'0000000000000000'
+; CHECK: DC AD(cfuncdtor)
+; CHECK: DC XL4'7FFF0017'
+; CHECK: DC XL8'0000000000000000'
+; Check direct relocation and low bit on dtor.
+; CHECK: DC AD(QD(stdin#S)+XL8'16')
+
+; Check for function descriptors in ADA section:
+; CHECK: * Offset 0 function descriptor of cfuncctor
+; CHECK: DC RD(cfuncctor)
+; CHECK: DC VD(cfuncctor)
+; CHECK: * Offset 16 function descriptor of cfuncdtor
+; CHECK: DC RD(cfuncdtor)
+; CHECK: DC VD(cfuncdtor)
+
+define hidden void @cfuncctor() {
+entry:
+ ret void
+}
+
+define hidden void @cfuncdtor() {
+entry:
+ ret void
+}
>From d0c59f8583a995bad0e442d1e8968b3ab00bc8b4 Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Fri, 19 Dec 2025 15:25:29 -0500
Subject: [PATCH 2/6] Update test, movove ADA symbol, fix call to target
independent emitXXStructorList
---
llvm/include/llvm/MC/MCSectionGOFF.h | 11 -----------
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 12 +++++++-----
llvm/test/CodeGen/SystemZ/zos_sinit.ll | 2 --
3 files changed, 7 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/MC/MCSectionGOFF.h b/llvm/include/llvm/MC/MCSectionGOFF.h
index 192996551c28b..2136148368fbd 100644
--- a/llvm/include/llvm/MC/MCSectionGOFF.h
+++ b/llvm/include/llvm/MC/MCSectionGOFF.h
@@ -27,13 +27,6 @@ namespace llvm {
class MCExpr;
class LLVM_ABI MCSectionGOFF final : public MCSection {
-public:
- enum XtorType { NotXtor, CtorType, DtorType };
-
-private:
- // ctor/dtor section related - i.e. C_@@S[Q]INIT
- enum XtorType XtorSectionType = NotXtor;
-
// Parent of this section. Implies that the parent is emitted first.
MCSectionGOFF *Parent;
@@ -122,10 +115,6 @@ class LLVM_ABI MCSectionGOFF final : public MCSection {
bool requiresNonZeroLength() const { return RequiresNonZeroLength; }
void setName(StringRef SectionName) { Name = SectionName; }
-
- bool isXtorSection() const { return XtorSectionType != NotXtor; }
- void setXtorSectionType(XtorType Value) { XtorSectionType = Value; }
- XtorType getXtorSectionType() const { return XtorSectionType; }
};
} // end namespace llvm
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 0633732a4f498..348469e15fe4b 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -1020,8 +1020,8 @@ void SystemZAsmPrinter::emitMachineConstantPoolValue(
// Emit the ctor or dtor list taking into account the init priority.
void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
const Constant *List, bool IsCtor) {
- if (TM.getTargetTriple().isOSBinFormatGOFF())
- AsmPrinter::emitXXStructorList(DL, List, IsCtor);
+ if (!TM.getTargetTriple().isOSBinFormatGOFF())
+ return AsmPrinter::emitXXStructorList(DL, List, IsCtor);
SmallVector<Structor, 8> Structors;
preprocessXXStructorList(DL, List, Structors);
@@ -1639,14 +1639,16 @@ void SystemZAsmPrinter::emitPPA1(MCSymbol *FnEndSym) {
}
void SystemZAsmPrinter::emitStartOfAsmFile(Module &M) {
- if (TM.getTargetTriple().isOSzOS())
+ if (TM.getTargetTriple().isOSzOS()) {
+ ADASym = getObjFileLowering().getADASection()->getBeginSymbol();
+
emitPPA2(M);
+ }
+
AsmPrinter::emitStartOfAsmFile(M);
}
void SystemZAsmPrinter::emitPPA2(Module &M) {
- ADASym = getObjFileLowering().getADASection()->getBeginSymbol();
-
OutStreamer->pushSection();
OutStreamer->switchSection(getObjFileLowering().getTextSection(),
GOFF::SK_PPA2);
diff --git a/llvm/test/CodeGen/SystemZ/zos_sinit.ll b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
index 036ec71466079..447dec345e175 100644
--- a/llvm/test/CodeGen/SystemZ/zos_sinit.ll
+++ b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
@@ -6,12 +6,10 @@
; Check for presence of C_@@SQINIT:
; CHECK: .xtor.22
-; CHECK: DC AD(cfuncctor)
; CHECK: DC XL4'7FFF0017'
; Check direct relocation and low bit on ctor.
; CHECK: DC AD(QD(stdin#S)+XL8'0')
; CHECK: DC XL8'0000000000000000'
-; CHECK: DC AD(cfuncdtor)
; CHECK: DC XL4'7FFF0017'
; CHECK: DC XL8'0000000000000000'
; Check direct relocation and low bit on dtor.
>From 076a491a1cf65c88a58fd4c4be0506bf7134e18b Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Tue, 6 Jan 2026 13:11:19 -0500
Subject: [PATCH 3/6] Address review comments regarding sections, unused
parameter/unnecessary code changes.
---
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 7 ++-----
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 14 ++++----------
2 files changed, 6 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 4ede966702335..b6c52f8c056db 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2864,7 +2864,6 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
const MCSection *TextSection,
- bool IsCtor,
unsigned Priority) {
// xl compilers on z/OS support priorities from min-int to max-int, with
// sinit as source priority 0. For clang, sinit has source priority 65535.
@@ -2897,12 +2896,10 @@ static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
MCSection *TargetLoweringObjectFileGOFF::getStaticCtorSection(
unsigned Priority, const MCSymbol *KeySym) const {
- return getStaticStructorSectionGOFF(getContext(), TextSection, true,
- Priority);
+ return getStaticStructorSectionGOFF(getContext(), TextSection, Priority);
}
MCSection *TargetLoweringObjectFileGOFF::getStaticDtorSection(
unsigned Priority, const MCSymbol *KeySym) const {
- return getStaticStructorSectionGOFF(getContext(), TextSection, false,
- Priority);
+ return getStaticStructorSectionGOFF(getContext(), TextSection, Priority);
}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 348469e15fe4b..45bda60529603 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -190,10 +190,6 @@ uint32_t SystemZAsmPrinter::AssociatedDataAreaTable::insert(const MCSymbol *Sym,
break;
}
- // Language Environment DLL logic requires function descriptors, for
- // imported functions, that are placed in the ADA to be 8 byte aligned.
- if (SlotKind == SystemZII::MO_ADA_DIRECT_FUNC_DESC)
- NextDisplacement = alignTo(NextDisplacement, 8);
uint32_t Displacement = NextDisplacement;
Displacements[std::make_pair(Sym, SlotKind)] = NextDisplacement;
NextDisplacement += Length;
@@ -1031,15 +1027,13 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
const Align Align = llvm::Align(4);
const TargetLoweringObjectFile &Obj = getObjFileLowering();
for (Structor &S : Structors) {
- MCSection *OutputSection =
- (IsCtor ? Obj.getStaticCtorSection(S.Priority, nullptr)
- : Obj.getStaticDtorSection(S.Priority, nullptr));
- OutStreamer->switchSection(OutputSection);
+ MCSectionGOFF *Section = static_cast<MCSectionGOFF *>(
+ IsCtor ? Obj.getStaticCtorSection(S.Priority, nullptr)
+ : Obj.getStaticDtorSection(S.Priority, nullptr));
+ OutStreamer->switchSection(Section);
if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
emitAlignment(Align);
- const MCSectionGOFF *Section =
- static_cast<const MCSectionGOFF *>(getCurrentSection());
uint32_t XtorPriority = Section->getPRAttributes().SortKey;
const GlobalValue *GV = dyn_cast<GlobalValue>(S.Func->stripPointerCasts());
>From e4b85dd03e3c8dd024c27de8f035f659a544d575 Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Mon, 12 Jan 2026 10:18:32 -0500
Subject: [PATCH 4/6] Remove separate ctor/dtor section functions and add
getStaticXtorSection
---
llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h | 6 ++----
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 7 +------
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 9 ++++-----
3 files changed, 7 insertions(+), 15 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 8f1f20f7ead0c..0180f35536503 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -331,10 +331,8 @@ class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
const TargetMachine &TM) const override;
MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
const TargetMachine &TM) const override;
- MCSection *getStaticCtorSection(unsigned Priority,
- const MCSymbol *KeySym) const override;
- MCSection *getStaticDtorSection(unsigned Priority,
- const MCSymbol *KeySym) const override;
+ MCSection *getStaticXtorSection(unsigned Priority,
+ const MCSymbol *KeySym) const;
};
} // end namespace llvm
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index b6c52f8c056db..b92e75742d069 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2894,12 +2894,7 @@ static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
return Xtor;
}
-MCSection *TargetLoweringObjectFileGOFF::getStaticCtorSection(
- unsigned Priority, const MCSymbol *KeySym) const {
- return getStaticStructorSectionGOFF(getContext(), TextSection, Priority);
-}
-
-MCSection *TargetLoweringObjectFileGOFF::getStaticDtorSection(
+MCSection *TargetLoweringObjectFileGOFF::getStaticXtorSection(
unsigned Priority, const MCSymbol *KeySym) const {
return getStaticStructorSectionGOFF(getContext(), TextSection, Priority);
}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 45bda60529603..bbba319ca9883 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -1025,11 +1025,11 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
return;
const Align Align = llvm::Align(4);
- const TargetLoweringObjectFile &Obj = getObjFileLowering();
+ const TargetLoweringObjectFileGOFF &Obj =
+ static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering());
for (Structor &S : Structors) {
MCSectionGOFF *Section = static_cast<MCSectionGOFF *>(
- IsCtor ? Obj.getStaticCtorSection(S.Priority, nullptr)
- : Obj.getStaticDtorSection(S.Priority, nullptr));
+ Obj.getStaticXtorSection(S.Priority, nullptr));
OutStreamer->switchSection(Section);
if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
emitAlignment(Align);
@@ -1050,8 +1050,7 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
ADAFuncRefExpr = MCBinaryExpr::createAdd(
MCSpecifierExpr::create(MCSymbolRefExpr::create(ADASym, OutContext),
SystemZ::S_QCon, OutContext),
- MCConstantExpr::create(ADATable.insert(Symbol, SlotKind).second, Ctx),
- Ctx);
+ MCConstantExpr::create(ADATable.insert(Symbol, SlotKind), Ctx), Ctx);
emitInt32(XtorPriority);
if (IsCtor) {
>From 1e031104806582f904e3b001cc8996a1340d4e0a Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Mon, 12 Jan 2026 15:15:39 -0500
Subject: [PATCH 5/6] Remove ADASym.
---
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 4 ----
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 10 +++++-----
llvm/lib/Target/SystemZ/SystemZAsmPrinter.h | 3 +--
llvm/test/CodeGen/SystemZ/zos_sinit.ll | 4 ++--
4 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index b92e75742d069..9c4f25aa46201 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2794,10 +2794,6 @@ void TargetLoweringObjectFileGOFF::getModuleMetadata(Module &M) {
TextLD->setWeak(false);
TextLD->setADA(ADAPR);
TextSection->setBeginSymbol(TextLD);
- // Initialize the label for the ADA section.
- MCSymbolGOFF *ADASym = static_cast<MCSymbolGOFF *>(
- getContext().getOrCreateSymbol(ADAPR->getName()));
- ADAPR->setBeginSymbol(ADASym);
}
MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index bbba319ca9883..6e1bd5e852ef7 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -1047,6 +1047,10 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
auto &Ctx = OutStreamer->getContext();
const MCExpr *ADAFuncRefExpr;
unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC;
+ const MCSymbol *ADASym = Section->getBeginSymbol();
+ if (!ADASym)
+ ADASym = Ctx.getOrCreateSymbol(Section->getName());
+
ADAFuncRefExpr = MCBinaryExpr::createAdd(
MCSpecifierExpr::create(MCSymbolRefExpr::create(ADASym, OutContext),
SystemZ::S_QCon, OutContext),
@@ -1632,12 +1636,8 @@ void SystemZAsmPrinter::emitPPA1(MCSymbol *FnEndSym) {
}
void SystemZAsmPrinter::emitStartOfAsmFile(Module &M) {
- if (TM.getTargetTriple().isOSzOS()) {
- ADASym = getObjFileLowering().getADASection()->getBeginSymbol();
-
+ if (TM.getTargetTriple().isOSzOS())
emitPPA2(M);
- }
-
AsmPrinter::emitStartOfAsmFile(M);
}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
index 42066f65d6416..663a0b2b295f9 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
@@ -31,7 +31,6 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
MCSymbol *CurrentFnPPA1Sym; // PPA1 Symbol.
MCSymbol *CurrentFnEPMarkerSym; // Entry Point Marker.
MCSymbol *PPA2Sym;
- MCSymbol *ADASym;
SystemZTargetStreamer *getTargetStreamer() {
MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
@@ -102,7 +101,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public AsmPrinter {
public:
SystemZAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
: AsmPrinter(TM, std::move(Streamer), ID), CurrentFnPPA1Sym(nullptr),
- CurrentFnEPMarkerSym(nullptr), PPA2Sym(nullptr), ADASym(nullptr),
+ CurrentFnEPMarkerSym(nullptr), PPA2Sym(nullptr),
ADATable(TM.getPointerSize(0)) {}
// Override AsmPrinter.
diff --git a/llvm/test/CodeGen/SystemZ/zos_sinit.ll b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
index 447dec345e175..82c1be244f8ba 100644
--- a/llvm/test/CodeGen/SystemZ/zos_sinit.ll
+++ b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
@@ -8,12 +8,12 @@
; CHECK: .xtor.22
; CHECK: DC XL4'7FFF0017'
; Check direct relocation and low bit on ctor.
-; CHECK: DC AD(QD(stdin#S)+XL8'0')
+; CHECK: DC AD(QD(.xtor.22)+XL8'0')
; CHECK: DC XL8'0000000000000000'
; CHECK: DC XL4'7FFF0017'
; CHECK: DC XL8'0000000000000000'
; Check direct relocation and low bit on dtor.
-; CHECK: DC AD(QD(stdin#S)+XL8'16')
+; CHECK: DC AD(QD(.xtor.22)+XL8'16')
; Check for function descriptors in ADA section:
; CHECK: * Offset 0 function descriptor of cfuncctor
>From 65369ec94e443ca015a1f4a00a8aea94e285a44c Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Wed, 21 Jan 2026 19:54:05 -0500
Subject: [PATCH 6/6] Set ADA section correctly.
---
.../llvm/CodeGen/TargetLoweringObjectFileImpl.h | 3 +--
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 17 ++++++++---------
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 15 ++++++++++++---
llvm/test/CodeGen/SystemZ/zos_sinit.ll | 4 ++--
4 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 0180f35536503..9f271dd502b24 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -331,8 +331,7 @@ class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {
const TargetMachine &TM) const override;
MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
const TargetMachine &TM) const override;
- MCSection *getStaticXtorSection(unsigned Priority,
- const MCSymbol *KeySym) const;
+ MCSection *getStaticXtorSection(unsigned Priority) const;
};
} // end namespace llvm
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 9c4f25aa46201..0c617d433994c 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -2794,6 +2794,10 @@ void TargetLoweringObjectFileGOFF::getModuleMetadata(Module &M) {
TextLD->setWeak(false);
TextLD->setADA(ADAPR);
TextSection->setBeginSymbol(TextLD);
+ // Initialize the label for the ADA section.
+ MCSymbolGOFF *ADASym = static_cast<MCSymbolGOFF *>(
+ getContext().getOrCreateSymbol(ADAPR->getName()));
+ ADAPR->setBeginSymbol(ADASym);
}
MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal(
@@ -2858,10 +2862,9 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
return TextSection;
}
-static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
- const MCSection *TextSection,
- unsigned Priority) {
- // xl compilers on z/OS support priorities from min-int to max-int, with
+MCSection *TargetLoweringObjectFileGOFF::getStaticXtorSection(
+ unsigned Priority) const {
+ // XL C/C++ compilers on z/OS support priorities from min-int to max-int, with
// sinit as source priority 0. For clang, sinit has source priority 65535.
// For GOFF, the priority sortkey field is an unsigned value. So, we
// add min-int to get sorting to work properly but also subtract the
@@ -2874,6 +2877,7 @@ static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
if (Priority != ClangDefaultSinitPriority)
Name = llvm::Twine(Name).concat(".").concat(llvm::utostr(Priority)).str();
+ MCContext &Ctx = getContext();
MCSectionGOFF *SInit = Ctx.getGOFFSection(
SectionKind::getMetadata(), GOFF::CLASS_SINIT,
GOFF::EDAttr{false, GOFF::ESD_RMODE_64, GOFF::ESD_NS_Parts,
@@ -2889,8 +2893,3 @@ static MCSectionGOFF *getStaticStructorSectionGOFF(MCContext &Ctx,
SInit);
return Xtor;
}
-
-MCSection *TargetLoweringObjectFileGOFF::getStaticXtorSection(
- unsigned Priority, const MCSymbol *KeySym) const {
- return getStaticStructorSectionGOFF(getContext(), TextSection, Priority);
-}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index 6e1bd5e852ef7..7ffc72ad2f684 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
@@ -1029,11 +1029,16 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering());
for (Structor &S : Structors) {
MCSectionGOFF *Section = static_cast<MCSectionGOFF *>(
- Obj.getStaticXtorSection(S.Priority, nullptr));
+ Obj.getStaticXtorSection(S.Priority));
OutStreamer->switchSection(Section);
if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
emitAlignment(Align);
+ // The priority is provided as an input to getStaticXtorSection(), and is
+ // recalculated within that function as `Prio` going to going into the
+ // PR section.
+ // This priority retrieved via the `SortKey` below is the recalculated
+ // Priority.
uint32_t XtorPriority = Section->getPRAttributes().SortKey;
const GlobalValue *GV = dyn_cast<GlobalValue>(S.Func->stripPointerCasts());
@@ -1047,9 +1052,13 @@ void SystemZAsmPrinter::emitXXStructorList(const DataLayout &DL,
auto &Ctx = OutStreamer->getContext();
const MCExpr *ADAFuncRefExpr;
unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC;
- const MCSymbol *ADASym = Section->getBeginSymbol();
+
+ MCSectionGOFF *ADASection =
+ static_cast<MCSectionGOFF *>(Obj.getADASection());
+ assert(ADASection && "ADA section must exist for GOFF targets!");
+ const MCSymbol *ADASym = ADASection->getBeginSymbol();
if (!ADASym)
- ADASym = Ctx.getOrCreateSymbol(Section->getName());
+ ADASym = Ctx.getOrCreateSymbol(ADASection->getName());
ADAFuncRefExpr = MCBinaryExpr::createAdd(
MCSpecifierExpr::create(MCSymbolRefExpr::create(ADASym, OutContext),
diff --git a/llvm/test/CodeGen/SystemZ/zos_sinit.ll b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
index 82c1be244f8ba..447dec345e175 100644
--- a/llvm/test/CodeGen/SystemZ/zos_sinit.ll
+++ b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
@@ -8,12 +8,12 @@
; CHECK: .xtor.22
; CHECK: DC XL4'7FFF0017'
; Check direct relocation and low bit on ctor.
-; CHECK: DC AD(QD(.xtor.22)+XL8'0')
+; CHECK: DC AD(QD(stdin#S)+XL8'0')
; CHECK: DC XL8'0000000000000000'
; CHECK: DC XL4'7FFF0017'
; CHECK: DC XL8'0000000000000000'
; Check direct relocation and low bit on dtor.
-; CHECK: DC AD(QD(.xtor.22)+XL8'16')
+; CHECK: DC AD(QD(stdin#S)+XL8'16')
; Check for function descriptors in ADA section:
; CHECK: * Offset 0 function descriptor of cfuncctor
More information about the llvm-commits
mailing list