dyld-750.5.tar.gz
[apple/dyld.git] / testing / test-cases / interpose-then-dlopen.dtest / main.c
1 // BUILD: $CC fooimpl.c -dynamiclib -o $BUILD_DIR/libfooimpl.dylib -install_name $RUN_DIR/libfooimpl.dylib
2 // BUILD: $CC foo.c -dynamiclib $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libfoo.dylib -Wl,-interposable_list,$SRC_DIR/interposable.txt -install_name $RUN_DIR/libfoo.dylib
3 // BUILD: $CC bar.c -bundle $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libbar.bundle -Wl,-interposable_list,$SRC_DIR/interposable.txt
4 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" -Wl,-interposable_list,$SRC_DIR/interposable.txt -o $BUILD_DIR/interpose-then-dlopen.exe
5 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/interpose-then-dlopen.exe
6 // BUILD: $CC interposer.c -dynamiclib $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libinterposer.dylib -install_name libinterposer.dylib
7
8 // RUN: DYLD_INSERT_LIBRARIES=libinterposer.dylib ./interpose-then-dlopen.exe
9
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <dlfcn.h>
16
17 #include "test_support.h"
18
19 // Note, libinterposer.dylib interposes interposableFoo
20 extern int interposableFoo();
21
22 // Note, libfoo interposes interposableBar
23 extern int interposableBar();
24
25 extern int callFunc();
26
27
28 static void tryImage(const char* path, int expectedFoo, int expectedBar)
29 {
30 void* handle = dlopen(path, RTLD_LAZY);
31 if ( handle == NULL ) {
32 FAIL("dlopen(\"%s\") error: %s", path, dlerror());
33 }
34
35 __typeof(&callFunc) callFooSym = (__typeof(&callFunc))dlsym(handle, "callFoo");
36 if ( callFooSym == NULL ) {
37 FAIL("dlsym(\"callFoo\") error: %s", dlerror());
38 }
39
40 int fooResult = callFooSym();
41 if ( fooResult != expectedFoo ) {
42 FAIL("callFoo() from %s not interposed as it returned %d", path, fooResult);
43 }
44
45 __typeof(&callFunc) callBarSym = (__typeof(&callFunc))dlsym(handle, "callBar");
46 if ( callBarSym == NULL ) {
47 FAIL("dlsym(\"callBar\") error: %s", dlerror());
48 }
49
50 int barResult = callBarSym();
51 if ( barResult != expectedBar ) {
52 FAIL("callBar() from %s not interposed as it returned %d", path, barResult);
53 }
54
55 }
56
57 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
58 tryImage(RUN_DIR "/libfoo.dylib", 4, 2);
59 tryImage(RUN_DIR "/libbar.bundle", 4, 100);
60
61 PASS("Success");
62 }