[llvm-branch-commits] [llvm] release/22.x: [SystemZ] Implement ctor/dtor emission via @@SQINIT and .xtor sections (#171476) (PR #178698)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jan 29 08:58:28 PST 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/178698
Backport 41567d8ec21b79e16c9f1223e2df23c65c1bc195
Requested by: @amy-kwan
>From a94d025bd6ae1b6e80ee4609c76503088cd20ca9 Mon Sep 17 00:00:00 2001
From: Amy Kwan <amy.kwan1 at ibm.com>
Date: Fri, 23 Jan 2026 13:29:08 -0500
Subject: [PATCH] [SystemZ] Implement ctor/dtor emission via @@SQINIT and .xtor
sections (#171476)
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.
(cherry picked from commit 41567d8ec21b79e16c9f1223e2df23c65c1bc195)
---
.../CodeGen/TargetLoweringObjectFileImpl.h | 1 +
llvm/include/llvm/MC/MCGOFFAttributes.h | 1 +
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 36 +++++++++++
llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 63 +++++++++++++++++++
llvm/lib/Target/SystemZ/SystemZAsmPrinter.h | 2 +
llvm/test/CodeGen/SystemZ/zos_sinit.ll | 34 ++++++++++
6 files changed, 137 insertions(+)
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..9f271dd502b24 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -331,6 +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;
};
} // 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/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 5edbc4caf3fae..6bbad68c26699 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,35 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal(
}
return TextSection;
}
+
+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
+ // 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();
+
+ MCContext &Ctx = getContext();
+ 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;
+}
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp
index a5fe29095503b..33e48f8ff1511 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"
@@ -1012,6 +1013,68 @@ 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())
+ return AsmPrinter::emitXXStructorList(DL, List, IsCtor);
+
+ SmallVector<Structor, 8> Structors;
+ preprocessXXStructorList(DL, List, Structors);
+ if (Structors.empty())
+ return;
+
+ const Align Align = llvm::Align(4);
+ const TargetLoweringObjectFileGOFF &Obj =
+ static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering());
+ for (Structor &S : Structors) {
+ MCSectionGOFF *Section =
+ static_cast<MCSectionGOFF *>(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());
+ 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;
+
+ MCSectionGOFF *ADASection =
+ static_cast<MCSectionGOFF *>(Obj.getADASection());
+ assert(ADASection && "ADA section must exist for GOFF targets!");
+ const MCSymbol *ADASym = ADASection->getBeginSymbol();
+ assert(ADASym && "ADA symbol should already be set!");
+
+ ADAFuncRefExpr = MCBinaryExpr::createAdd(
+ MCSpecifierExpr::create(MCSymbolRefExpr::create(ADASym, OutContext),
+ SystemZ::S_QCon, OutContext),
+ MCConstantExpr::create(ADATable.insert(Symbol, SlotKind), 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;
diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
index cb101e472824f..663a0b2b295f9 100644
--- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
+++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.h
@@ -108,6 +108,8 @@ class LLVM_LIBRARY_VISIBILITY SystemZAsmPrinter : public 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..447dec345e175
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/zos_sinit.ll
@@ -0,0 +1,34 @@
+; 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 XL4'7FFF0017'
+; Check direct relocation and low bit on ctor.
+; 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(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
+}
More information about the llvm-branch-commits
mailing list