dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-RTLD_NOLOAD.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/dlopen-RTLD_NOLOAD-basic.exe
4 // BUILD: cd $BUILD_DIR && ln -s libfoo.dylib libfoo-sym.dylib
5
6 // RUN: ./dlopen-RTLD_NOLOAD-basic.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_NOLOAD-basic\n");
17
18 ///
19 /// This tests that RTLD_NOLOAD finds existing dylib statically linked
20 ///
21 void* handle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_NOLOAD);
22 if ( handle == NULL ) {
23 printf("[FAIL] dlopen-RTLD_NOLOAD-basic: dlopen(libfoo.dylib, RTLD_NOLOAD) 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_NOLOAD-basic: dlsym(handle, \"foo\") failed but it should have worked: %s\n", dlerror());
29 return 0;
30 }
31
32 ///
33 /// This tests that RTLD_NOLOAD verifies that non-existant dylib returns NULL
34 ///
35 void* handle2 = dlopen(RUN_DIR "/libfobbulate.dylib", RTLD_NOLOAD);
36 if ( handle2 != NULL ) {
37 printf("[FAIL] dlopen-RTLD_NOLOAD-basic: dlopen(libfobbulate.dylib, RTLD_NOLOAD) succeeded but it should have failed\n");
38 return 0;
39 }
40
41
42 ///
43 /// This tests that RTLD_NOLOAD finds symlink to existing dylib
44 ///
45 void* handle3 = dlopen(RUN_DIR "/libfoo-sym.dylib", RTLD_NOLOAD);
46 if ( handle3 == NULL ) {
47 printf("[FAIL] dlopen-RTLD_NOLOAD-basic: dlopen(libfoo-sym.dylib, RTLD_NOLOAD) failed but it should have worked: %s\n", dlerror());
48 return 0;
49 }
50
51
52 ///
53 /// This tests that RTLD_NOLOAD of something in the dyld cache that is not yet loaded returns NULL
54 ///
55 void* handle4 = dlopen("/usr/lib/libz.1.dylib", RTLD_NOLOAD);
56 if ( handle4 != NULL ) {
57 printf("[FAIL] dlopen-RTLD_NOLOAD-basic: dlopen(libz.dylib, RTLD_NOLOAD) worked but it should have failed\n");
58 return 0;
59 }
60
61
62 printf("[PASS] dlopen-RTLD_NOLOAD-basic\n");
63 return 0;
64 }