]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlopen-flat.dtest/main.c
dyld-750.5.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 #include "test_support.h"
12
13 int gInitialisersCalled = 0;
14
15 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
16 int result;
17
18 // Foo exports foo()
19 void* fooHandle = 0;
20 {
21 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
22 if (!fooHandle) {
23 FAIL("dlopen(\"" RUN_DIR "/libfoo.dylib\") failed with error: %s", dlerror());
24 }
25 if (gInitialisersCalled != 1) {
26 FAIL("gInitialisersCalled != 1");
27 }
28 }
29 // Now unload foo which should do something.
30 result = dlclose(fooHandle);
31 if (result != 0) {
32 FAIL("dlclose() returned %c", result);
33 }
34
35 // Open foo again which should do something.
36 {
37 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
38 if (!fooHandle) {
39 FAIL("dlopen failed with error: %s", dlerror());
40 }
41 if (gInitialisersCalled != 2) {
42 FAIL("gInitialisersCalled != 2");
43 }
44 }
45
46 // Bar is going to resolve foo()
47 void* barHandle = 0;
48 {
49 barHandle = dlopen(RUN_DIR "/libbar.dylib", RTLD_LAZY);
50 if (!barHandle) {
51 FAIL("dlopen(\"" RUN_DIR "/libbar.dylib\" failed with error: %s", dlerror());
52 }
53 if (gInitialisersCalled != 3) {
54 FAIL("gInitialisersCalled != 3");
55 }
56 }
57 // Now unload foo which shouldn't do anything.
58 result = dlclose(fooHandle);
59 if (result != 0) {
60 FAIL("dlclose(\"" RUN_DIR "/libfoo.dylib\") returned %c", result);
61 }
62
63 // Open foo again which shouldn't do anything.
64 {
65 fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
66 if (!fooHandle) {
67 FAIL("dlopen(\"" RUN_DIR "/libfoo.dylib\" failed with error: %s", dlerror());
68 }
69 if (gInitialisersCalled != 3) {
70 FAIL("gInitialisersCalled != 3");
71 }
72 }
73
74 PASS("Success");
75 }
76