dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dyld-insert-library-rpath.dtest / main.cpp
1
2 // BOOT_ARGS: dyld_flags=2
3
4 // BUILD: mkdir -p $BUILD_DIR/lib
5 // BUILD: $CC main.cpp -std=c++11 -o $BUILD_DIR/rpath_insert_main.exe -Wl,-rpath,$RUN_DIR/lib
6 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/lib/libfoo.dylib
7 // BUILD: $CC bar.c -dynamiclib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib
8 // BUILD: $CC baz.c -dynamiclib -install_name $RUN_DIR/libbaz.dylib -o $BUILD_DIR/libbaz.dylib
9 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/rpath_insert_main.exe
10
11 // Load foo with @rpath, bar with @executable_path, and baz with @loader_path
12
13 // Note, dyld2 only supports DYLD_INSERT_LIBRARIES with @executable path so we expect failures on @rpath and @loader_path
14
15 // RUN: DYLD_INSERT_LIBRARIES="@rpath/libfoo.dylib" DYLD_AMFI_FAKE=0xFF ./rpath_insert_main.exe libfoo.dylib
16 // RUN: DYLD_INSERT_LIBRARIES="@executable_path/libbar.dylib" ./rpath_insert_main.exe libbar.dylib
17 // RUN: DYLD_INSERT_LIBRARIES="@loader_path/libbaz.dylib" DYLD_AMFI_FAKE=0xFF ./rpath_insert_main.exe libbaz.dylib
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <mach-o/dyld_priv.h>
24
25 bool gFoundLibrary = false;
26 const char* gLibraryName = NULL;
27
28 bool wasImageLoaded(const char* libraryName) {
29 gFoundLibrary = false;
30 gLibraryName = libraryName;
31 _dyld_register_for_image_loads([](const mach_header* mh, const char* path, bool unloadable) {
32 if ( strstr(path, gLibraryName) != NULL ) {
33 gFoundLibrary = true;
34 }
35 });
36 return gFoundLibrary;
37 }
38
39 int main(int argc, const char* argv[])
40 {
41 printf("[BEGIN] dyld-insert-library-rpath\n");
42
43 if (argc != 2) {
44 printf("[FAIL] dyld-insert-library-rpath: expected library name\n");
45 return 0;
46 }
47
48 bool expectInsertFailure = getenv("DYLD_AMFI_FAKE") != NULL;
49
50 if (wasImageLoaded(argv[1])) {
51 // Image was loaded, but make sure that is what we wanted to happen
52 if ( expectInsertFailure ) {
53 printf("[FAIL] dyld-insert-library-rpath: expected insert to fail for '%s'\n", argv[1]);
54 return 0;
55 }
56 } else {
57 // Image was not loaded, so make sure we are ok with that
58 if ( !expectInsertFailure ) {
59 printf("[FAIL] dyld-insert-library-rpath: expected insert to pass for '%s'\n", argv[1]);
60 return 0;
61 }
62 }
63
64 printf("[PASS] dyld-insert-library-rpath\n");
65
66 return 0;
67 }