Skip to content

Commit e01f6fb

Browse files
committed
[llvm] 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 026af9e commit e01f6fb

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5531,7 +5531,7 @@ The ``#pragma clang section`` directive obeys the following rules:
55315531
55325532
* The pragma clang section is enabled automatically, without need of any flags.
55335533
5534-
* This feature is only defined to work sensibly for ELF and Mach-O targets.
5534+
* This feature is only defined to work sensibly for ELF, Mach-O and COFF targets.
55355535
55365536
* If section name is specified through _attribute_((section("myname"))), then
55375537
the attribute name gains precedence.

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,8 @@ Windows Support
845845
When `-fms-compatibility-version=18.00` or prior is set on the command line this Microsoft extension is still
846846
allowed as VS2013 and prior allow it.
847847

848+
- Clang now supports the ``#pragma clang section`` directive for COFF targets.
849+
848850
LoongArch Support
849851
^^^^^^^^^^^^^^^^^
850852

clang/test/Sema/pragma-clang-section.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
2+
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
23
#pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" text = "mytext.1" // expected-note 2 {{#pragma entered here}}
34
#pragma clang section bss="" data="" rodata="" text=""
45
#pragma clang section

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -781,29 +781,32 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
781781
return {Group, IsComdat, Flags};
782782
}
783783

784-
static MCSection *selectExplicitSectionGlobal(
785-
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
786-
MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
787-
bool Retain, bool ForceUnique) {
788-
StringRef SectionName = GO->getSection();
789-
784+
static StringRef handlePragmaClangSection(const GlobalObject *GO, SectionKind Kind) {
790785
// Check if '#pragma clang section' name is applicable.
791786
// Note that pragma directive overrides -ffunction-section, -fdata-section
792787
// and so section name is exactly as user specified and not uniqued.
793788
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
794789
if (GV && GV->hasImplicitSection()) {
795790
auto Attrs = GV->getAttributes();
796-
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
797-
SectionName = Attrs.getAttribute("bss-section").getValueAsString();
798-
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
799-
SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
800-
} else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
801-
SectionName = Attrs.getAttribute("relro-section").getValueAsString();
802-
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
803-
SectionName = Attrs.getAttribute("data-section").getValueAsString();
804-
}
791+
if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
792+
return Attrs.getAttribute("bss-section").getValueAsString();
793+
else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
794+
return Attrs.getAttribute("rodata-section").getValueAsString();
795+
else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
796+
return Attrs.getAttribute("relro-section").getValueAsString();
797+
else if (Attrs.hasAttribute("data-section") && Kind.isData())
798+
return Attrs.getAttribute("data-section").getValueAsString();
805799
}
806800

801+
return GO->getSection();
802+
}
803+
804+
static MCSection *selectExplicitSectionGlobal(
805+
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
806+
MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
807+
bool Retain, bool ForceUnique) {
808+
StringRef SectionName = handlePragmaClangSection(GO, Kind);
809+
807810
// Infer section flags from the section name if we can.
808811
Kind = getELFKindForNamedSection(SectionName, Kind);
809812

@@ -1284,21 +1287,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
12841287
MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
12851288
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
12861289

1287-
StringRef SectionName = GO->getSection();
1288-
1289-
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1290-
if (GV && GV->hasImplicitSection()) {
1291-
auto Attrs = GV->getAttributes();
1292-
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
1293-
SectionName = Attrs.getAttribute("bss-section").getValueAsString();
1294-
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
1295-
SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
1296-
} else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
1297-
SectionName = Attrs.getAttribute("relro-section").getValueAsString();
1298-
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
1299-
SectionName = Attrs.getAttribute("data-section").getValueAsString();
1300-
}
1301-
}
1290+
StringRef SectionName = handlePragmaClangSection(GO, Kind);
13021291

13031292
// Parse the section specifier and create it if valid.
13041293
StringRef Segment, Section;
@@ -1667,7 +1656,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
16671656

16681657
MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16691658
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1670-
StringRef Name = GO->getSection();
1659+
StringRef Name = handlePragmaClangSection(GO, Kind);
16711660
if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
16721661
/*AddSegmentInfo=*/false) ||
16731662
Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
@@ -1677,6 +1666,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16771666
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
16781667
/*AddSegmentInfo=*/false))
16791668
Kind = SectionKind::getMetadata();
1669+
16801670
int Selection = 0;
16811671
unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
16821672
StringRef COMDATSymName = "";

llvm/test/CodeGen/ARM/clang-section.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s
2+
;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s
23
;Test that global variables and functions are assigned to correct sections.
34

45
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"

0 commit comments

Comments
 (0)