From 2b6d1f48672539503e37cd6d51a3d2022c627d10 Mon Sep 17 00:00:00 2001 From: Kazoku Date: Sun, 12 Oct 2025 01:48:37 +0700 Subject: [PATCH 1/2] fix: can't get dlopen() --- src/real_dlsym.cpp | 100 ++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/real_dlsym.cpp b/src/real_dlsym.cpp index ec74e4e..8bd6d89 100644 --- a/src/real_dlsym.cpp +++ b/src/real_dlsym.cpp @@ -6,37 +6,57 @@ #include #include +#include #include #include "real_dlsym.h" #include "elfhacks.h" -void *(*__dlopen)(const char *, int) = nullptr; -void *(*__dlsym)(void *, const char *) = nullptr; -static bool print_dlopen = getenv("IMGOVERLAY_DEBUG_DLOPEN") != nullptr; -static bool print_dlsym = getenv("IMGOVERLAY_DEBUG_DLSYM") != nullptr; +void *(*__dlopen)(const char *, int) = NULL; +void *(*__dlsym)(void *, const char *) = NULL; +char *(*__dlerror)(void) = NULL; +static bool print_dlopen; +static bool print_dlsym; -void get_real_functions() +static void get_real_functions() { eh_obj_t libdl; + int ret; - if (eh_find_obj(&libdl, "*libdl.so*")) { - if (eh_find_obj(&libdl, "*libc.so*")) { - fprintf(stderr, "can't get libdl.so and libc.so\n"); - exit(1); - } - } - - if (eh_find_sym(&libdl, "dlopen", (void **) &__dlopen)) { - fprintf(stderr, "can't get dlopen()\n"); - exit(1); + const char* libs[] = { +#if defined(__GLIBC__) + "*libdl.so*", +#endif + "*libc.so*", + "*libc.*.so*", + "*ld-musl-*.so*", + }; + + print_dlopen = getenv("IMGOVERLAY_DEBUG_DLOPEN") != NULL; + print_dlsym = getenv("IMGOVERLAY_DEBUG_DLSYM") != NULL; + + for (size_t i = 0; i < sizeof(libs) / sizeof(*libs); i++) + { + ret = eh_find_obj(&libdl, libs[i]); + if (ret) + continue; + + eh_find_sym(&libdl, "dlopen", (void **) &__dlopen); + eh_find_sym(&libdl, "dlsym", (void **) &__dlsym); + eh_find_sym(&libdl, "dlerror", (void **) &__dlerror); + eh_destroy_obj(&libdl); + + if (__dlopen && __dlsym && __dlerror) + break; + __dlopen = NULL; + __dlsym = NULL; + __dlerror = NULL; } - if (eh_find_sym(&libdl, "dlsym", (void **) &__dlsym)) { - fprintf(stderr, "can't get dlsym()\n"); - exit(1); + if (!__dlopen && !__dlsym && !__dlerror) + { + fprintf(stderr, "IMGOVERLAY: Can't get dlopen(), dlsym() and dlerror()\n"); + exit(ret ? ret : 1); } - - eh_destroy_obj(&libdl); } /** @@ -45,7 +65,7 @@ void get_real_functions() */ void *real_dlopen(const char *filename, int flag) { - if (__dlopen == nullptr) + if (__dlopen == NULL) get_real_functions(); void *result = __dlopen(filename, flag); @@ -76,7 +96,7 @@ void *real_dlopen(const char *filename, int flag) */ void *real_dlsym(void *handle, const char *symbol) { - if (__dlsym == nullptr) + if (__dlsym == NULL) get_real_functions(); void *result = __dlsym(handle, symbol); @@ -87,33 +107,13 @@ void *real_dlsym(void *handle, const char *symbol) return result; } -void* get_proc_address(const char* name) { - void (*func)() = (void (*)())real_dlsym(RTLD_NEXT, name); - return (void*)func; -} - -#ifdef HOOK_DLSYM -EXPORT_C_(void *) imgoverlay_find_glx_ptr(const char *name); -EXPORT_C_(void *) imgoverlay_find_egl_ptr(const char *name); - -EXPORT_C_(void*) dlsym(void * handle, const char * name) +char* real_dlerror(void) { - void* func = nullptr; -#ifdef HAVE_X11 - func = imgoverlay_find_glx_ptr(name); - if (func) { - //fprintf(stderr,"%s: local: %s\n", __func__ , name); - return func; - } -#endif - - func = imgoverlay_find_egl_ptr(name); - if (func) { - //fprintf(stderr,"%s: local: %s\n", __func__ , name); - return func; - } - - //fprintf(stderr,"%s: foreign: %s\n", __func__ , name); - return real_dlsym(handle, name); + if (__dlerror == NULL) + get_real_functions(); + return __dlerror(); } -#endif + +void* get_proc_address(const char* name) { + return real_dlsym(RTLD_NEXT, name); +} \ No newline at end of file From aeee7608f719e43780dd9b22495dd2442650f41e Mon Sep 17 00:00:00 2001 From: Kazoku Date: Sun, 12 Oct 2025 01:49:29 +0700 Subject: [PATCH 2/2] chore: ignore clang cache --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 50e3374..c97c1ff 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,7 @@ subprojects/Vulkan-Headers-*/ #GNU Global Metadata **/GPATH **/GRTAGS -**/GTAGS \ No newline at end of file +**/GTAGS + +# clang cache +.cache \ No newline at end of file