Skip to content

Commit 2290262

Browse files
committed
Improve hooking on macOS
1 parent 324b95b commit 2290262

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

doorstop.c

100644100755
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,35 +201,32 @@ int fclose_hook(FILE *stream) {
201201
__attribute__ ((constructor)) void doorstop_setup() {
202202
plthook_t *hook;
203203

204-
if(plthook_open(&hook, NULL) != 0) {
204+
// Some versions of Unity (especially macOS) ship with UnityPlayer shared lib
205+
void *unity_player = plthook_handle_by_name("UnityPlayer");
206+
if(unity_player && plthook_open_by_handle(&hook, unity_player) == 0) {
207+
printf("Found UnityPlayer, hooking into it instead\n");
208+
}
209+
else if(plthook_open(&hook, NULL) != 0) {
205210
printf("Failed to open current process PLT! Cannot run Doorstop! Error: %s\n", plthook_error());
206211
return;
207212
}
208213

209214
if(plthook_replace(hook, "dlsym", &dlsym_hook, NULL) != 0)
210215
printf("Failed to hook dlsym, ignoring it. Error: %s\n", plthook_error());
211-
216+
212217
if(plthook_replace(hook, "fclose", &fclose_hook, NULL) != 0)
213218
printf("Failed to hook fclose, ignoring it. Error: %s\n", plthook_error());
214-
219+
215220
#if __APPLE__
216221
/*
217222
On older Unity versions, Mono methods are resolved by the OS's loader directly.
218223
Because of this, there is no dlsym, in which case we need to apply a PLT hook.
219224
*/
220-
void *mono_handle = NULL;
221-
uint32_t cnt = _dyld_image_count();
222-
for (uint32_t idx = 0; idx < cnt; idx++)
223-
{
224-
const char *image_name = idx ? _dyld_get_image_name(idx) : NULL;
225-
if (image_name && strstr(image_name, "libmono"))
226-
{
227-
mono_handle = dlopen(image_name, RTLD_LAZY | RTLD_NOLOAD);
228-
break;
229-
}
230-
}
225+
void *mono_handle = plthook_handle_by_name("libmono");
231226

232-
if(plthook_replace(hook, "mono_jit_init_version", &jit_init_hook, NULL) == 0 && mono_handle)
227+
if(plthook_replace(hook, "mono_jit_init_version", &jit_init_hook, NULL) != 0)
228+
printf("Failed to hook jit_init_version, ignoring it. Error: %s\n", plthook_error());
229+
else if(mono_handle)
233230
doorstop_init_mono_functions(mono_handle);
234231
#endif
235232

0 commit comments

Comments
 (0)