]>
git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/weak-coalesce-unload.dtest/main.c
2 // BUILD: $CC foo1.c -dynamiclib -install_name $RUN_DIR/libfoo1.dylib -o $BUILD_DIR/libfoo1.dylib
3 // BUILD: $CC foo2.c -dynamiclib -install_name $RUN_DIR/libfoo2.dylib -o $BUILD_DIR/libfoo2.dylib
4 // BUILD: $CC foo3.c -dynamiclib -install_name $RUN_DIR/libfoo3.dylib -o $BUILD_DIR/libfoo3.dylib
5 // BUILD: $CC main.c -o $BUILD_DIR/weak-coalesce-unload.exe -DRUN_DIR="$RUN_DIR"
7 // RUN: ./weak-coalesce-unload.exe
14 #include "test_support.h"
17 extern void* fooPtr();
21 // dlopen foo1 which defines "foo"
22 void* handle1
= dlopen(RUN_DIR
"/libfoo1.dylib", RTLD_FIRST
);
23 if ( handle1
== NULL
) {
24 FAIL("dlopen(\"%s\") failed with: %s", RUN_DIR
"/libfoo1.dylib", dlerror());
27 const void* symFoo1
= dlsym(handle1
, "foo");
28 if ( symFoo1
== NULL
) {
29 FAIL("dlsym(handle1, foo) failed");
32 const void* symFooPtr1
= dlsym(handle1
, "fooPtr");
33 if ( symFooPtr1
== NULL
) {
34 FAIL("dlsym(handle1, fooPtr) failed");
36 void* fooptr1
= ((__typeof(&fooPtr
))symFooPtr1
)();
38 int close1
= dlclose(handle1
);
40 FAIL("dlclose(handle1) failed with: %s", dlerror());
43 // Now dlopen foo2 and get the value it finds for foo
44 void* handle2
= dlopen(RUN_DIR
"/libfoo2.dylib", RTLD_FIRST
);
45 if ( handle2
== NULL
) {
46 FAIL("dlopen(\"%s\") failed with: %s", RUN_DIR
"/libfoo2.dylib", dlerror());
49 const void* symFoo2
= dlsym(handle2
, "foo");
50 if ( symFoo2
== NULL
) {
51 FAIL("dlsym(handle2, foo) failed");
54 const void* symFooPtr2
= dlsym(handle2
, "fooPtr");
55 if ( symFooPtr2
== NULL
) {
56 FAIL("dlsym(handle2, fooPtr) failed");
58 void* fooptr2
= ((__typeof(&fooPtr
))symFooPtr2
)();
60 // Don't close foo2, but instead open foo3
61 void* handle3
= dlopen(RUN_DIR
"/libfoo3.dylib", RTLD_FIRST
);
62 if ( handle3
== NULL
) {
63 FAIL("dlopen(\"%s\") failed with: %s", RUN_DIR
"/libfoo3.dylib", dlerror());
66 const void* symFoo3
= dlsym(handle3
, "foo");
67 if ( symFoo3
== NULL
) {
68 FAIL("dlsym(handle3, foo) failed");
71 const void* symFooPtr3
= dlsym(handle3
, "fooPtr");
72 if ( symFooPtr3
== NULL
) {
73 FAIL("dlsym(handle3, fooPtr) failed");
75 void* fooptr3
= ((__typeof(&fooPtr
))symFooPtr3
)();
77 // No-one should point to libfoo1.dylib
78 if ( symFoo1
== symFoo2
) {
81 if ( symFoo1
== symFoo3
) {
85 // foo2 and foo3 should be different
86 if ( symFoo2
== symFoo3
) {
90 // But their coalesced values should be the same
91 if ( fooptr1
== fooptr2
) {
92 FAIL("fooptr1 == fooptr2");
94 if ( fooptr2
!= fooptr3
) {
95 FAIL("fooptr2 != fooptr3");
98 // foo should return the value from foo2, not the value from foo3
99 // Also calling this would explode if we somehow pointed at foo1
100 if ( ((__typeof(&foo
))fooptr2
)() != 2 ) {
103 if ( ((__typeof(&foo
))fooptr3
)() != 2 ) {
107 PASS("weak-coalesce-unload");