]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlsym-RTLD_DEFAULT.dtest/main.c
dyld-750.5.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 <string.h>
11 #include <mach-o/dyld_priv.h>
12
13 #include "test_support.h"
14
15 // verify RTLD_DEFAULT search order
16
17 int mainSymbol = 4;
18
19
20 // my local implemention of free
21 void free(void* p) { }
22
23
24 static bool symbolInImage(const char* symName, const char* image)
25 {
26 void* sym = dlsym(RTLD_DEFAULT, symName);
27 if ( sym == NULL )
28 return false;
29 const char* imagePath = dyld_image_path_containing_address(sym);
30 if ( imagePath == NULL )
31 return false;
32 return (strstr(imagePath, image) != NULL);
33 }
34
35
36
37 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
38 // verify mainSymbol is found in main executable
39 if ( !symbolInImage("mainSymbol", "dlsym-RTLD_DEFAULT") ) {
40 FAIL("mainSymbol");
41 }
42
43 // verify free is found in main executable, overrideing one in OS
44 if ( !symbolInImage("free", "dlsym-RTLD_DEFAULT") ) {
45 FAIL("free");
46 }
47
48 // verify foo is found in libfoo-static.dylib
49 if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
50 FAIL("foo not in libfoo-static.dylib");
51 }
52
53 void* handle = dlopen(RUN_DIR "/libfoo-dynamic.dylib", RTLD_LAZY);
54 if ( handle == NULL ) {
55 FAIL("libfoo-dynamic.dylib could not be loaded");
56 }
57
58 // verify foo is still found in statically linked lib
59 if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
60 FAIL("foo not in libfoo-static.dylib");
61 }
62
63 // verify foo2 is found in libfoo-dynamic.dylib"
64 if ( !symbolInImage("foo2", "libfoo-dynamic.dylib") ) {
65 FAIL("foo2 not in libfoo-dynamic.dylib");
66 }
67
68 // renamed and re-exported symbols work
69 if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
70 FAIL("strcmp not found");
71 }
72
73 PASS("Success");
74 }
75