darlinghq / darling

Darwin/macOS emulation layer for Linux
http://www.darlinghq.org
GNU General Public License v3.0
11.45k stars 445 forks source link

dyld: lazy symbol binding failed: Symbol not found: _objc_alloc_init #606

Closed CuriousTommy closed 3 years ago

CuriousTommy commented 4 years ago

I noticed that Darling fails to execute my program when I use [[... alloc] init]. It seems to happen with any Objective C class I use. I used macOS Catalina to compile this software.

$ darling shell
Darling [/Volumes/SystemRoot/home/user/Documents]$ ./alloc_test 
dyld: lazy symbol binding failed: Symbol not found: _objc_alloc_init
  Referenced from: /Volumes/SystemRoot/home/user/Documents/alloc_test
  Expected in: /usr/lib/libobjc.A.dylib

dyld: Symbol not found: _objc_alloc_init
  Referenced from: /Volumes/SystemRoot/home/user/Documents/alloc_test
  Expected in: /usr/lib/libobjc.A.dylib

Abort trap: 6 (core dumped)
ahyattdev commented 4 years ago

Could you share the source of this test?

bugaevc commented 4 years ago

We need to update our objc4; this method seems to be new in 10.14.4

CuriousTommy commented 4 years ago

Sound's like you guys got this figured out, but just in case... here is a very simple source code example.

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    NSArray<NSString *> *value = [[NSArray alloc] init];
    return 0;
}
Berrysoft commented 4 years ago

Same problem when compiling FSEvents with clang 10.0.1 on Linux.

Berrysoft commented 4 years ago

I backported this function for objc4:

diff --git a/runtime/NSObject.mm b/runtime/NSObject.mm
index c7e7f43..9b299ab 100644
--- a/runtime/NSObject.mm
+++ b/runtime/NSObject.mm
@@ -1774,6 +1774,13 @@ objc_allocWithZone(Class cls)
     return callAlloc(cls, true/*checkNil*/, true/*allocWithZone*/);
 }

+// Calls [[cls alloc] init].
+id
+objc_alloc_init(Class cls)
+{
+    return [callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/) init];
+}
+

 void
 _objc_rootDealloc(id obj)
diff --git a/runtime/objc-internal.h b/runtime/objc-internal.h
index 5bcb28c..f112568 100644
--- a/runtime/objc-internal.h
+++ b/runtime/objc-internal.h
@@ -502,6 +502,9 @@ OBJC_EXPORT id objc_alloc(Class cls)
 OBJC_EXPORT id objc_allocWithZone(Class cls)
     OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0);

+OBJC_EXPORT id objc_alloc_init(Class _Nullable cls)
+    OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2);
+
 OBJC_EXPORT id objc_retain(id obj)
     __asm__("_objc_retain")
     OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0);

and it works.

facekapow commented 3 years ago

This should be fixed; objc4 was updated and this is defined now.