dyld-750.5.tar.gz
[apple/dyld.git] / testing / test-cases / init-term-segments.dtest / main.c
1
2
3 // BUILD: $CC foo.c -dynamiclib -fno-register-global-dtors-with-atexit -Wl,-segprot,__SOMETEXT,rx,rx -Wl,-segprot,__MORETEXT,rx,rx -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib
4 // BUILD: $CC main.c -o $BUILD_DIR/init-term-segments.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./init-term-segments.exe
7
8 #include <stdio.h>
9 #include <dlfcn.h>
10 #include <stdlib.h>
11
12 #include "test_support.h"
13
14 extern bool foo(bool* ptr);
15
16 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
17 void* h = dlopen(RUN_DIR "/libfoo.dylib", RTLD_NOW);
18 if (h == NULL) {
19 FAIL("dlerror = %s", dlerror());
20 }
21
22 void* fooSym = dlsym(RTLD_DEFAULT, "foo");
23 if ( fooSym == NULL ) {
24 FAIL("dlsym failure");
25 }
26
27 bool ranTerm = false;
28 bool ranInit = ((__typeof(&foo))fooSym)(&ranTerm);
29 if (!ranInit) {
30 FAIL("didn't run init");
31 }
32
33 if ( dlclose(h) != 0 ) {
34 FAIL("didn't dlclose");
35 }
36
37 if (!ranTerm) {
38 FAIL("didn't run term");
39 }
40
41 PASS("Success");
42 }
43