dyld-625.13.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-RTLD_LOCAL-hides.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC bar.c -dynamiclib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib
4 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-RTLD_LOCAL-hides.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./dlopen-RTLD_LOCAL-hides.exe
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <dlfcn.h>
12
13
14 int main()
15 {
16 printf("[BEGIN] dlopen-RTLD_LOCAL-hides\n");
17
18 ///
19 /// This tests that RTLD_LOCAL prevents RTLD_DEFAULT from finding symbols, but can be found via handle
20 ///
21 void* handle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LOCAL);
22 if ( handle == NULL ) {
23 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlopen(libfoo.dylib, RTLD_LOCAL) failed but it should have worked: %s\n", dlerror());
24 return 0;
25 }
26 void* sym = dlsym(handle, "foo");
27 if ( sym == NULL ) {
28 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlsym(handle, \"foo\") failed but it should have worked: %s\n", dlerror());
29 return 0;
30 }
31 void* sym2 = dlsym(RTLD_DEFAULT, "foo");
32 if ( sym2 != NULL ) {
33 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlsym(RTLD_DEFAULT, \"foo\") succeeded but it should have failed\n");
34 return 0;
35 }
36
37
38 ///
39 /// This tests that RTLD_GLOBAL after RTLD_LOCAL allows RTLD_DEFAULT to find symbols
40 ///
41 void* handle2 = dlopen(RUN_DIR "/libfoo.dylib", RTLD_GLOBAL);
42 if ( handle2 == NULL ) {
43 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlopen(libfoo.dylib, RTLD_GLOBAL) failed but it should have worked: %s\n", dlerror());
44 return 0;
45 }
46 void* sym3 = dlsym(RTLD_DEFAULT, "foo");
47 if ( sym3 == NULL ) {
48 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlsym(RTLD_DEFAULT, \"foo\") failed after upgrading to RTLD_GLOBAL\n");
49 return 0;
50 }
51
52
53 ///
54 /// This tests that RTLD_LOCAL after RTLD_GLOBAL does not block RTLD_DEFAULT from finding symbols
55 ///
56 void* handle3 = dlopen(RUN_DIR "/libbar.dylib", RTLD_GLOBAL);
57 if ( handle3 == NULL ) {
58 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlopen(libbar.dylib, RTLD_GLOBAL) failed but it should have worked: %s\n", dlerror());
59 return 0;
60 }
61 void* handle4 = dlopen(RUN_DIR "/libbar.dylib", RTLD_LOCAL);
62 if ( handle4 == NULL ) {
63 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlopen(libbar.dylib, RTLD_LOCAL) failed but it should have worked: %s\n", dlerror());
64 return 0;
65 }
66 void* sym4 = dlsym(RTLD_DEFAULT, "bar");
67 if ( sym4 == NULL ) {
68 printf("[FAIL] dlopen-RTLD_LOCAL-hides: dlsym(RTLD_DEFAULT, \"bar\") failed but it should have worked: %s\n", dlerror());
69 return 0;
70 }
71
72 printf("[PASS] dlopen-RTLD_LOCAL-hides\n");
73 return 0;
74 }