dyld-733.8.tar.gz
[apple/dyld.git] / testing / test-cases / dlsym-handle.dtest / main.c
1
2 // BUILD: $CC base.c -dynamiclib -install_name $RUN_DIR/libbase.dylib -o $BUILD_DIR/libbase.dylib
3 // BUILD: $CC foo.c -dynamiclib $BUILD_DIR/libbase.dylib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
4 // BUILD: $CC bar.c -dynamiclib $BUILD_DIR/libbase.dylib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib
5 // BUILD: $CC main.c -o $BUILD_DIR/dlsym-handle.exe -DRUN_DIR="$RUN_DIR"
6
7 // RUN: ./dlsym-handle.exe
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <dlfcn.h>
12 #include <mach-o/dyld_priv.h>
13
14
15 // verify RTLD_DEFAULT search order
16
17 int mainSymbol = 4;
18
19 int main()
20 {
21 printf("[BEGIN] dlsym-handle\n");
22
23 void* fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
24 if ( fooHandle == NULL ) {
25 printf("[FAIL] dlsym-handle: libfoo.dylib could not be loaded, %s\n", dlerror());
26 return 0;
27 }
28
29 void* barHandle = dlopen(RUN_DIR "/libbar.dylib", RTLD_LAZY);
30 if ( barHandle == NULL ) {
31 printf("[FAIL] dlsym-handle: libbar.dylib could not be loaded, %s\n", dlerror());
32 return 0;
33 }
34
35 // verify fooHandle does not find mainSymbol
36 if ( dlsym(fooHandle, "mainSymbol") != NULL ) {
37 printf("[FAIL] dlsym-handle: mainSymbol was found with fooHandle\n");
38 return 0;
39 }
40
41 // verify fooHandle can find foo
42 if ( dlsym(fooHandle, "foo") == NULL ) {
43 printf("[FAIL] dlsym-handle: foo not found with fooHandle\n");
44 return 0;
45 }
46
47 // verify fooHandle can find base
48 if ( dlsym(fooHandle, "base") == NULL ) {
49 printf("[FAIL] dlsym-handle: base not found with fooHandle\n");
50 return 0;
51 }
52
53 // verify fooHandle cannot find bar
54 if ( dlsym(fooHandle, "bar") != NULL ) {
55 printf("[FAIL] dlsym-handle: bar found with fooHandle\n");
56 return 0;
57 }
58
59 // verify barHandle can find bar
60 if ( dlsym(barHandle, "bar") == NULL ) {
61 printf("[FAIL] dlsym-handle: bar not found with barHandle\n");
62 return 0;
63 }
64
65 // verify barHandle can find base
66 if ( dlsym(barHandle, "base") == NULL ) {
67 printf("[FAIL] dlsym-handle: base not found with barHandle\n");
68 return 0;
69 }
70
71 // verify barHandle cannot find foo
72 if ( dlsym(barHandle, "foo") != NULL ) {
73 printf("[FAIL] dlsym-handle: foo found with barHandle\n");
74 return 0;
75 }
76
77 // verify renamed and re-exported symbols work
78 if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
79 printf("[FAIL] dlsym-handle: strcmp not found\n");
80 return 0;
81 }
82
83 // verify bad handle errors
84 if ( dlsym((void*)0xdeadbeef, "malloc") != NULL ) {
85 printf("[FAIL] dlsym-handle: malloc found with bad handle\n");
86 return 0;
87 }
88 else {
89 const char* message = dlerror();
90 if ( strstr(message, "invalid") == NULL ) {
91 printf("[FAIL] dlsym-handle: invalid handle error message missing 'invalid'\n");
92 return 0;
93 }
94 }
95
96 printf("[PASS] dlsym-handle\n");
97 return 0;
98 }
99