dyld-750.5.tar.gz
[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 #include "test_support.h"
13
14 // verify dlclose() runs static terminator
15
16 typedef void (*NotifyProc)(void);
17
18 static bool termDidRun = false;
19
20 static void termNotifyFunc()
21 {
22 termDidRun = true;
23 }
24
25 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
26 // load dylib
27 void* handle = dlopen(RUN_DIR "/libterm.dylib", RTLD_LAZY);
28 if ( handle == NULL ) {
29 FAIL("libterm.dylib could not be loaded, %s", dlerror());
30 }
31
32 // stuff pointer to my notifier
33 NotifyProc* pointerAddress = (NotifyProc*)dlsym(handle, "gNotifer");
34 if ( pointerAddress == NULL ) {
35 FAIL("gNotifer not found in libterm.dylib");
36 }
37 *pointerAddress = &termNotifyFunc;
38
39 // unload dylib
40 dlclose(handle);
41
42 if ( termDidRun )
43 PASS("Success");
44 else
45 FAIL("terminator not run");
46 }
47