dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-flat.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -Wl,-U,_gInitialisersCalled -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC bar.c -dynamiclib -Wl,-U,_gInitialisersCalled $BUILD_DIR/libfoo.dylib -flat_namespace -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib -Wl,-w
4 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" -o $BUILD_DIR/dlopen-flat.exe
5
6 // RUN: ./dlopen-flat.exe
7
8 #include <stdio.h>
9 #include <dlfcn.h>
10
11 int gInitialisersCalled = 0;
12
13 int main() {
14 printf("[BEGIN] dlopen-flat\n");
15 int result;
16
17 // Foo exports foo()
18 void* fooHandle = 0;
19 {
20 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
21 if (!fooHandle) {
22 printf("dlopen failed with error: %s\n", dlerror());
23 return 1;
24 }
25 if (gInitialisersCalled != 1) {
26 printf("gInitialisersCalled != 1\n");
27 printf("[FAIL] dlopen-flat\n");
28 return 1;
29 }
30 }
31 // Now unload foo which should do something.
32 result = dlclose(fooHandle);
33 if (result != 0) {
34 printf("dlclose() returned %c\n", result);
35 printf("[FAIL] dlopen-flat\n");
36 return 1;
37 }
38
39 // Open foo again which should do something.
40 {
41 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
42 if (!fooHandle) {
43 printf("dlopen failed with error: %s\n", dlerror());
44 return 1;
45 }
46 if (gInitialisersCalled != 2) {
47 printf("gInitialisersCalled != 2\n");
48 printf("[FAIL] dlopen-flat\n");
49 return 1;
50 }
51 }
52
53 // Bar is going to resolve foo()
54 void* barHandle = 0;
55 {
56 barHandle = dlopen(RUN_DIR "/libbar.dylib", RTLD_LAZY);
57 if (!barHandle) {
58 printf("dlopen failed with error: %s\n", dlerror());
59 return 1;
60 }
61 if (gInitialisersCalled != 3) {
62 printf("gInitialisersCalled != 3\n");
63 printf("[FAIL] dlopen-flat\n");
64 return 1;
65 }
66 }
67 // Now unload foo which shouldn't do anything.
68 result = dlclose(fooHandle);
69 if (result != 0) {
70 printf("dlclose() returned %c\n", result);
71 printf("[FAIL] dlopen-flat\n");
72 return 1;
73 }
74
75 // Open foo again which shouldn't do anything.
76 {
77 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
78 if (!fooHandle) {
79 printf("dlopen failed with error: %s\n", dlerror());
80 return 1;
81 }
82 if (gInitialisersCalled != 3) {
83 printf("gInitialisersCalled != 3\n");
84 printf("[FAIL] dlopen-flat\n");
85 return 1;
86 }
87 }
88
89 printf("[PASS] dlopen-flat\n");
90 return 0;
91 }
92