While playing with this app I found something odd:
let dylib1 = dlopen("/System/Library/Frameworks/CreateMLComponents.framework/CreateMLComponents", O_RDONLY)!
let s1 = dlsym(dylib1, "CreateMLComponentsVersionString")!
var info1 = Dl_info()
let success1 = dladdr(s1, &info1)
precondition(success1 != 0)
print(String(cString: info1.dli_sname!)) // CreateMLComponentsVersionString
let path1 = String(cString: info1.dli_fname!)
print(path1) // /System/Library/Frameworks/CreateMLComponents.framework/Versions/A/CreateMLComponents
let exists1 = FileManager.default.fileExists(atPath: path1)
print(exists1) // true
let dylib2 = dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", O_RDONLY)!
let s2 = dlsym(dylib2, "NSAllocateMemoryPages")! //
var info2 = Dl_info()
let success2 = dladdr(s2, &info2)
precondition(success2 != 0)
print(String(cString: info2.dli_sname!)) // NSAllocateMemoryPages
let path2 = String(cString: info2.dli_fname!)
print(path2) // /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
let exists2 = FileManager.default.fileExists(atPath: path2)
print(exists2) // false
The app runs fine and prints true for exists1 and false for exists2. That means that while both dlsym
calls succeed and both dladdr
calls return paths (within CreateMLComponents.framework and Foundation.framework correspondingly) the first file exists while the second file doesn't exist.
This raises quite a few questions:
-
Why some of the dylib files (in fact – most dylibs inside /System/Library/Frameworks hirerarchy) don't exist at the expected locations?
-
Why do we have symbolic link files (like Foundation.framework/Foundation) that point to those non-existent locations? What is the purpose of those symbols links?
-
Where are those missing dylib files in fact? They must be somewhere, no?! I guess to figure out the answer I could search the whole disk raw bytes for a particular byte pattern to know the answer but hope there's an easier way to know the truth!
-
Why do we have some exceptional cases like "CreateMLComponents.framework" and a couple of others that don't follow the rules established by the rest?
Thanks!
the first file exists while the second file doesn't exist.
Yes. That’s expected, and it’s fallout from the dynamic linker shared cache. I explain that in this post. And, it’ll probably be worth your while reading An Apple Library Primer for general backstory.
Taking a step back, why are you trying to dlopen
system libraries? There are some cases where that makes sense, but in most situations it’s better to import those libraries rather than dynamically load them.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"