diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index b78a4d2b2108b2..5f9e9ea742678f 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(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(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 00000000000000..d8d9ed48853b91 --- /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 +@data = global i32 42 #0 + +; CHECK-LABEL: .section __MY_BSS,__my_bss +; CHECK: .globl _bss +; CHECK: _bss: +; CHECK: .long 0 +@bss = global i32 0 #0 + +; CHECK-LABEL: .section __MY_RODATA,__my_rodata +; CHECK: .globl _const +; CHECK: _const: +; CHECK: .long 42 +@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 +@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" }