[clang] [llvm] [PATCH] [COFF] Implement pragma clang section on COFF targets (PR #112714)
Vinicius Tadeu Zein via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 17 07:07:53 PDT 2024
https://github.com/vtz created https://github.com/llvm/llvm-project/pull/112714
This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.
>From 02223262c678f02b024b3ce83bb89f08d94815bf Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein <vinicius.zein at kpit.com>
Date: Wed, 16 Oct 2024 13:22:48 -0400
Subject: [PATCH] [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.
---
clang/test/Sema/pragma-clang-section-coff.c | 7 ++++
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 33 ++++++++++++++++++-
2 files changed, 39 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Sema/pragma-clang-section-coff.c
diff --git a/clang/test/Sema/pragma-clang-section-coff.c b/clang/test/Sema/pragma-clang-section-coff.c
new file mode 100644
index 00000000000000..573a629505a0cf
--- /dev/null
+++ b/clang/test/Sema/pragma-clang-section-coff.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
+// expected-no-diagnostics
+#pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" text = "mytext.1"
+#pragma clang section bss="" data="" rodata="" text=""
+#pragma clang section
+
+int a;
\ No newline at end of file
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index ce50a3c19ffe04..e7cc8d32d97ad0 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1677,6 +1677,22 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
/*AddSegmentInfo=*/false))
Kind = SectionKind::getMetadata();
+
+ const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
+ if (GV && GV->hasImplicitSection()) {
+ auto Attrs = GV->getAttributes();
+ if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
+ Name = Attrs.getAttribute("bss-section").getValueAsString();
+ } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
+ Name = Attrs.getAttribute("rodata-section").getValueAsString();
+ } else if (Attrs.hasAttribute("relro-section") &&
+ Kind.isReadOnlyWithRel()) {
+ Name = Attrs.getAttribute("relro-section").getValueAsString();
+ } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
+ Name = Attrs.getAttribute("data-section").getValueAsString();
+ }
+ }
+
int Selection = 0;
unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
StringRef COMDATSymName = "";
@@ -2378,13 +2394,28 @@ MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
StringRef SectionName = GO->getSection();
// Handle the XCOFF::TD case first, then deal with the rest.
- if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO))
+ if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO)) {
if (GVar->hasAttribute("toc-data"))
return getContext().getXCOFFSection(
SectionName, Kind,
XCOFF::CsectProperties(/*MappingClass*/ XCOFF::XMC_TD, XCOFF::XTY_SD),
/* MultiSymbolsAllowed*/ true);
+ if (GVar->hasImplicitSection()) {
+ auto Attrs = GVar->getAttributes();
+ if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
+ SectionName = Attrs.getAttribute("bss-section").getValueAsString();
+ } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
+ SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
+ } else if (Attrs.hasAttribute("relro-section") &&
+ Kind.isReadOnlyWithRel()) {
+ SectionName = Attrs.getAttribute("relro-section").getValueAsString();
+ } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
+ SectionName = Attrs.getAttribute("data-section").getValueAsString();
+ }
+ }
+ }
+
XCOFF::StorageMappingClass MappingClass;
if (Kind.isText())
MappingClass = XCOFF::XMC_PR;
More information about the cfe-commits
mailing list