Skip to content

Commit

Permalink
i#58 MacOS: injector for MacOS, part 2:
Browse files Browse the repository at this point in the history
Use DYLD_INSERT_LIBRARIES and proper names for loader-assisted late
injection.

drpreload.dylib needs explicit -init in order for its _init routine to be
called.

SVN-Revision: 2424
  • Loading branch information
derekbruening committed Dec 5, 2013
1 parent c766779 commit 1f8da27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
7 changes: 7 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ if (UNIX)
"${dynamorio_link_flags} -Xlinker --version-script -Xlinker ${export_list}")
endif (NOT HAVE_FVISIBILITY)

if (APPLE)
# Needs explicit -init (regardless of name: _init, init, etc.)
set(dynamorio_link_flags "${dynamorio_link_flags} -init __init")
endif (APPLE)

set_target_properties(dynamorio PROPERTIES
LINK_FLAGS "${dynamorio_link_flags}")

Expand Down Expand Up @@ -600,6 +605,8 @@ if (UNIX)
# Linker complains about unresolved symbols.
# This means we can't use preload by itself -- but these days we never do that.
target_link_libraries(${PRELOAD_NAME} dynamorio)
# Needs explicit -init (regardless of name: _init, init, etc.)
append_property_string(TARGET ${PRELOAD_NAME} LINK_FLAGS "-init __init")
endif (APPLE)

else (UNIX)
Expand Down
20 changes: 17 additions & 3 deletions core/unix/injector.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,23 @@ pre_execve_ld_preload(const char *dr_path)
cur_path == NULL ? "" : ":",
cur_path == NULL ? "" : cur_path);
NULL_TERMINATE_BUFFER(ld_lib_path);
#ifdef MACOS
setenv("DYLD_LIBRARY_PATH", ld_lib_path, true/*overwrite*/);
/* XXX: why does it not work w/o the full path? */
snprintf(ld_lib_path, BUFFER_SIZE_ELEMENTS(ld_lib_path),
"%.*s/%s:%.*s/%s",
last_slash - dr_path, dr_path, "libdrpreload.dylib",
last_slash - dr_path, dr_path, "libdynamorio.dylib");
setenv("DYLD_INSERT_LIBRARIES", ld_lib_path, true/*overwrite*/);
/* This is required to use DYLD_INSERT_LIBRARIES on apps that use
* two-level naming, but it can cause an app to run incorrectly.
* Long-term we'll want a true early injector.
*/
setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", true/*overwrite*/);
#else
setenv("LD_LIBRARY_PATH", ld_lib_path, true/*overwrite*/);
setenv("LD_PRELOAD", "libdynamorio.so libdrpreload.so",
true/*overwrite*/);
setenv("LD_PRELOAD", "libdynamorio.so libdrpreload.so", true/*overwrite*/);
#endif
if (verbose) {
printf("Setting LD_USE_LOAD_BIAS for PIEs so the loader will honor "
"DR's preferred base. (i#719)\n"
Expand Down Expand Up @@ -437,6 +451,7 @@ dr_inject_prepare_to_exec(const char *exe, const char **argv, void **data OUT)
{
dr_inject_info_t *info = create_inject_info(exe, argv);
int errcode = 0;
*data = info;
if (!exe_is_right_bitwidth(exe, &errcode) &&
errcode != WARN_IMAGE_MACHINE_TYPE_MISMATCH_EXE) {
free(info);
Expand All @@ -446,7 +461,6 @@ dr_inject_prepare_to_exec(const char *exe, const char **argv, void **data OUT)
info->pipe_fd = 0; /* No pipe. */
info->exec_self = true;
info->method = INJECT_LD_PRELOAD;
*data = info;
#ifdef STATIC_LIBRARY
setenv("DYNAMORIO_TAKEOVER_IN_INIT", "1", true/*overwrite*/);
#endif
Expand Down
11 changes: 10 additions & 1 deletion tools/drdeploy.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,16 @@ static bool check_dr_root(const char *dr_root, bool debug,
"lib64\\drpreinject.dll",
"lib64\\release\\dynamorio.dll",
"lib64\\debug\\dynamorio.dll"
#else /* UNIX */
#elif defined(MACOS)
"lib32/debug/libdrpreload.dylib",
"lib32/debug/libdynamorio.dylib",
"lib32/release/libdrpreload.dylib",
"lib32/release/libdynamorio.dylib",
"lib64/debug/libdrpreload.dylib",
"lib64/debug/libdynamorio.dylib",
"lib64/release/libdrpreload.dylib",
"lib64/release/libdynamorio.dylib"
#else /* LINUX */
"lib32/debug/libdrpreload.so",
"lib32/debug/libdynamorio.so",
"lib32/release/libdrpreload.so",
Expand Down

0 comments on commit 1f8da27

Please sign in to comment.