]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/weak-coalesce-dlopen.dtest/main.cpp
dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / weak-coalesce-dlopen.dtest / main.cpp
1
2 // BUILD: $CC foo.cpp -lc++ -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC main.cpp -lc++ -o $BUILD_DIR/weak-coalesce-dlopen.exe -DRUN_DIR="$RUN_DIR"
4
5 // RUN: ./weak-coalesce-dlopen.exe
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <dlfcn.h>
11 #include <new>
12
13 #include "test_support.h"
14
15 extern void* foo();
16
17 void* lastAllocatedValue = NULL;
18
19 void* operator new(size_t size) {
20 lastAllocatedValue = malloc(size);
21 return lastAllocatedValue;
22 }
23
24 int main()
25 {
26 // The value we allocate should come from our new function
27 int* value1 = new int(1);
28 if ( value1 != lastAllocatedValue ) {
29 FAIL("value1 (%p) != lastAllocatedValue (%p)", value1, lastAllocatedValue);
30 }
31
32 // dlopen foo which defines "foo"
33 // In dyld2, for chained fixups, this will run weakBindOld which patches the cache for
34 // weak defs. That patching will fail if the cache uses __DATA_CONST and was not marked as
35 // RW prior to patching
36 void* handle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_FIRST);
37 if ( handle == NULL ) {
38 FAIL("dlopen(\"%s\") failed with: %s", RUN_DIR "/libfoo.dylib", dlerror());
39 }
40
41 const void* symFoo = dlsym(handle, "foo");
42 if ( symFoo == NULL ) {
43 FAIL("dlsym(handle, foo) failed");
44 }
45
46 // The value foo allocates should come from our new function
47 void* value2 = ((__typeof(&foo))symFoo)();
48 if ( value2 != lastAllocatedValue ) {
49 FAIL("value2 (%p) != lastAllocatedValue (%p)", value2, lastAllocatedValue);
50 }
51
52 PASS("weak-coalesce-dlopen");
53 }
54