]>
git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/_dyld_register_for_image_loads.dtest/main.cxx
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CXX main.cxx -o $BUILD_DIR/dyld_register_test.exe $BUILD_DIR/libfoo.dylib -DRUN_DIR="$RUN_DIR"
4 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo2.dylib -o $BUILD_DIR/libfoo2.dylib
5 // BUILD: $CC foo.c -bundle -o $BUILD_DIR/foo.bundle
6 // BUILD: $CC up.c -dynamiclib -install_name $RUN_DIR/libup.dylib -o $BUILD_DIR/libup.dylib
7 // BUILD: $CC baz.c -dynamiclib -install_name $RUN_DIR/libbaz.dylib -o $BUILD_DIR/libbaz.dylib $BUILD_DIR/libup.dylib
8 // BUILD: $CC bar.c -dynamiclib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib -Wl,-upward_library,$BUILD_DIR/libup.dylib -DRUN_DIR="$RUN_DIR"
10 // RUN: ./dyld_register_test.exe
15 #include <mach-o/dyld.h>
16 #include <mach-o/dyld_priv.h>
18 #include <unordered_set>
20 extern "C" void foo();
22 extern mach_header __dso_handle
;
24 static std::unordered_set
<const mach_header
*> sCurrentImages
;
25 static bool expectedUnloadableState
= false;
27 static void notify(const mach_header
* mh
, const char* path
, bool unloadable
)
29 fprintf(stderr
, "mh=%p, path=%s, unloadable=%d\n", mh
, path
, unloadable
);
30 if ( sCurrentImages
.count(mh
) != 0 ) {
31 printf("[FAIL] _dyld_register_for_image_loads: notified twice about %p\n", mh
);
34 sCurrentImages
.insert(mh
);
36 const char* leaf
= strrchr(path
, '/');
37 if ( unloadable
!= expectedUnloadableState
) {
38 printf("[FAIL] _dyld_register_for_image_loads: image incorrectly marked unloadable(%s) but expected unloadable(%s) %p %s\n",
39 unloadable
? "true" : "true", expectedUnloadableState
? "true" : "false", mh
, path
);
47 printf("[BEGIN] _dyld_register_for_image_loads\n");
49 // Initially all images must not be unloadable as they are directly linked to the main executable
50 expectedUnloadableState
= false;
51 _dyld_register_for_image_loads(¬ify
);
53 // verify we were notified about already loaded images
54 if ( sCurrentImages
.count(&__dso_handle
) == 0 ) {
55 printf("[FAIL] _dyld_register_for_image_loads() did not notify us about main executable\n");
58 const mach_header
* libSysMH
= dyld_image_header_containing_address((void*)&printf
);
59 if ( sCurrentImages
.count(libSysMH
) == 0 ) {
60 printf("[FAIL] _dyld_register_for_image_loads() did not notify us about libsystem_c.dylib\n");
63 const mach_header
* libFoo
= dyld_image_header_containing_address((void*)&foo
);
64 if ( sCurrentImages
.count(libFoo
) == 0 ) {
65 printf("[FAIL] _dyld_register_for_image_loads() did not notify us about libfoo.dylib\n");
69 // These dlopen's can be unloaded
70 expectedUnloadableState
= true;
72 // verify we were notified about load of libfoo2.dylib
73 void* handle2
= dlopen(RUN_DIR
"/libfoo2.dylib", RTLD_FIRST
);
74 if ( handle2
== NULL
) {
75 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR
"/libfoo.dylib", dlerror());
78 const void* libfoo2Foo
= dlsym(handle2
, "foo");
79 const mach_header
* libfoo2MH
= dyld_image_header_containing_address(libfoo2Foo
);
80 if ( sCurrentImages
.count(libfoo2MH
) == 0 ) {
81 printf("[FAIL] _dyld_register_for_image_loads() did not notify us about libfoo2.dylib\n");
85 // verify we were notified about load of foo.bundle
86 void* handleB
= dlopen(RUN_DIR
"/foo.bundle", RTLD_FIRST
);
87 if ( handleB
== NULL
) {
88 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR
"/foo.bundle", dlerror());
91 const void* libfooBFoo
= dlsym(handle2
, "foo");
92 const mach_header
* libfooB
= dyld_image_header_containing_address(libfooBFoo
);
93 if ( sCurrentImages
.count(libfooB
) == 0 ) {
94 printf("[FAIL] _dyld_register_for_image_loads() did not notify us about foo.bundle\n");
98 // Upward linking with dlopen may confuse the notifier as we may try to notify twice on the upwardly linked image
100 // libbar upward links libup
101 // libbar also dlopens libbaz
102 // libbaz links libup
103 // We should not get a duplicate notification on libup
104 void* handleBar
= dlopen(RUN_DIR
"/libbar.dylib", RTLD_FIRST
);
105 if ( handleBar
== NULL
) {
106 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR
"/libbar.dylib", dlerror());
110 printf("[PASS] _dyld_register_for_image_loads\n");