]>
git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlsym-RTLD_MAIN_ONLY.dtest/main.c
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"
6 // RUN: ./dlsym-RTLD_MAIN_ONLY.exe
10 #include <mach-o/dyld_priv.h>
13 // verify RTLD_MAIN_ONLY search order
18 // my local implemention of free
19 void free(void* p
) { }
22 static bool symbolInImage(const char* symName
, const char* image
)
24 void* sym
= dlsym(RTLD_MAIN_ONLY
, symName
);
27 const char* imagePath
= dyld_image_path_containing_address(sym
);
28 if ( imagePath
== NULL
)
30 return (strstr(imagePath
, image
) != NULL
);
38 printf("[BEGIN] dlsym-RTLD_MAIN_ONLY\n");
40 // verify mainSymbol is found
41 if ( !symbolInImage("mainSymbol", "dlsym-RTLD_MAIN_ONLY") ) {
42 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: mainSymbol should have been found\n");
46 // verify free is found in this program - not in OS
47 if ( !symbolInImage("free", "dlsym-RTLD_MAIN_ONLY") ) {
48 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: free\n");
52 // verify foo is not found
53 if ( dlsym(RTLD_MAIN_ONLY
, "foo") != NULL
) {
54 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo should not have been found\n");
58 void* handle
= dlopen(RUN_DIR
"/libfoo-dynamic.dylib", RTLD_LAZY
);
59 if ( handle
== NULL
) {
60 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: libfoo-dynamic.dylib could not be loaded\n");
64 // verify foo is still not found
65 if ( dlsym(RTLD_MAIN_ONLY
, "foo") != NULL
) {
66 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo should not have been found after dlopen\n");
70 // verify foo2 is not found in libfoo-dynamic.dylib", because RTLD_MAIN_ONLY only searches main executable
71 if ( dlsym(RTLD_MAIN_ONLY
, "foo2") != NULL
) {
72 printf("[FAIL] dlsym-RTLD_MAIN_ONLY: foo2 found but should not have been\n");
76 printf("[PASS] dlsym-RTLD_MAIN_ONLY\n");