[ORC][MachOPlatform] Don't add InitSectionSymbols for __objc_imageinfo.
authorLang Hames <lhames@gmail.com>
Fri, 5 May 2023 06:57:22 +0000 (23:57 -0700)
committerLang Hames <lhames@gmail.com>
Fri, 5 May 2023 07:52:03 +0000 (00:52 -0700)
The __objc_imageinfo section may be deleted (leaving dangling references to any
symbols that it contains), and shouldn't have any dependencies anyway. This
patch verifies that the section has no dependencies and then skips the section.

rdar://108469243

compiler-rt/test/orc/TestCases/Darwin/arm64/Inputs/objc-Foo-foo-class-method.S [new file with mode: 0644]
compiler-rt/test/orc/TestCases/Darwin/arm64/objc-cross-file-method-call.S [new file with mode: 0644]
llvm/lib/ExecutionEngine/Orc/MachOPlatform.cpp

diff --git a/compiler-rt/test/orc/TestCases/Darwin/arm64/Inputs/objc-Foo-foo-class-method.S b/compiler-rt/test/orc/TestCases/Darwin/arm64/Inputs/objc-Foo-foo-class-method.S
new file mode 100644 (file)
index 0000000..12a1f35
--- /dev/null
@@ -0,0 +1,88 @@
+       .section        __TEXT,__text,regular,pure_instructions
+       .build_version macos, 14, 0     sdk_version 14, 0
+       .p2align        2
+"+[Foo foo]":
+       mov     w0, wzr
+       ret
+
+       .section        __TEXT,__objc_classname,cstring_literals
+l_OBJC_CLASS_NAME_:
+       .asciz  "Foo"
+
+       .section        __TEXT,__objc_methname,cstring_literals
+l_OBJC_METH_VAR_NAME_:
+       .asciz  "foo"
+
+       .section        __TEXT,__objc_methtype,cstring_literals
+l_OBJC_METH_VAR_TYPE_:
+       .asciz  "i16@0:8"
+
+       .section        __DATA,__objc_const
+       .p2align        3
+__OBJC_$_CLASS_METHODS_Foo:
+       .long   24
+       .long   1
+       .quad   l_OBJC_METH_VAR_NAME_
+       .quad   l_OBJC_METH_VAR_TYPE_
+       .quad   "+[Foo foo]"
+
+       .p2align        3
+__OBJC_METACLASS_RO_$_Foo:
+       .long   1
+       .long   40
+       .long   40
+       .space  4
+       .quad   0
+       .quad   l_OBJC_CLASS_NAME_
+       .quad   __OBJC_$_CLASS_METHODS_Foo
+       .quad   0
+       .quad   0
+       .quad   0
+       .quad   0
+
+       .section        __DATA,__objc_data
+       .globl  _OBJC_METACLASS_$_Foo
+       .p2align        3
+_OBJC_METACLASS_$_Foo:
+       .quad   _OBJC_METACLASS_$_NSObject
+       .quad   _OBJC_METACLASS_$_NSObject
+       .quad   __objc_empty_cache
+       .quad   0
+       .quad   __OBJC_METACLASS_RO_$_Foo
+
+       .section        __DATA,__objc_const
+       .p2align        3
+__OBJC_CLASS_RO_$_Foo:
+       .long   0
+       .long   8
+       .long   8
+       .space  4
+       .quad   0
+       .quad   l_OBJC_CLASS_NAME_
+       .quad   0
+       .quad   0
+       .quad   0
+       .quad   0
+       .quad   0
+
+       .section        __DATA,__objc_data
+       .globl  _OBJC_CLASS_$_Foo
+       .p2align        3
+_OBJC_CLASS_$_Foo:
+       .quad   _OBJC_METACLASS_$_Foo
+       .quad   _OBJC_CLASS_$_NSObject
+       .quad   __objc_empty_cache
+       .quad   0
+       .quad   __OBJC_CLASS_RO_$_Foo
+
+       .section        __DATA,__objc_classlist,regular,no_dead_strip
+       .p2align        3
+l_OBJC_LABEL_CLASS_$:
+       .quad   _OBJC_CLASS_$_Foo
+
+       .section        __DATA,__objc_imageinfo,regular,no_dead_strip
+L_OBJC_IMAGE_INFO:
+       .long   0
+       .long   64
+
+.subsections_via_symbols
diff --git a/compiler-rt/test/orc/TestCases/Darwin/arm64/objc-cross-file-method-call.S b/compiler-rt/test/orc/TestCases/Darwin/arm64/objc-cross-file-method-call.S
new file mode 100644 (file)
index 0000000..0d0d2de
--- /dev/null
@@ -0,0 +1,37 @@
+// Test that calls to Objective-C methods in other files work.
+//
+// RUN: %clang -c -o %t.o %s
+// RUN: %clang -c -o %t.aux.o %S/Inputs/objc-Foo-foo-class-method.S
+// RUN: %llvm_jitlink -preload libobjc.A.dylib %t.o %t.aux.o
+
+        .section       __TEXT,__text,regular,pure_instructions
+       .build_version macos, 14, 0     sdk_version 14, 0
+       .globl  _main
+       .p2align        2
+_main:
+       adrp    x8, _OBJC_CLASSLIST_REFERENCES_$_@PAGE
+       ldr     x0, [x8, _OBJC_CLASSLIST_REFERENCES_$_@PAGEOFF]
+       adrp    x8, _OBJC_SELECTOR_REFERENCES_@PAGE
+       ldr     x1, [x8, _OBJC_SELECTOR_REFERENCES_@PAGEOFF]
+       b       _objc_msgSend
+
+       .section        __DATA,__objc_classrefs,regular,no_dead_strip
+       .p2align        3, 0x0
+_OBJC_CLASSLIST_REFERENCES_$_:
+       .quad   _OBJC_CLASS_$_Foo
+
+       .section        __TEXT,__objc_methname,cstring_literals
+l_OBJC_METH_VAR_NAME_:
+       .asciz  "foo"
+
+       .section        __DATA,__objc_selrefs,literal_pointers,no_dead_strip
+       .p2align        3, 0x0
+_OBJC_SELECTOR_REFERENCES_:
+       .quad   l_OBJC_METH_VAR_NAME_
+
+       .section        __DATA,__objc_imageinfo,regular,no_dead_strip
+L_OBJC_IMAGE_INFO:
+       .long   0
+       .long   64
+
+.subsections_via_symbols
index c6eb120..23a992d 100644 (file)
@@ -908,7 +908,8 @@ Error MachOPlatform::MachOPlatformPlugin::preserveImportantSections(
   // address in the first graph that it appears in, then verify and discard it
   // in all subsequent graphs. In this pass we preserve unconditionally -- we'll
   // manually throw it away in the processObjCImageInfo pass.
-  if (auto *ObjCImageInfoSec = G.findSectionByName("__DATA,__objc_imageinfo")) {
+  if (auto *ObjCImageInfoSec =
+          G.findSectionByName(MachOObjCImageInfoSectionName)) {
     if (ObjCImageInfoSec->blocks_size() != 1)
       return make_error<StringError>(
           "In " + G.getName() +
@@ -916,6 +917,13 @@ Error MachOPlatform::MachOPlatformPlugin::preserveImportantSections(
           inconvertibleErrorCode());
     G.addAnonymousSymbol(**ObjCImageInfoSec->blocks().begin(), 0, 0, false,
                          true);
+
+    for (auto *B : ObjCImageInfoSec->blocks())
+      if (!B->edges_empty())
+        return make_error<StringError>("In " + G.getName() + ", " +
+                                           MachOObjCImageInfoSectionName +
+                                           " contains references to symbols",
+                                       inconvertibleErrorCode());
   }
 
   // Init sections are important: We need to preserve them and so that their
@@ -923,6 +931,11 @@ Error MachOPlatform::MachOPlatformPlugin::preserveImportantSections(
   // registerObjectPlatformSections.
   JITLinkSymbolSet InitSectionSymbols;
   for (auto &InitSectionName : MachOInitSectionNames) {
+    // Skip ObjCImageInfo -- this shouldn't have any dependencies, and we may
+    // remove it later.
+    if (InitSectionName == MachOObjCImageInfoSectionName)
+      continue;
+
     // Skip non-init sections.
     auto *InitSection = G.findSectionByName(InitSectionName);
     if (!InitSection)