[PATCH] D143173: MachO: support custom section names on global variables

Tim Northover via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 2 05:27:40 PST 2023


t.p.northover created this revision.
t.p.northover added a reviewer: ab.
Herald added subscribers: hiraditya, mcrosier.
Herald added a project: All.
t.p.northover requested review of this revision.
Herald added a project: LLVM.

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

Files:
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/test/CodeGen/AArch64/custom-sections.ll


Index: llvm/test/CodeGen/AArch64/custom-sections.ll
===================================================================
--- /dev/null
+++ 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" }
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1275,6 +1275,20 @@
 
   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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143173.494268.patch
Type: text/x-patch
Size: 2357 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230202/5d9a6726/attachment.bin>


More information about the llvm-commits mailing list