[clang] [llvm] [PATCH] [COFF] Implement pragma clang section on COFF targets (PR #112714)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 17 07:08:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Vinicius Tadeu Zein (vtz)

<details>
<summary>Changes</summary>

This patch implements the directive pragma clang section on COFF targets with the exact same features available on ELF and Mach-O.

---
Full diff: https://github.com/llvm/llvm-project/pull/112714.diff


2 Files Affected:

- (added) clang/test/Sema/pragma-clang-section-coff.c (+7) 
- (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+32-1) 


``````````diff
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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/112714


More information about the cfe-commits mailing list