diff options
author | Vinicius Tadeu Zein <vtzein@gmail.com> | 2025-01-21 19:12:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-21 16:12:58 -0800 |
commit | 6ab9dafec807a64a4e940bfaecc815e23454dfe8 (patch) | |
tree | 58bb7851791c7811efee02aac36105b7651a892c /llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | |
parent | 8a5f1ef88bd96a8b15937927d9d0cd51324ba6de (diff) | |
download | llvm-6ab9dafec807a64a4e940bfaecc815e23454dfe8.zip llvm-6ab9dafec807a64a4e940bfaecc815e23454dfe8.tar.gz llvm-6ab9dafec807a64a4e940bfaecc815e23454dfe8.tar.bz2 |
[clang] Implement #pragma clang section on COFF targets (#112714)
This patch implements the directive #pragma clang section on COFF targets
with the exact same features available on ELF and Mach-O.
Diffstat (limited to 'llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp')
-rw-r--r-- | llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index be243c0..7db949f 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -788,29 +788,35 @@ 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); @@ -1291,21 +1297,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; @@ -1674,7 +1666,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, |