]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/weak-coalesce-unload.dtest/main.c
dyld-851.27.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 #include "test_support.h"
15
16 extern int foo();
17 extern void* fooPtr();
18
19 int main()
20 {
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());
25 }
26
27 const void* symFoo1 = dlsym(handle1, "foo");
28 if ( symFoo1 == NULL ) {
29 FAIL("dlsym(handle1, foo) failed");
30 }
31
32 const void* symFooPtr1 = dlsym(handle1, "fooPtr");
33 if ( symFooPtr1 == NULL ) {
34 FAIL("dlsym(handle1, fooPtr) failed");
35 }
36 void* fooptr1 = ((__typeof(&fooPtr))symFooPtr1)();
37
38 int close1 = dlclose(handle1);
39 if ( close1 != 0 ) {
40 FAIL("dlclose(handle1) failed with: %s", dlerror());
41 }
42
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());
47 }
48
49 const void* symFoo2 = dlsym(handle2, "foo");
50 if ( symFoo2 == NULL ) {
51 FAIL("dlsym(handle2, foo) failed");
52 }
53
54 const void* symFooPtr2 = dlsym(handle2, "fooPtr");
55 if ( symFooPtr2 == NULL ) {
56 FAIL("dlsym(handle2, fooPtr) failed");
57 }
58 void* fooptr2 = ((__typeof(&fooPtr))symFooPtr2)();
59
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());
64 }
65
66 const void* symFoo3 = dlsym(handle3, "foo");
67 if ( symFoo3 == NULL ) {
68 FAIL("dlsym(handle3, foo) failed");
69 }
70
71 const void* symFooPtr3 = dlsym(handle3, "fooPtr");
72 if ( symFooPtr3 == NULL ) {
73 FAIL("dlsym(handle3, fooPtr) failed");
74 }
75 void* fooptr3 = ((__typeof(&fooPtr))symFooPtr3)();
76
77 // No-one should point to libfoo1.dylib
78 if ( symFoo1 == symFoo2 ) {
79 FAIL("foo1 == foo2");
80 }
81 if ( symFoo1 == symFoo3 ) {
82 FAIL("foo1 == foo3");
83 }
84
85 // foo2 and foo3 should be different
86 if ( symFoo2 == symFoo3 ) {
87 FAIL("foo2 != foo3");
88 }
89
90 // But their coalesced values should be the same
91 if ( fooptr1 == fooptr2 ) {
92 FAIL("fooptr1 == fooptr2");
93 }
94 if ( fooptr2 != fooptr3 ) {
95 FAIL("fooptr2 != fooptr3");
96 }
97
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 ) {
101 FAIL("foo2 != 2");
102 }
103 if ( ((__typeof(&foo))fooptr3)() != 2 ) {
104 FAIL("foo3 != 2");
105 }
106
107 PASS("weak-coalesce-unload");
108 }
109