]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlsym-RTLD_MAIN_ONLY.dtest/main.c
2300ad100364aff1cabda70add9a30254ad18503
[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
14 // verify RTLD_MAIN_ONLY search order
15
16 int mainSymbol = 4;
17
18
19 // my local implemention of free
20 void free(void* p) { }
21
22
23 static bool symbolInImage(const char* symName, const char* image)
24 {
25 void* sym = dlsym(RTLD_MAIN_ONLY, symName);
26 if ( sym == NULL )
27 return false;
28 const char* imagePath = dyld_image_path_containing_address(sym);
29 if ( imagePath == NULL )
30 return false;
31 return (strstr(imagePath, image) != NULL);
32 }
33
34
35
36
37 int main()
38 {
39 printf("[BEGIN] dlsym-RTLD_MAIN_ONLY\n");
40
41 // verify mainSymbol is found
42 if ( !symbolInImage("mainSymbol", "dlsym-RTLD_MAIN_ONLY") ) {
43 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: mainSymbol should have been found\n");
44 return 0;
45 }
46
47 // verify free is found in this program - not in OS
48 if ( !symbolInImage("free", "dlsym-RTLD_MAIN_ONLY") ) {
49 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: free\n");
50 return 0;
51 }
52
53 // verify foo is not found
54 if ( dlsym(RTLD_MAIN_ONLY, "foo") != NULL ) {
55 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo should not have been found\n");
56 return 0;
57 }
58
59 void* handle = dlopen(RUN_DIR "/libfoo-dynamic.dylib", RTLD_LAZY);
60 if ( handle == NULL ) {
61 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: libfoo-dynamic.dylib could not be loaded\n");
62 return 0;
63 }
64
65 // verify foo is still not found
66 if ( dlsym(RTLD_MAIN_ONLY, "foo") != NULL ) {
67 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo should not have been found after dlopen\n");
68 return 0;
69 }
70
71 // verify foo2 is not found in libfoo-dynamic.dylib", because RTLD_MAIN_ONLY only searches main executable
72 if ( dlsym(RTLD_MAIN_ONLY, "foo2") != NULL ) {
73 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo2 found but should not have been\n");
74 return 0;
75 }
76
77 printf("[PASS] dlsym-RTLD_MAIN_ONLY\n");
78 return 0;
79 }
80