Index: include/llvm/MC/MCContext.h =================================================================== --- include/llvm/MC/MCContext.h +++ include/llvm/MC/MCContext.h @@ -22,6 +22,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/raw_ostream.h" #include +#include #include #include // FIXME: Shouldn't be needed. @@ -198,7 +199,10 @@ } }; - StringMap MachOUniquingMap; + /// Use shared_ptr for MachO as different keys can map to the same + /// section. + StringMap> MachOUniquingMap; + std::map ELFUniquingMap; std::map COFFUniquingMap; StringMap ELFRelSecNames; @@ -214,6 +218,10 @@ MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, unsigned Instance); + /// Helper function to get the reference to shared_ptr. + std::shared_ptr & + getMachOSectionRef(StringRef Segment, StringRef Section); + public: explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI, const MCObjectFileInfo *MOFI, @@ -308,6 +316,13 @@ BeginSymName); } + /// Map the coal sections to the non-coal sections: + /// + /// "__TEXT/__textcoal_nt" => section "__TEXT/__text" + /// "__TEXT/__const_coal" => section "__TEXT/__const" + /// "__DATA/__datacoal_nt" => section "__DATA/__data" + void setMachOCoalSections(); + MCSectionELF *getELFSection(StringRef Section, unsigned Type, unsigned Flags) { return getELFSection(Section, Type, Flags, nullptr); Index: lib/MC/MCContext.cpp =================================================================== --- lib/MC/MCContext.cpp +++ lib/MC/MCContext.cpp @@ -78,8 +78,6 @@ I.second->~MCSectionELF(); for (auto &I : COFFUniquingMap) I.second->~MCSectionCOFF(); - for (auto &I : MachOUniquingMap) - I.second->~MCSectionMachO(); UsedNames.clear(); Symbols.clear(); @@ -269,6 +267,17 @@ // Section Management //===----------------------------------------------------------------------===// +std::shared_ptr & +MCContext::getMachOSectionRef(StringRef Segment, StringRef Section) { + // Form the name to look up. + SmallString<64> Name; + Name += Segment; + Name.push_back(','); + Name += Section; + + return MachOUniquingMap[Name]; +}; + MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section, unsigned TypeAndAttributes, unsigned Reserved2, SectionKind Kind, @@ -278,24 +287,30 @@ // may not have the same flags as the requested section, if so this should be // diagnosed by the client as an error. - // Form the name to look up. - SmallString<64> Name; - Name += Segment; - Name.push_back(','); - Name += Section; - // Do the lookup, if we have a hit, return it. - MCSectionMachO *&Entry = MachOUniquingMap[Name]; - if (Entry) - return Entry; + std::shared_ptr &Entry = getMachOSectionRef(Segment, Section); + if (Entry.get()) + return Entry.get(); MCSymbol *Begin = nullptr; if (BeginSymName) Begin = createTempSymbol(BeginSymName, false); // Otherwise, return a new section. - return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, - Reserved2, Kind, Begin); + Entry = std::shared_ptr(new (*this) MCSectionMachO( + Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin), + [this](MCSectionMachO *S) { this->deallocate(S); }); + + return Entry.get(); +} + +void MCContext::setMachOCoalSections() { + getMachOSectionRef("__TEXT", "__textcoal_nt") = + getMachOSectionRef("__TEXT", "__text"); + getMachOSectionRef("__TEXT", "__const_coal") = + getMachOSectionRef("__TEXT", "__const"); + getMachOSectionRef("__DATA", "__datacoal_nt") = + getMachOSectionRef("__DATA", "__data"); } void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) { Index: lib/MC/MCObjectFileInfo.cpp =================================================================== --- lib/MC/MCObjectFileInfo.cpp +++ lib/MC/MCObjectFileInfo.cpp @@ -113,6 +113,18 @@ = Ctx->getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); + // If the target is not powerpc, map the coal sections to the non-coal + // sections. + // + // "__TEXT/__textcoal_nt" => section "__TEXT/__text" + // "__TEXT/__const_coal" => section "__TEXT/__const" + // "__DATA/__datacoal_nt" => section "__DATA/__data" + Triple::ArchType ArchTy = T.getArch(); + + if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64 && + ArchTy != Triple::ppc64le) + Ctx->setMachOCoalSections(); + TextCoalSection = Ctx->getMachOSection("__TEXT", "__textcoal_nt", MachO::S_COALESCED | Index: test/CodeGen/PowerPC/coal-sections.ll =================================================================== --- /dev/null +++ test/CodeGen/PowerPC/coal-sections.ll @@ -0,0 +1,24 @@ +; RUN: llc < %s -mtriple powerpc-apple-darwin | FileCheck %s + +; Check that *coal* sections are emitted. + +; CHECK: .section __TEXT,__textcoal_nt,coalesced,pure_instructions +; CHECK: .section __TEXT,__textcoal_nt,coalesced,pure_instructions +; CHECK-NEXT: .globl _foo + +; CHECK: .section __TEXT,__const_coal,coalesced +; CHECK-NEXT: .globl _a + +; CHECK: .section __DATA,__datacoal_nt,coalesced +; CHECK-NEXT: .globl _b + +@a = weak_odr constant [4 x i32] [i32 1, i32 2, i32 3, i32 4], align 16 +@b = weak global i32 5, align 4 +@g = common global i32* null, align 8 + +; Function Attrs: nounwind ssp uwtable +define weak i32* @foo() { +entry: + store i32* getelementptr inbounds ([4 x i32], [4 x i32]* @a, i64 0, i64 0), i32** @g, align 8 + ret i32* @b +} Index: test/CodeGen/X86/coal-sections.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/coal-sections.ll @@ -0,0 +1,23 @@ +; RUN: llc < %s -mtriple x86_64-apple-darwin | FileCheck %s + +; Check that *coal* sections are not emitted. + +; CHECK: .section __TEXT,__text,regular,pure_instructions{{$}} +; CHECK-NEXT: .globl _foo + +; CHECK: .section __TEXT,__const{{$}} +; CHECK-NEXT: .globl _a + +; CHECK: .section __DATA,__data{{$}} +; CHECK-NEXT: .globl _b + +@a = weak_odr constant [4 x i32] [i32 1, i32 2, i32 3, i32 4], align 16 +@b = weak global i32 5, align 4 +@g = common global i32* null, align 8 + +; Function Attrs: nounwind ssp uwtable +define weak i32* @foo() { +entry: + store i32* getelementptr inbounds ([4 x i32], [4 x i32]* @a, i64 0, i64 0), i32** @g, align 8 + ret i32* @b +} Index: test/CodeGen/X86/global-sections.ll =================================================================== --- test/CodeGen/X86/global-sections.ll +++ test/CodeGen/X86/global-sections.ll @@ -117,7 +117,7 @@ ; TODO: linux drops this into .rodata, we drop it into ".gnu.linkonce.r.G2" -; DARWIN: .section __TEXT,__const_coal,coalesced +; DARWIN: .section __TEXT,__const{{$}} ; DARWIN: _G2: ; DARWIN: .long 42 @@ -176,7 +176,6 @@ ; LINUX: .weak "foo bar" ; LINUX: "foo bar": -; DARWIN: .section __DATA,__datacoal_nt,coalesced ; DARWIN: .globl "_foo bar" ; DARWIN: .weak_definition "_foo bar" ; DARWIN: "_foo bar": @@ -190,7 +189,7 @@ ; LINUX: .byte 1 ; LINUX: .size G6, 1 -; DARWIN: .section __TEXT,__const_coal,coalesced +; DARWIN: .section __TEXT,__const{{$}} ; DARWIN: .globl _G6 ; DARWIN: .weak_definition _G6 ; DARWIN:_G6: @@ -239,7 +238,7 @@ @G10 = weak global [100 x i32] zeroinitializer, align 32 ; <[100 x i32]*> [#uses=0] -; DARWIN: .section __DATA,__datacoal_nt,coalesced +; DARWIN: .section __DATA,__data{{$}} ; DARWIN: .globl _G10 ; DARWIN: .weak_definition _G10 ; DARWIN: .align 5 Index: test/MC/MachO/coal-sections-powerpc.s =================================================================== --- /dev/null +++ test/MC/MachO/coal-sections-powerpc.s @@ -0,0 +1,46 @@ +// RUN: llvm-mc -triple powerpc-apple-darwin -filetype=obj %s -o - | llvm-readobj -sections | FileCheck %s + +// CHECK: Section { +// CHECK-NEXT: Index: 0 + +// CHECK: Section { +// CHECK-NEXT: Index: 1 +// CHECK-NEXT: Name: __textcoal_nt ( + +// CHECK: Section { +// CHECK-NEXT: Index: 2 + +// CHECK: Section { +// CHECK-NEXT: Index: 3 +// CHECK-NEXT: Name: __const_coal ( + +// CHECK: Section { +// CHECK-NEXT: Index: 4 +// CHECK-NEXT: Name: __datacoal_nt ( + + .section __TEXT,__text,regular,pure_instructions + .machine ppc + .section __TEXT,__textcoal_nt,coalesced,pure_instructions + .section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16 + .section __TEXT,__text,regular,pure_instructions + .section __TEXT,__textcoal_nt,coalesced,pure_instructions + .globl _foo + .weak_definition _foo + .align 4 +_foo: + blr + +.subsections_via_symbols + .section __TEXT,__const_coal,coalesced + .globl _a ; @a + .weak_definition _a + .align 4 +_a: + .long 1 ; 0x1 + + .section __DATA,__datacoal_nt,coalesced + .globl _b ; @b + .weak_definition _b + .align 2 +_b: + .long 5 ; 0x5 Index: test/MC/MachO/coal-sections-x86_64.s =================================================================== --- /dev/null +++ test/MC/MachO/coal-sections-x86_64.s @@ -0,0 +1,39 @@ +// RUN: llvm-mc -triple x86_64-apple-darwin -filetype=obj %s -o - | llvm-readobj -sections | FileCheck %s + +// CHECK: Sections [ +// CHECK-NEXT: Section { +// CHECK-NEXT: Index: 0 +// CHECK-NEXT: Name: __text ( + +// CHECK: Section { +// CHECK-NEXT: Index: 1 +// CHECK-NEXT: Name: __const ( + +// CHECK: Section { +// CHECK-NEXT: Index: 2 +// CHECK-NEXT: Name: __data ( + +// CHECK-NOT: Section + + .section __TEXT,__textcoal_nt,coalesced,pure_instructions + .globl _foo + .weak_definition _foo + .align 4, 0x90 +_foo: + retq + + .section __TEXT,__const_coal,coalesced + .globl _a ## @a + .weak_definition _a + .align 4 +_a: + .long 1 ## 0x1 + + .section __DATA,__datacoal_nt,coalesced + .globl _b ## @b + .weak_definition _b + .align 2 +_b: + .long 5 ## 0x5 + +.subsections_via_symbols Index: test/MC/MachO/weakdef.s =================================================================== --- test/MC/MachO/weakdef.s +++ test/MC/MachO/weakdef.s @@ -50,7 +50,7 @@ // CHECK-NEXT: } // CHECK-NEXT: Section { // CHECK-NEXT: Index: 1 -// CHECK-NEXT: Name: __datacoal_nt (5F 5F 64 61 74 61 63 6F 61 6C 5F 6E 74 00 00 00) +// CHECK-NEXT: Name: __data (5F 5F 64 61 74 61 00 00 00 00 00 00 00 00 00 00) // CHECK-NEXT: Segment: __DATA (5F 5F 44 41 54 41 00 00 00 00 00 00 00 00 00 00) // CHECK-NEXT: Address: 0x0 // CHECK-NEXT: Size: 0x4 @@ -58,7 +58,7 @@ // CHECK-NEXT: Alignment: 0 // CHECK-NEXT: RelocationOffset: 0x19C // CHECK-NEXT: RelocationCount: 1 -// CHECK-NEXT: Type: 0xB +// CHECK-NEXT: Type: 0x0 // CHECK-NEXT: Attributes [ (0x0) // CHECK-NEXT: ] // CHECK-NEXT: Reserved1: 0x0 @@ -69,7 +69,7 @@ // CHECK-NEXT: } // CHECK-NEXT: Section { // CHECK-NEXT: Index: 2 -// CHECK-NEXT: Name: __const_coal (5F 5F 63 6F 6E 73 74 5F 63 6F 61 6C 00 00 00 00) +// CHECK-NEXT: Name: __const (5F 5F 63 6F 6E 73 74 00 00 00 00 00 00 00 00 00) // CHECK-NEXT: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00) // CHECK-NEXT: Address: 0x4 // CHECK-NEXT: Size: 0x0 @@ -77,7 +77,7 @@ // CHECK-NEXT: Alignment: 0 // CHECK-NEXT: RelocationOffset: 0x0 // CHECK-NEXT: RelocationCount: 0 -// CHECK-NEXT: Type: 0xB +// CHECK-NEXT: Type: 0x0 // CHECK-NEXT: Attributes [ (0x0) // CHECK-NEXT: ] // CHECK-NEXT: Reserved1: 0x0 @@ -87,7 +87,7 @@ // CHECK-NEXT: } // CHECK-NEXT: ] // CHECK-NEXT: Relocations [ -// CHECK-NEXT: Section __datacoal_nt { +// CHECK-NEXT: Section __data { // CHECK-NEXT: 0x0 0 2 1 GENERIC_RELOC_VANILLA 0 __ZTS3optIbE // CHECK-NEXT: } // CHECK-NEXT: ] @@ -96,7 +96,7 @@ // CHECK-NEXT: Name: __ZTI3optIbE (14) // CHECK-NEXT: Extern // CHECK-NEXT: Type: Section (0xE) -// CHECK-NEXT: Section: __datacoal_nt (0x2) +// CHECK-NEXT: Section: __data (0x2) // CHECK-NEXT: RefType: UndefinedNonLazy (0x0) // CHECK-NEXT: Flags [ (0x80) // CHECK-NEXT: WeakDef (0x80) @@ -107,7 +107,7 @@ // CHECK-NEXT: Name: __ZTS3optIbE (1) // CHECK-NEXT: Extern // CHECK-NEXT: Type: Section (0xE) -// CHECK-NEXT: Section: __const_coal (0x3) +// CHECK-NEXT: Section: __const (0x3) // CHECK-NEXT: RefType: UndefinedNonLazy (0x0) // CHECK-NEXT: Flags [ (0x80) // CHECK-NEXT: WeakDef (0x80)