]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlopen-flat.dtest/main.c
dyld-519.2.2.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
4 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" -o $BUILD_DIR/dlopen-flat.exe
5
6 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR ./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 const char* path = RUN_DIR "/libfoo.dylib";
21 fooHandle = dlopen(path, RTLD_LAZY);
22 if (!fooHandle) {
23 printf("dlopen failed with error: %s\n", dlerror());
24 return 1;
25 }
26 if (gInitialisersCalled != 1) {
27 printf("gInitialisersCalled != 1\n");
28 printf("[FAIL] dlopen-flat\n");
29 return 1;
30 }
31 }
32 // Now unload foo which should do something.
33 result = dlclose(fooHandle);
34 if (result != 0) {
35 printf("dlclose() returned %c\n", result);
36 printf("[FAIL] dlopen-flat\n");
37 return 1;
38 }
39
40 // Open foo again which should do something.
41 {
42 const char* path = RUN_DIR "/libfoo.dylib";
43 fooHandle = dlopen(path, RTLD_LAZY);
44 if (!fooHandle) {
45 printf("dlopen failed with error: %s\n", dlerror());
46 return 1;
47 }
48 if (gInitialisersCalled != 2) {
49 printf("gInitialisersCalled != 2\n");
50 printf("[FAIL] dlopen-flat\n");
51 return 1;
52 }
53 }
54
55 // Bar is going to resolve foo()
56 void* barHandle = 0;
57 {
58 const char* path = RUN_DIR "/libbar.dylib";
59 barHandle = dlopen(path, RTLD_LAZY);
60 if (!barHandle) {
61 printf("dlopen failed with error: %s\n", dlerror());
62 return 1;
63 }
64 if (gInitialisersCalled != 3) {
65 printf("gInitialisersCalled != 3\n");
66 printf("[FAIL] dlopen-flat\n");
67 return 1;
68 }
69 }
70 // Now unload foo which shouldn't do anything.
71 result = dlclose(fooHandle);
72 if (result != 0) {
73 printf("dlclose() returned %c\n", result);
74 printf("[FAIL] dlopen-flat\n");
75 return 1;
76 }
77
78 // Open foo again which shouldn't do anything.
79 {
80 const char* path = RUN_DIR "/libfoo.dylib";
81 fooHandle = dlopen(path, RTLD_LAZY);
82 if (!fooHandle) {
83 printf("dlopen failed with error: %s\n", dlerror());
84 return 1;
85 }
86 if (gInitialisersCalled != 3) {
87 printf("gInitialisersCalled != 3\n");
88 printf("[FAIL] dlopen-flat\n");
89 return 1;
90 }
91 }
92
93 printf("[PASS] dlopen-flat\n");
94 return 0;
95 }
96