diff --git a/compiler-rt/lib/orc/macho_platform.cpp b/compiler-rt/lib/orc/macho_platform.cpp --- a/compiler-rt/lib/orc/macho_platform.cpp +++ b/compiler-rt/lib/orc/macho_platform.cpp @@ -50,6 +50,7 @@ // Swift types. class ProtocolRecord; class ProtocolConformanceRecord; +class TypeMetadataRecord; extern "C" void swift_registerProtocols(const ProtocolRecord *begin, @@ -59,6 +60,10 @@ const ProtocolConformanceRecord *begin, const ProtocolConformanceRecord *end) ORC_RT_WEAK_IMPORT; +extern "C" void swift_registerTypeMetadataRecords( + const TypeMetadataRecord *begin, + const TypeMetadataRecord *end) ORC_RT_WEAK_IMPORT; + namespace { Error validatePointerSectionExtent(const char *SectionName, @@ -172,6 +177,21 @@ return Error::success(); } +Error registerSwift5Types(const std::vector &Sections, + const MachOJITDylibInitializers &MOJDIs) { + + if (ORC_RT_UNLIKELY(!Sections.empty() && !swift_registerTypeMetadataRecords)) + return make_error( + "swift_registerTypeMetadataRecords is not available"); + + for (const auto &Section : Sections) + swift_registerTypeMetadataRecords( + Section.Start.toPtr(), + Section.End.toPtr()); + + return Error::success(); +} + Error runModInits(const std::vector &ModInitsSections, const MachOJITDylibInitializers &MOJDIs) { @@ -261,6 +281,7 @@ {"__DATA,__objc_classlist", registerObjCClasses}, {"__TEXT,__swift5_protos", registerSwift5Protocols}, {"__TEXT,__swift5_proto", registerSwift5ProtocolConformances}, + {"__TEXT,__swift5_types", registerSwift5Types}, {"__DATA,__mod_init_func", runModInits}}; // FIXME: Move to thread-state. diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -400,6 +400,8 @@ if osx_version >= (10, 11): config.available_features.add('osx-autointerception') config.available_features.add('osx-ld64-live_support') + if osx_version >= (10, 15): + config.available_features.add('osx-swift-runtime') except subprocess.CalledProcessError: pass diff --git a/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S b/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/TestCases/Darwin/x86-64/trivial-swift-types-section.S @@ -0,0 +1,94 @@ +# RUN: %clang -c -o %t %s +# RUN: %llvm_jitlink -dlopen /usr/lib/swift/libswiftCore.dylib %t + +# REQUIRES: osx-swift-runtime + +# Check that _typeByName is able to find a type registered by metadata in +# the __swift5_types section. + + .section __TEXT,__text,regular,pure_instructions + .globl _main + .p2align 4, 0x90 +_main: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + # Constant String for "4Test3FooV" + movabsq $8018152761824990260, %rdi + movabsq $-1585267068834392465, %rsi + callq _$ss11_typeByNameyypXpSgSSF + xorl %edi, %edi + testq %rax, %rax + sete %dil + callq _exit + .cfi_endproc + + .private_extern _$s4Test3FooVMa + .globl _$s4Test3FooVMa + .p2align 4, 0x90 +_$s4Test3FooVMa: + leaq _$s4Test3FooVMf+8(%rip), %rax + xorl %edx, %edx + retq + + .section __TEXT,__const +l___unnamed_1: + .asciz "Test" + + .private_extern _$s4TestMXM + .globl _$s4TestMXM + .weak_definition _$s4TestMXM + .p2align 2 +_$s4TestMXM: + .long 0 + .long 0 + .long (l___unnamed_1-_$s4TestMXM)-8 + +l___unnamed_2: + .asciz "Foo" + + .private_extern _$s4Test3FooVMn + .globl _$s4Test3FooVMn + .p2align 2 +_$s4Test3FooVMn: + .long 81 + .long (_$s4TestMXM-_$s4Test3FooVMn)-4 + .long (l___unnamed_2-_$s4Test3FooVMn)-8 + .long (_$s4Test3FooVMa-_$s4Test3FooVMn)-12 + .long (_$s4Test3FooVMF-_$s4Test3FooVMn)-16 + .long 0 + .long 2 + + .section __DATA,__const + .p2align 3 +_$s4Test3FooVMf: + .quad _$sytWV + .quad 512 + .quad _$s4Test3FooVMn + + .private_extern "_symbolic _____ 4Test3FooV" + .section __TEXT,__swift5_typeref,regular,no_dead_strip + .globl "_symbolic _____ 4Test3FooV" + .weak_definition "_symbolic _____ 4Test3FooV" + .p2align 1 +"_symbolic _____ 4Test3FooV": + .byte 1 + .long (_$s4Test3FooVMn-"_symbolic _____ 4Test3FooV")-1 + .byte 0 + + .section __TEXT,__swift5_fieldmd,regular,no_dead_strip + .p2align 2 +_$s4Test3FooVMF: + .long "_symbolic _____ 4Test3FooV"-_$s4Test3FooVMF + .long 0 + .short 0 + .short 12 + .long 0 + + .section __TEXT,__swift5_types,regular,no_dead_strip + .p2align 2 +l_type_metadata_table: + .long _$s4Test3FooVMn-l_type_metadata_table diff --git a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp --- a/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp +++ b/llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp @@ -136,13 +136,14 @@ StringRef ObjCSelRefsSectionName = "__DATA,__objc_selrefs"; StringRef Swift5ProtoSectionName = "__TEXT,__swift5_proto"; StringRef Swift5ProtosSectionName = "__TEXT,__swift5_protos"; +StringRef Swift5TypesSectionName = "__TEXT,__swift5_types"; StringRef ThreadBSSSectionName = "__DATA,__thread_bss"; StringRef ThreadDataSectionName = "__DATA,__thread_data"; StringRef ThreadVarsSectionName = "__DATA,__thread_vars"; StringRef InitSectionNames[] = { ModInitFuncSectionName, ObjCSelRefsSectionName, ObjCClassListSectionName, - Swift5ProtosSectionName, Swift5ProtoSectionName}; + Swift5ProtosSectionName, Swift5ProtoSectionName, Swift5TypesSectionName}; } // end anonymous namespace diff --git a/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp b/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp --- a/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp +++ b/llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp @@ -77,6 +77,7 @@ Sec.getName() == "__DATA,__objc_classlist" || Sec.getName() == "__TEXT,__swift5_protos" || Sec.getName() == "__TEXT,__swift5_proto" || + Sec.getName() == "__TEXT,__swift5_types" || Sec.getName() == "__DATA,__mod_init_func") return true; return false;