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