Skip to content

Commit 6048939

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 c6c864d commit 6048939

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

clang/docs/LanguageExtensions.rst

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

clang/docs/ReleaseNotes.rst

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

1096+
- Clang now supports the ``#pragma clang section`` directive for COFF targets.
1097+
10961098
LoongArch Support
10971099
^^^^^^^^^^^^^^^^^
10981100

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: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -788,29 +788,35 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
788788
return {Group, IsComdat, Flags};
789789
}
790790

791-
static MCSection *selectExplicitSectionGlobal(
792-
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
793-
MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
794-
bool Retain, bool ForceUnique) {
795-
StringRef SectionName = GO->getSection();
796-
791+
static StringRef handlePragmaClangSection(const GlobalObject *GO,
792+
SectionKind Kind) {
797793
// Check if '#pragma clang section' name is applicable.
798794
// Note that pragma directive overrides -ffunction-section, -fdata-section
799795
// and so section name is exactly as user specified and not uniqued.
800796
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
801797
if (GV && GV->hasImplicitSection()) {
802798
auto Attrs = GV->getAttributes();
803-
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
804-
SectionName = Attrs.getAttribute("bss-section").getValueAsString();
805-
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
806-
SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
807-
} else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
808-
SectionName = Attrs.getAttribute("relro-section").getValueAsString();
809-
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
810-
SectionName = Attrs.getAttribute("data-section").getValueAsString();
811-
}
799+
if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
800+
return Attrs.getAttribute("bss-section").getValueAsString();
801+
else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
802+
return Attrs.getAttribute("rodata-section").getValueAsString();
803+
else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
804+
return Attrs.getAttribute("relro-section").getValueAsString();
805+
else if (Attrs.hasAttribute("data-section") && Kind.isData())
806+
return Attrs.getAttribute("data-section").getValueAsString();
812807
}
813808

809+
return GO->getSection();
810+
}
811+
812+
static MCSection *selectExplicitSectionGlobal(const GlobalObject *GO,
813+
SectionKind Kind,
814+
const TargetMachine &TM,
815+
MCContext &Ctx, Mangler &Mang,
816+
unsigned &NextUniqueID,
817+
bool Retain, bool ForceUnique) {
818+
StringRef SectionName = handlePragmaClangSection(GO, Kind);
819+
814820
// Infer section flags from the section name if we can.
815821
Kind = getELFKindForNamedSection(SectionName, Kind);
816822

@@ -1291,21 +1297,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
12911297
MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
12921298
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
12931299

1294-
StringRef SectionName = GO->getSection();
1295-
1296-
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
1297-
if (GV && GV->hasImplicitSection()) {
1298-
auto Attrs = GV->getAttributes();
1299-
if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
1300-
SectionName = Attrs.getAttribute("bss-section").getValueAsString();
1301-
} else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
1302-
SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
1303-
} else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) {
1304-
SectionName = Attrs.getAttribute("relro-section").getValueAsString();
1305-
} else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
1306-
SectionName = Attrs.getAttribute("data-section").getValueAsString();
1307-
}
1308-
}
1300+
StringRef SectionName = handlePragmaClangSection(GO, Kind);
13091301

13101302
// Parse the section specifier and create it if valid.
13111303
StringRef Segment, Section;
@@ -1674,7 +1666,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
16741666

16751667
MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16761668
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
1677-
StringRef Name = GO->getSection();
1669+
StringRef Name = handlePragmaClangSection(GO, Kind);
16781670
if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
16791671
/*AddSegmentInfo=*/false) ||
16801672
Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
@@ -1684,6 +1676,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
16841676
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
16851677
/*AddSegmentInfo=*/false))
16861678
Kind = SectionKind::getMetadata();
1679+
16871680
int Selection = 0;
16881681
unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
16891682
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)