]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlclose-static-terminator.dtest/main.c
dd19bbfd23651a4bfe3dd52f2841aae9e629238d
[apple/dyld.git] / testing / test-cases / dlclose-static-terminator.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libterm.dylib -o $BUILD_DIR/libterm.dylib
3 // BUILD: $CC main.c -o $BUILD_DIR/dlclose-term.exe -DRUN_DIR="$RUN_DIR"
4
5 // RUN: ./dlclose-term.exe
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <dlfcn.h>
10 #include <mach-o/dyld_priv.h>
11
12
13 // verify dlclose() runs static terminator
14
15 typedef void (*NotifyProc)(void);
16
17 static bool termDidRun = false;
18
19 static void termNotifyFunc()
20 {
21 termDidRun = true;
22 }
23
24 int main()
25 {
26 printf("[BEGIN] dlclose-static-terminator\n");
27
28 // load dylib
29 void* handle = dlopen(RUN_DIR "/libterm.dylib", RTLD_LAZY);
30 if ( handle == NULL ) {
31 printf("[FAIL] dlclose-static-terminator: libterm.dylib could not be loaded, %s\n", dlerror());
32 return 0;
33 }
34
35 // stuff pointer to my notifier
36 NotifyProc* pointerAddress = (NotifyProc*)dlsym(handle, "gNotifer");
37 if ( pointerAddress == NULL ) {
38 printf("[FAIL] dlclose-static-terminator: gNotifer not found in libterm.dylib\n");
39 return 0;
40 }
41 *pointerAddress = &termNotifyFunc;
42
43 // unload dylib
44 dlclose(handle);
45
46 if ( termDidRun )
47 printf("[PASS] dlclose-static-terminator\n");
48 else
49 printf("[FAIL] dlclose-static-terminator: terminator not run\n");
50
51 return 0;
52 }
53