dyld-733.8.tar.gz
[apple/dyld.git] / testing / test-cases / restrict-search.dtest / main.c
1 // BUILD_ONLY: MacOSX
2
3 // BUILD: mkdir $BUILD_DIR/lc
4 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/lc/libfoo.dylib -install_name /blah/libfoo.dylib
5 // BUILD: $CC main.c $BUILD_DIR/lc/libfoo.dylib -o $BUILD_DIR/restrict-search-lc-find.exe -Wl,-dyld_env,DYLD_LIBRARY_PATH=@loader_path/lc -DMODE=lc-find -DSHOULD_BE_FOUND=1
6 // BUILD: $CC main.c $BUILD_DIR/lc/libfoo.dylib -o $BUILD_DIR/restrict-search-lc-no-find.exe -Wl,-dyld_env,DYLD_LIBRARY_PATH=@loader_path/lc -DMODE=lc-no-find -sectcreate __RESTRICT __restrict /dev/null
7 // BUILD: mkdir $BUILD_DIR/rpath
8 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/rpath/libfoo.dylib -install_name @rpath/libfoo.dylib
9 // BUILD: $CC main.c $BUILD_DIR/rpath/libfoo.dylib -o $BUILD_DIR/restrict-search-rpath-find.exe -Wl,-rpath,@loader_path/rpath/ -DMODE=rpath-find -DSHOULD_BE_FOUND=1
10 // BUILD: $CC main.c $BUILD_DIR/rpath/libfoo.dylib -o $BUILD_DIR/restrict-search-rpath-no-find.exe -Wl,-rpath,@loader_path/rpath/ -DMODE=rpath-no-find -sectcreate __RESTRICT __restrict /dev/null
11
12
13 // RUN: ./restrict-search-lc-find.exe
14 // RUN: ./restrict-search-lc-no-find.exe
15 // RUN: ./restrict-search-rpath-find.exe
16 // RUN: ./restrict-search-rpath-no-find.exe
17
18
19
20 #include <stdio.h>
21 #include <dlfcn.h>
22
23 // Two ways to find libfoo.dylb: @rpath or DYLD_LIBRARY_PATH (set via LC_DYLD_ENVIRONMENT)
24 // These should work for non-restrictured programs.
25 // These should fail for restricted programs.
26 // By weak linking we can test if libfoo.dylib was found or not.
27
28
29 extern int foo() __attribute__((weak_import));
30
31
32 #define STRINGIFY2( x) #x
33 #define STRINGIFY(x) STRINGIFY2(x)
34
35
36 int main(int argc, const char* argv[])
37 {
38 printf("[BEGIN] restrict-search %s\n", STRINGIFY(MODE));
39 #if SHOULD_BE_FOUND
40 if ( &foo == NULL )
41 printf("[FAIL] restrict-search %s\n", STRINGIFY(MODE));
42 else
43 printf("[PASS] restrict-search %s\n", STRINGIFY(MODE));
44 #else
45 // dylib won't be found at runtime, so &foo should be NULL
46 if ( &foo == NULL )
47 printf("[PASS] restrict-search %s\n", STRINGIFY(MODE));
48 else
49 printf("[FAIL] restrict-search %s\n", STRINGIFY(MODE));
50 #endif
51
52 return 0;
53 }
54