dyld-750.5.tar.gz
[apple/dyld.git] / testing / test-cases / dlsym-RTLD_MAIN_ONLY.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_MAIN_ONLY.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./dlsym-RTLD_MAIN_ONLY.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_MAIN_ONLY 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_MAIN_ONLY, 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
38 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
39 // verify mainSymbol is found
40 if ( !symbolInImage("mainSymbol", "dlsym-RTLD_MAIN_ONLY") ) {
41 FAIL("mainSymbol should have been found");
42 }
43
44 // verify free is found in this program - not in OS
45 if ( !symbolInImage("free", "dlsym-RTLD_MAIN_ONLY") ) {
46 FAIL("free");
47 }
48
49 // verify foo is not found
50 if ( dlsym(RTLD_MAIN_ONLY, "foo") != NULL ) {
51 FAIL("foo should not have been found");
52 }
53
54 void* handle = dlopen(RUN_DIR "/libfoo-dynamic.dylib", RTLD_LAZY);
55 if ( handle == NULL ) {
56 FAIL("libfoo-dynamic.dylib could not be loaded");
57 }
58
59 // verify foo is still not found
60 if ( dlsym(RTLD_MAIN_ONLY, "foo") != NULL ) {
61 FAIL("foo should not have been found after dlopen");
62 }
63
64 // verify foo2 is not found in libfoo-dynamic.dylib", because RTLD_MAIN_ONLY only searches main executable
65 if ( dlsym(RTLD_MAIN_ONLY, "foo2") != NULL ) {
66 FAIL("foo2 found but should not have been");
67 }
68
69 PASS("Success");
70 }
71