dyld-750.5.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 #include "test_support.h"
15
16 // verify RTLD_DEFAULT search order
17
18 int mainSymbol = 4;
19
20 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
21 void* fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
22 if ( fooHandle == NULL ) {
23 FAIL("libfoo.dylib could not be loaded, %s", dlerror());
24 }
25
26 void* barHandle = dlopen(RUN_DIR "/libbar.dylib", RTLD_LAZY);
27 if ( barHandle == NULL ) {
28 FAIL("libbar.dylib could not be loaded, %s", dlerror());
29 }
30
31 // verify fooHandle does not find mainSymbol
32 if ( dlsym(fooHandle, "mainSymbol") != NULL ) {
33 FAIL("mainSymbol was found with fooHandle");
34 }
35
36 // verify fooHandle can find foo
37 if ( dlsym(fooHandle, "foo") == NULL ) {
38 FAIL("foo not found with fooHandle");
39 }
40
41 // verify fooHandle can find base
42 if ( dlsym(fooHandle, "base") == NULL ) {
43 FAIL("base not found with fooHandle");
44 }
45
46 // verify fooHandle cannot find bar
47 if ( dlsym(fooHandle, "bar") != NULL ) {
48 FAIL("bar found with fooHandle");
49 }
50
51 // verify barHandle can find bar
52 if ( dlsym(barHandle, "bar") == NULL ) {
53 FAIL("bar not found with barHandle");
54 }
55
56 // verify barHandle can find base
57 if ( dlsym(barHandle, "base") == NULL ) {
58 FAIL("base not found with barHandle");
59 }
60
61 // verify barHandle cannot find foo
62 if ( dlsym(barHandle, "foo") != NULL ) {
63 FAIL("foo found with barHandle");
64 }
65
66 // verify renamed and re-exported symbols work
67 if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
68 FAIL("strcmp not found");
69 }
70
71 // verify bad handle errors
72 if ( dlsym((void*)0xdeadbeef, "malloc") != NULL ) {
73 FAIL("malloc found with bad handle");
74 }
75 else {
76 const char* message = dlerror();
77 if ( strstr(message, "invalid") == NULL ) {
78 FAIL(" invalid handle error message missing 'invalid'");
79 }
80 }
81
82 PASS("Success");
83 }
84