Skip to content

Commit 0222326

Browse files
committed
[PATCH] [COFF] Implement pragma clang section on COFF targets
This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.
1 parent ae778ae commit 0222326

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
2+
// expected-no-diagnostics
3+
#pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" text = "mytext.1"
4+
#pragma clang section bss="" data="" rodata="" text=""
5+
#pragma clang section
6+
7+
int a;

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,22 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16771677
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
16781678
/*AddSegmentInfo=*/false))
16791679
Kind = SectionKind::getMetadata();
1680+
1681+
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1682+
if (GV && GV->hasImplicitSection()) {
1683+
auto Attrs = GV->getAttributes();
1684+
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
1685+
Name = Attrs.getAttribute("bss-section").getValueAsString();
1686+
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
1687+
Name = Attrs.getAttribute("rodata-section").getValueAsString();
1688+
} else if (Attrs.hasAttribute("relro-section") &&
1689+
Kind.isReadOnlyWithRel()) {
1690+
Name = Attrs.getAttribute("relro-section").getValueAsString();
1691+
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
1692+
Name = Attrs.getAttribute("data-section").getValueAsString();
1693+
}
1694+
}
1695+
16801696
int Selection = 0;
16811697
unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
16821698
StringRef COMDATSymName = "";
@@ -2378,13 +2394,28 @@ MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
23782394
StringRef SectionName = GO->getSection();
23792395

23802396
// Handle the XCOFF::TD case first, then deal with the rest.
2381-
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
2397+
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) {
23822398
if (GVar->hasAttribute("toc-data"))
23832399
return getContext().getXCOFFSection(
23842400
SectionName, Kind,
23852401
XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD),
23862402
/* MultiSymbolsAllowed*/ true);
23872403

2404+
if (GVar->hasImplicitSection()) {
2405+
auto Attrs = GVar->getAttributes();
2406+
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
2407+
SectionName = Attrs.getAttribute("bss-section").getValueAsString();
2408+
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
2409+
SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
2410+
} else if (Attrs.hasAttribute("relro-section") &&
2411+
Kind.isReadOnlyWithRel()) {
2412+
SectionName = Attrs.getAttribute("relro-section").getValueAsString();
2413+
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
2414+
SectionName = Attrs.getAttribute("data-section").getValueAsString();
2415+
}
2416+
}
2417+
}
2418+
23882419
XCOFF::StorageMappingClass MappingClass;
23892420
if (Kind.isText())
23902421
MappingClass = XCOFF::XMC_PR;

0 commit comments

Comments
 (0)