dyld-750.5.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 #include "test_support.h"
14
15 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
16 ///
17 /// This tests that RTLD_LOCAL prevents RTLD_DEFAULT from finding symbols, but can be found via handle
18 ///
19 void* handle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LOCAL);
20 if ( handle == NULL ) {
21 FAIL("dlopen(\"libfoo.dylib\", RTLD_LOCAL) failed but it should have worked: %s", dlerror());
22 }
23 void* sym = dlsym(handle, "foo");
24 if ( sym == NULL ) {
25 FAIL("dlsym(handle, \"foo\") failed but it should have worked: %s", dlerror());
26 }
27 void* sym2 = dlsym(RTLD_DEFAULT, "foo");
28 if ( sym2 != NULL ) {
29 FAIL("dlsym(RTLD_DEFAULT, \"foo\") succeeded but it should have failed");
30 }
31
32 ///
33 /// This tests that RTLD_GLOBAL after RTLD_LOCAL allows RTLD_DEFAULT to find symbols
34 ///
35 void* handle2 = dlopen(RUN_DIR "/libfoo.dylib", RTLD_GLOBAL);
36 if ( handle2 == NULL ) {
37 FAIL("dlopen(\"libfoo.dylib\", RTLD_GLOBAL) failed but it should have worked: %s", dlerror());
38 }
39 void* sym3 = dlsym(RTLD_DEFAULT, "foo");
40 if ( sym3 == NULL ) {
41 FAIL("dlsym(RTLD_DEFAULT, \"foo\") failed after upgrading to RTLD_GLOBAL");
42 }
43
44 ///
45 /// This tests that RTLD_LOCAL after RTLD_GLOBAL does not block RTLD_DEFAULT from finding symbols
46 ///
47 void* handle3 = dlopen(RUN_DIR "/libbar.dylib", RTLD_GLOBAL);
48 if ( handle3 == NULL ) {
49 FAIL("dlopen(\"libbar.dylib\", RTLD_GLOBAL) failed but it should have worked: %s", dlerror());
50 }
51 void* handle4 = dlopen(RUN_DIR "/libbar.dylib", RTLD_LOCAL);
52 if ( handle4 == NULL ) {
53 FAIL("dlopen(\"libbar.dylib\", RTLD_LOCAL) failed but it should have worked: %s", dlerror());
54 }
55 void* sym4 = dlsym(RTLD_DEFAULT, "bar");
56 if ( sym4 == NULL ) {
57 FAIL("dlsym(RTLD_DEFAULT, \"bar\") failed but it should have worked: %s", dlerror());
58 }
59
60 PASS("Success");
61 }