Skip to content

Commit e186a67

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 e186a67

File tree

5 files changed

+145
-32
lines changed

5 files changed

+145
-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: 23 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,
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
;RUN: llc -mtriple=x86_64-windows-msvc %s -o - | FileCheck %s
3+
;Test that global variables and functions are assigned to correct sections.
4+
5+
@a = global i32 0, align 4 #0
6+
@b = global i32 1, align 4 #0
7+
@c = global [4 x i32] zeroinitializer, align 4 #0
8+
@d = global [5 x i16] zeroinitializer, align 2 #0
9+
@e = global [6 x i16] [i16 0, i16 0, i16 1, i16 0, i16 0, i16 0], align 2 #0
10+
@f = constant i32 2, align 4 #0
11+
@h = global i32 0, align 4 #1
12+
@i = global i32 0, align 4 #2
13+
@j = constant i32 4, align 4 #2
14+
@k = global i32 0, align 4 #2
15+
@_ZZ3gooE7lstat_h = internal global i32 0, align 4 #2
16+
@_ZL1g = internal global [2 x i32] zeroinitializer, align 4 #0
17+
@l = global i32 5, align 4 #3
18+
@m = constant i32 6, align 4 #3
19+
@n = global i32 0, align 4
20+
@o = global i32 6, align 4
21+
@p = constant i32 7, align 4
22+
23+
declare i32 @zoo(ptr, ptr) #6
24+
25+
; Function Attrs: noinline nounwind
26+
define i32 @hoo() #7 {
27+
; CHECK-LABEL: hoo:
28+
; CHECK: # %bb.0: # %entry
29+
; CHECK-NEXT: movl b(%rip), %eax
30+
; CHECK-NEXT: retq
31+
entry:
32+
%0 = load i32, ptr @b, align 4
33+
ret i32 %0
34+
}
35+
36+
attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
37+
attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
38+
attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
39+
attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "rodata-section"="my_rodata.2" }
40+
attributes #6 = { "correctly-rounded-divide-sqrt-fp-math"="false" "denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
41+
attributes #7 = { noinline nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "denormal-fp-math"="preserve-sign,preserve-sign" "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="true" "no-jump-tables"="false" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
42+
43+
!llvm.module.flags = !{!0, !1, !2, !3}
44+
45+
!0 = !{i32 1, !"wchar_size", i32 4}
46+
!1 = !{i32 1, !"static_rwdata", i32 1}
47+
!2 = !{i32 1, !"enumsize_buildattr", i32 2}
48+
!3 = !{i32 1, !"armlib_unavailable", i32 0}
49+
50+
;CHECK: .text
51+
;CHECK: .type hoo,%function
52+
;CHECK: hoo:
53+
54+
;CHECK: .type a,%object
55+
;CHECK: .section my_bss.1,"aw",%nobits
56+
;CHECK: a:
57+
58+
;CHECK: .type b,%object
59+
;CHECK: .section my_data.1,"aw",%progbits
60+
;CHECK: b:
61+
62+
;CHECK: .type c,%object
63+
;CHECK: .section my_bss.1,"aw",%nobits
64+
;CHECK: c:
65+
66+
;CHECK: .type d,%object
67+
;CHECK: d:
68+
69+
;CHECK: .type e,%object
70+
;CHECK: .section my_data.1,"aw",%progbits
71+
;CHECK: e:
72+
73+
;CHECK: .type f,%object
74+
;CHECK: .section my_rodata.1,"a",%progbits
75+
;CHECK: f:
76+
77+
;CHECK: .type h,%object
78+
;CHECK: .bss
79+
;CHECK: h:
80+
81+
;CHECK: .type i,%object
82+
;CHECK: .section my_bss.2,"aw",%nobits
83+
;CHECK: i:
84+
85+
;CHECK: .type j,%object
86+
;CHECK: .section my_rodata.1,"a",%progbits
87+
;CHECK: j:
88+
89+
;CHECK: .type k,%object
90+
;CHECK: .section my_bss.2,"aw",%nobits
91+
;CHECK: k:
92+
93+
;CHECK: .type _ZZ3gooE7lstat_h,%object @ @_ZZ3gooE7lstat_h
94+
;CHECK: _ZZ3gooE7lstat_h:
95+
96+
;CHECK: .type _ZL1g,%object
97+
;CHECK: .section my_bss.1,"aw",%nobits
98+
;CHECK: _ZL1g:
99+
100+
;CHECK: .type l,%object
101+
;CHECK: .section my_data.2,"aw",%progbits
102+
;CHECK: l:
103+
104+
;CHECK: .type m,%object
105+
;CHECK: .section my_rodata.2,"a",%progbits
106+
;CHECK: m:
107+
108+
;CHECK: .type n,%object
109+
;CHECK: .bss
110+
;CHECK: n:
111+
112+
;CHECK: .type o,%object
113+
;CHECK: .data
114+
;CHECK: o:
115+
116+
;CHECK: .type p,%object
117+
;CHECK: .section .rodata,"a",%progbits
118+
;CHECK: p:

0 commit comments

Comments
 (0)