2 // BUILD: $CC linked1.m -dynamiclib -o $BUILD_DIR/liblinked1.dylib -install_name $RUN_DIR/liblinked1.dylib -lobjc
3 // BUILD: $CC linked2.m -dynamiclib -o $BUILD_DIR/liblinked2.dylib -install_name $RUN_DIR/liblinked2.dylib -lobjc
4 // BUILD: $CC main.m -o $BUILD_DIR/_dyld_for_each_objc_class.exe $BUILD_DIR/liblinked1.dylib $BUILD_DIR/liblinked2.dylib -lobjc
6 // RUN: ./_dyld_for_each_objc_class.exe
8 // The preoptimized objc class information is available via _dyld_for_each_objc_class().
9 // This test ensures that we match the objc behaviour when there are duplicates.
10 // For objc today, it walks the images in reverse load order, so the deepest library will be
11 // the canonical definition of a class.
13 #include <mach-o/dyld_priv.h>
15 #import <Foundation/Foundation.h>
17 #include "test_support.h"
19 // All the libraries have a copy of DyldClass
20 @interface DyldClass : NSObject
23 @implementation DyldClass
26 // Only the main executable has DyldMainClass
27 @interface DyldMainClass : NSObject
30 @implementation DyldMainClass
33 extern Class OBJC_CLASS_$_DyldClass;
34 extern Class OBJC_CLASS_$_DyldMainClass;
36 Class getMainDyldClass() {
37 return (Class)&OBJC_CLASS_$_DyldClass;
40 Class getMainDyldMainClass() {
41 return (Class)&OBJC_CLASS_$_DyldMainClass;
44 extern id objc_getClass(const char *name);
46 // Get the DyldClass from liblinked1.dylib
47 extern Class getLinked1DyldClass();
49 // Get the DyldClass from liblinked2.dylib
50 extern Class getLinked2DyldClass();
52 // Get the DyldLinkedClass from liblinked2.dylib
53 extern Class getLinked2DyldLinkedClass();
55 static bool gotDyldClassMain = false;
56 static bool gotDyldClassLinked = false;
57 static bool gotDyldClassLinked2 = false;
60 static bool objcOptimizedByDyld() {
61 extern const uint32_t objcInfo[] __asm("section$start$__DATA_CONST$__objc_imageinfo");
62 return (objcInfo[1] & 0x80);
65 static bool haveDyldCache() {
66 size_t unusedCacheLen;
67 return (_dyld_get_shared_cache_range(&unusedCacheLen) != NULL);
70 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
71 if (!objcOptimizedByDyld() || !haveDyldCache()) {
72 __block bool sawClass = false;
73 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
77 FAIL("dyld2 shouldn't see any classes");
79 PASS("no shared cache or no dyld optimized objc");
82 // Check that DyldClass comes from liblinked2 as it is last in load order
83 id runtimeDyldClass = objc_getClass("DyldClass");
84 if (runtimeDyldClass != getLinked2DyldClass()) {
85 FAIL("DyldClass should have come from liblinked2");
88 // Check that DyldLinkedClass comes from liblinked2 as it is last in load order
89 id runtimeDyldLinkedClass = objc_getClass("DyldLinkedClass");
90 if (runtimeDyldLinkedClass != getLinked2DyldLinkedClass()) {
91 FAIL("DyldLinkedClass should have come from liblinked2");
94 // Walk all the implementations of "DyldClass"
95 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
96 // We should walk these in the order liblinked2, liblinked, main exe
97 if (!gotDyldClassLinked2) {
98 if (classPtr != getLinked2DyldClass()) {
99 FAIL("Optimized DyldClass should have come from liblinked2");
102 FAIL("Optimized DyldClass isLoaded should have been set on liblinked2");
104 gotDyldClassLinked2 = true;
107 if (!gotDyldClassLinked) {
108 if (classPtr != getLinked1DyldClass()) {
109 FAIL("Optimized DyldClass should have come from liblinked");
112 FAIL("Optimized DyldClass isLoaded should have been set on liblinked");
114 gotDyldClassLinked = true;
117 if (!gotDyldClassMain) {
118 if (classPtr != getMainDyldClass()) {
119 FAIL("Optimized DyldClass should have come from main exe");
122 FAIL("Optimized DyldClass isLoaded should have been set on main exe");
124 gotDyldClassMain = true;
127 FAIL("Unexpected Optimized DyldClass");
130 if ( !gotDyldClassLinked2 || !gotDyldClassLinked || !gotDyldClassMain) {
131 FAIL("Failed to find all duplicates of 'DyldClass'");
134 // Visit again, and return liblinked2's DyldClass
135 __block void* dyldClassImpl = nil;
136 _dyld_for_each_objc_class("DyldClass", ^(void* classPtr, bool isLoaded, bool* stop) {
137 dyldClassImpl = classPtr;
140 if (dyldClassImpl != getLinked2DyldClass()) {
141 FAIL("_dyld_for_each_objc_class should have returned DyldClass from liblinked2");
144 // Visit DyldMainClass and make sure it makes the callback for just the result from main.exe
145 __block void* dyldMainClassImpl = nil;
146 _dyld_for_each_objc_class("DyldMainClass", ^(void* classPtr, bool isLoaded, bool* stop) {
147 dyldMainClassImpl = classPtr;
150 if (dyldMainClassImpl != getMainDyldMainClass()) {
151 FAIL("_dyld_for_each_objc_class should have returned DyldMainClass from main.exe");