[llvm] 5c18444 - MachO: support custom section names on global variables

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 10 10:23:29 PST 2023


Author: Tim Northover
Date: 2023-03-10T18:23:25Z
New Revision: 5c18444289f0e618b8491429a34d65577b0d4c32

URL: https://github.com/llvm/llvm-project/commit/5c18444289f0e618b8491429a34d65577b0d4c32
DIFF: https://github.com/llvm/llvm-project/commit/5c18444289f0e618b8491429a34d65577b0d4c32.diff

LOG: MachO: support custom section names on global variables

These attributes have been accepted in ELF for a while, and are generated by
Clang in some places, so it makes sense to support them on MachO too.

https://reviews.llvm.org/D143173

Added: 
    llvm/test/CodeGen/AArch64/custom-sections.ll

Modified: 
    llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index b78a4d2b2108b..5f9e9ea742678 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1267,6 +1267,20 @@ MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
 
   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();
+    }
+  }
+
   const Function *F = dyn_cast<Function>(GO);
   if (F && F->hasFnAttribute("implicit-section-name")) {
     SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();

diff  --git a/llvm/test/CodeGen/AArch64/custom-sections.ll b/llvm/test/CodeGen/AArch64/custom-sections.ll
new file mode 100644
index 0000000000000..d8d9ed48853b9
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/custom-sections.ll
@@ -0,0 +1,28 @@
+; RUN: llc -mtriple=arm64-apple-macosx %s -o - | FileCheck %s
+
+; CHECK-LABEL: .section __MY_DATA,__my_data
+; CHECK:       .globl _data
+; CHECK: _data:
+; CHECK:       .long 42
+ at data = global i32 42 #0
+
+; CHECK-LABEL: .section __MY_BSS,__my_bss
+; CHECK:       .globl _bss
+; CHECK: _bss:
+; CHECK:       .long 0
+ at bss = global i32 0 #0
+
+; CHECK-LABEL: .section __MY_RODATA,__my_rodata
+; CHECK:       .globl _const
+; CHECK: _const:
+; CHECK:       .long 42
+ at const = constant i32 42 #0
+
+; CHECK-LABEL: .section __MY_RELRO,__my_relro
+; CHECK:       .globl _vars_relro
+; CHECK: _vars_relro:
+; CHECK:       .quad _data
+; CHECK:       .quad _bss
+ at vars_relro = hidden constant [2 x ptr] [ptr @data, ptr @bss], align 16 #0
+
+attributes #0 = { "data-section"="__MY_DATA,__my_data" "bss-section"="__MY_BSS,__my_bss" "rodata-section"="__MY_RODATA,__my_rodata" "relro-section"="__MY_RELRO,__my_relro" }


        


More information about the llvm-commits mailing list