]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlsym-RTLD_DEFAULT.dtest/main.c
dyld-519.2.2.tar.gz
[apple/dyld.git] / testing / test-cases / dlsym-RTLD_DEFAULT.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo-static.dylib -o $BUILD_DIR/libfoo-static.dylib
3 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo-dynamic.dylib -o $BUILD_DIR/libfoo-dynamic.dylib -DDYN
4 // BUILD: $CC main.c $BUILD_DIR/libfoo-static.dylib -o $BUILD_DIR/dlsym-RTLD_DEFAULT.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./dlsym-RTLD_DEFAULT.exe
7
8 #include <stdio.h>
9 #include <dlfcn.h>
10 #include <mach-o/dyld_priv.h>
11
12
13 // verify RTLD_DEFAULT search order
14
15 int mainSymbol = 4;
16
17
18 // my local implemention of free
19 void free(void* p) { }
20
21
22 static bool symbolInImage(const char* symName, const char* image)
23 {
24 void* sym = dlsym(RTLD_DEFAULT, symName);
25 if ( sym == NULL )
26 return false;
27 const char* imagePath = dyld_image_path_containing_address(sym);
28 if ( imagePath == NULL )
29 return false;
30 return (strstr(imagePath, image) != NULL);
31 }
32
33
34
35
36 int main()
37 {
38 printf("[BEGIN] dlsym-RTLD_DEFAULT\n");
39
40 // verify mainSymbol is found in main executable
41 if ( !symbolInImage("mainSymbol", "dlsym-RTLD_DEFAULT") ) {
42 printf("[FAIL] dlsym-RTLD_DEFAULT: mainSymbol\n");
43 return 0;
44 }
45
46 // verify free is found in main executable, overrideing one in OS
47 if ( !symbolInImage("free", "dlsym-RTLD_DEFAULT") ) {
48 printf("[FAIL] dlsym-RTLD_DEFAULT: free\n");
49 return 0;
50 }
51
52 // verify foo is found in libfoo-static.dylib
53 if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
54 printf("[FAIL] dlsym-RTLD_DEFAULT: foo not in libfoo-static.dylib\n");
55 return 0;
56 }
57
58 void* handle = dlopen(RUN_DIR "/libfoo-dynamic.dylib", RTLD_LAZY);
59 if ( handle == NULL ) {
60 printf("[FAIL] dlsym-RTLD_DEFAULT: libfoo-dynamic.dylib could not be loaded\n");
61 return 0;
62 }
63
64 // verify foo is still found in statically linked lib
65 if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
66 printf("[FAIL] dlsym-RTLD_DEFAULT: foo not in libfoo-static.dylib\n");
67 return 0;
68 }
69
70 // verify foo2 is found in libfoo-dynamic.dylib"
71 if ( !symbolInImage("foo2", "libfoo-dynamic.dylib") ) {
72 printf("[FAIL] dlsym-RTLD_DEFAULT: foo2 not in libfoo-dynamic.dylib\n");
73 return 0;
74 }
75
76 // renamed and re-exported symbols work
77 if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
78 printf("[FAIL] dlsym-RTLD_DEFAULT: strcmp not found\n");
79 return 0;
80 }
81
82 printf("[PASS] dlsym-RTLD_DEFAULT\n");
83 return 0;
84 }
85