dyld-733.6.tar.gz
[apple/dyld.git] / testing / test-cases / weak-coalesce-unload.dtest / main.c
1
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"
6
7 // RUN: ./weak-coalesce-unload.exe
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <dlfcn.h>
13
14 extern int foo();
15 extern void* fooPtr();
16
17 int main()
18 {
19 printf("[BEGIN] weak-coalesce-unload\n");
20
21 // dlopen foo1 which defines "foo"
22 void* handle1 = dlopen(RUN_DIR "/libfoo1.dylib", RTLD_FIRST);
23 if ( handle1 == NULL ) {
24 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR "/libfoo1.dylib", dlerror());
25 return 0;
26 }
27
28 const void* symFoo1 = dlsym(handle1, "foo");
29 if ( symFoo1 == NULL ) {
30 printf("[FAIL] dlsym(handle1, foo) failed\n");
31 return 0;
32 }
33
34 const void* symFooPtr1 = dlsym(handle1, "fooPtr");
35 if ( symFooPtr1 == NULL ) {
36 printf("[FAIL] dlsym(handle1, fooPtr) failed\n");
37 return 0;
38 }
39 void* fooptr1 = ((__typeof(&fooPtr))symFooPtr1)();
40
41 int close1 = dlclose(handle1);
42 if ( close1 != 0 ) {
43 printf("[FAIL] dlclose(handle1) failed with: %s\n", dlerror());
44 return 0;
45 }
46
47 // Now dlopen foo2 and get the value it finds for foo
48 void* handle2 = dlopen(RUN_DIR "/libfoo2.dylib", RTLD_FIRST);
49 if ( handle2 == NULL ) {
50 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR "/libfoo2.dylib", dlerror());
51 return 0;
52 }
53
54 const void* symFoo2 = dlsym(handle2, "foo");
55 if ( symFoo2 == NULL ) {
56 printf("[FAIL] dlsym(handle2, foo) failed\n");
57 return 0;
58 }
59
60 const void* symFooPtr2 = dlsym(handle2, "fooPtr");
61 if ( symFooPtr2 == NULL ) {
62 printf("[FAIL] dlsym(handle2, fooPtr) failed\n");
63 return 0;
64 }
65 void* fooptr2 = ((__typeof(&fooPtr))symFooPtr2)();
66
67 // Don't close foo2, but instead open foo3
68 void* handle3 = dlopen(RUN_DIR "/libfoo3.dylib", RTLD_FIRST);
69 if ( handle3 == NULL ) {
70 printf("[FAIL] dlopen(\"%s\") failed with: %s\n", RUN_DIR "/libfoo3.dylib", dlerror());
71 return 0;
72 }
73
74 const void* symFoo3 = dlsym(handle3, "foo");
75 if ( symFoo3 == NULL ) {
76 printf("[FAIL] dlsym(handle3, foo) failed\n");
77 return 0;
78 }
79
80 const void* symFooPtr3 = dlsym(handle3, "fooPtr");
81 if ( symFooPtr3 == NULL ) {
82 printf("[FAIL] dlsym(handle3, fooPtr) failed\n");
83 return 0;
84 }
85 void* fooptr3 = ((__typeof(&fooPtr))symFooPtr3)();
86
87 // No-one should point to libfoo1.dylib
88 if ( symFoo1 == symFoo2 ) {
89 printf("[FAIL] foo1 == foo2\n");
90 return 0;
91 }
92 if ( symFoo1 == symFoo3 ) {
93 printf("[FAIL] foo1 == foo3\n");
94 return 0;
95 }
96
97 // foo2 and foo3 should be different
98 if ( symFoo2 == symFoo3 ) {
99 printf("[FAIL] foo2 != foo3\n");
100 return 0;
101 }
102
103 // But their coalesced values should be the same
104 if ( fooptr1 == fooptr2 ) {
105 printf("[FAIL] fooptr1 == fooptr2\n");
106 return 0;
107 }
108 if ( fooptr2 != fooptr3 ) {
109 printf("[FAIL] fooptr2 != fooptr3\n");
110 return 0;
111 }
112
113 // foo should return the value from foo2, not the value from foo3
114 // Also calling this would explode if we somehow pointed at foo1
115 if ( ((__typeof(&foo))fooptr2)() != 2 ) {
116 printf("[FAIL] foo2 != 2\n");
117 return 0;
118 }
119 if ( ((__typeof(&foo))fooptr3)() != 2 ) {
120 printf("[FAIL] foo3 != 2\n");
121 return 0;
122 }
123
124 printf("[PASS] weak-coalesce-unload\n");
125 return 0;
126 }
127