[clang] [llvm] [clang] Implement pragma clang section on COFF targets (PR #112714)
Vinicius Tadeu Zein via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 22 08:04:45 PST 2024
https://github.com/vtz updated https://github.com/llvm/llvm-project/pull/112714
>From e01f6fb36734b214e23cbbe66818b3269d21a591 Mon Sep 17 00:00:00 2001
From: Vinicius Tadeu Zein <vinicius.zein at kpit.com>
Date: Fri, 22 Nov 2024 10:39:08 -0500
Subject: [PATCH] [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.
---
clang/docs/LanguageExtensions.rst | 2 +-
clang/docs/ReleaseNotes.rst | 2 +
clang/test/Sema/pragma-clang-section.c | 1 +
.../CodeGen/TargetLoweringObjectFileImpl.cpp | 52 ++++++++-----------
llvm/test/CodeGen/ARM/clang-section.ll | 1 +
5 files changed, 26 insertions(+), 32 deletions(-)
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 3c9078bcdf8118..caeebb39ce31ed 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5531,7 +5531,7 @@ The ``#pragma clang section`` directive obeys the following rules:
* The pragma clang section is enabled automatically, without need of any flags.
-* This feature is only defined to work sensibly for ELF and Mach-O targets.
+* This feature is only defined to work sensibly for ELF, Mach-O and COFF targets.
* If section name is specified through _attribute_((section("myname"))), then
the attribute name gains precedence.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 54145b28154eb4..4d9401c34e2f17 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -845,6 +845,8 @@ Windows Support
When `-fms-compatibility-version=18.00` or prior is set on the command line this Microsoft extension is still
allowed as VS2013 and prior allow it.
+- Clang now supports the ``#pragma clang section`` directive for COFF targets.
+
LoongArch Support
^^^^^^^^^^^^^^^^^
diff --git a/clang/test/Sema/pragma-clang-section.c b/clang/test/Sema/pragma-clang-section.c
index 458c91c2cf31cd..e33e1dfe8cbef7 100644
--- a/clang/test/Sema/pragma-clang-section.c
+++ b/clang/test/Sema/pragma-clang-section.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm64-windows-msvc
#pragma clang section bss = "mybss.1" data = "mydata.1" rodata = "myrodata.1" text = "mytext.1" // expected-note 2 {{#pragma entered here}}
#pragma clang section bss="" data="" rodata="" text=""
#pragma clang section
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index ce50a3c19ffe04..28875f91ae7202 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -781,29 +781,32 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
return {Group, IsComdat, Flags};
}
-static MCSection *selectExplicitSectionGlobal(
- const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
- MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
- bool Retain, bool ForceUnique) {
- StringRef SectionName = GO->getSection();
-
+static StringRef handlePragmaClangSection(const GlobalObject *GO, SectionKind Kind) {
// Check if '#pragma clang section' name is applicable.
// Note that pragma directive overrides -ffunction-section, -fdata-section
// and so section name is exactly as user specified and not uniqued.
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
if (GV && GV->hasImplicitSection()) {
auto Attrs = GV->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();
- }
+ if (Attrs.hasAttribute("bss-section") && Kind.isBSS())
+ return Attrs.getAttribute("bss-section").getValueAsString();
+ else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())
+ return Attrs.getAttribute("rodata-section").getValueAsString();
+ else if (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel())
+ return Attrs.getAttribute("relro-section").getValueAsString();
+ else if (Attrs.hasAttribute("data-section") && Kind.isData())
+ return Attrs.getAttribute("data-section").getValueAsString();
}
+ return GO->getSection();
+}
+
+static MCSection *selectExplicitSectionGlobal(
+ const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM,
+ MCContext &Ctx, Mangler &Mang, unsigned &NextUniqueID,
+ bool Retain, bool ForceUnique) {
+ StringRef SectionName = handlePragmaClangSection(GO, Kind);
+
// Infer section flags from the section name if we can.
Kind = getELFKindForNamedSection(SectionName, Kind);
@@ -1284,21 +1287,7 @@ static void checkMachOComdat(const GlobalValue *GV) {
MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
- StringRef SectionName = GO->getSection();
-
- const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
- if (GV && GV->hasImplicitSection()) {
- auto Attrs = GV->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();
- }
- }
+ StringRef SectionName = handlePragmaClangSection(GO, Kind);
// Parse the section specifier and create it if valid.
StringRef Segment, Section;
@@ -1667,7 +1656,7 @@ static int getSelectionForCOFF(const GlobalValue *GV) {
MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
- StringRef Name = GO->getSection();
+ StringRef Name = handlePragmaClangSection(GO, Kind);
if (Name == getInstrProfSectionName(IPSK_covmap, Triple::COFF,
/*AddSegmentInfo=*/false) ||
Name == getInstrProfSectionName(IPSK_covfun, Triple::COFF,
@@ -1677,6 +1666,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
Name == getInstrProfSectionName(IPSK_covname, Triple::COFF,
/*AddSegmentInfo=*/false))
Kind = SectionKind::getMetadata();
+
int Selection = 0;
unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
StringRef COMDATSymName = "";
diff --git a/llvm/test/CodeGen/ARM/clang-section.ll b/llvm/test/CodeGen/ARM/clang-section.ll
index 9277d90dba65b1..ab409d23ff6b28 100644
--- a/llvm/test/CodeGen/ARM/clang-section.ll
+++ b/llvm/test/CodeGen/ARM/clang-section.ll
@@ -1,4 +1,5 @@
;RUN: llc -mtriple=armv7-eabi %s -o - | FileCheck %s
+;RUN: llc -mtriple=armv7-msvc %s -o - | FileCheck %s
;Test that global variables and functions are assigned to correct sections.
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
More information about the cfe-commits
mailing list