From b67f905ac1464d094f4967557e6204356e7e2aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Thu, 16 Jan 2025 12:33:43 +0100 Subject: [PATCH] darwin: Fix Module.load() with alias The provided name could be an alias, so we may not be able to resolve it using the same name. When we're on macOS >= 13 or equivalent dyld versions, we can use _dyld_get_dlopen_image_header() to resolve the resulting module by address instead. --- gum/backend-darwin/gummodule-darwin.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gum/backend-darwin/gummodule-darwin.c b/gum/backend-darwin/gummodule-darwin.c index 6a989d9a3..5c517c6c0 100644 --- a/gum/backend-darwin/gummodule-darwin.c +++ b/gum/backend-darwin/gummodule-darwin.c @@ -179,12 +179,30 @@ gum_module_load (const gchar * module_name, { GumModule * module; gpointer handle; + static gsize initialized = FALSE; + static const struct mach_header * (* get_dlopen_image_header) (void * handle); handle = dlopen (module_name, RTLD_LAZY); if (handle == NULL) goto not_found; - module = gum_process_find_module_by_name (module_name); + if (g_once_init_enter (&initialized)) + { + get_dlopen_image_header = + dlsym (RTLD_DEFAULT, "_dyld_get_dlopen_image_header"); + + g_once_init_leave (&initialized, TRUE); + } + + if (get_dlopen_image_header != NULL) + { + module = gum_process_find_module_by_address ( + GUM_ADDRESS (get_dlopen_image_header (handle))); + } + else + { + module = gum_process_find_module_by_name (module_name); + } g_assert (module != NULL); dlclose (handle);