Skip to content

Commit 2a58424

Browse files
committed
SPTCH-3534: Use already loaded version of JSCoreGTK if available
1 parent 6c6f61c commit 2a58424

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

execute_jscore.c

+29-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <string.h>
66

77
#include <dlfcn.h>
8+
#ifndef __APPLE__
9+
# include <link.h>
10+
#endif
811

912
#include <JavaScriptCore/JavaScript.h>
1013

@@ -376,13 +379,32 @@ bool proxy_execute_jscore_global_init(void) {
376379
g_proxy_execute_jscore.module = dlopen(
377380
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/JavaScriptCore", RTLD_LAZY | RTLD_LOCAL);
378381
#else
379-
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-4.1.so.0", RTLD_LAZY | RTLD_LOCAL);
380-
if (!g_proxy_execute_jscore.module)
381-
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-4.0.so.18", RTLD_LAZY | RTLD_LOCAL);
382-
if (!g_proxy_execute_jscore.module)
383-
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-3.0.so.0", RTLD_LAZY | RTLD_LOCAL);
384-
if (!g_proxy_execute_jscore.module)
385-
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-1.0.so.0", RTLD_LAZY | RTLD_LOCAL);
382+
const char *library_names[] = {"libjavascriptcoregtk-4.1.so.0", "libjavascriptcoregtk-4.0.so.18",
383+
"libjavascriptcoregtk-3.0.so.0", "libjavascriptcoregtk-1.0.so.0"};
384+
const size_t library_names_size = sizeof(library_names) / sizeof(library_names[0]);
385+
386+
// Use existing JavaScriptCoreGTK if already loaded
387+
struct link_map *map = NULL;
388+
void *current_process = dlopen(0, RTLD_LAZY);
389+
if (!current_process)
390+
return false;
391+
if (dlinfo(current_process, RTLD_DI_LINKMAP, &map) == 0) {
392+
while (map && !g_proxy_execute_jscore.module) {
393+
for (size_t i = 0; i < library_names_size; i++) {
394+
if (strstr(map->l_name, library_names[i])) {
395+
g_proxy_execute_jscore.module = dlopen(map->l_name, RTLD_NOLOAD | RTLD_LAZY | RTLD_LOCAL);
396+
break;
397+
}
398+
}
399+
map = map->l_next;
400+
}
401+
}
402+
dlclose(current_process);
403+
404+
// Load the first available version of the JavaScriptCoreGTK
405+
for (size_t i = 0; !g_proxy_execute_jscore.module && i < library_names_size; i++) {
406+
g_proxy_execute_jscore.module = dlopen(library_names[i], RTLD_LAZY | RTLD_LOCAL);
407+
}
386408
#endif
387409

388410
if (!g_proxy_execute_jscore.module)

0 commit comments

Comments
 (0)