Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ subprojects/Vulkan-Headers-*/
#GNU Global Metadata
**/GPATH
**/GRTAGS
**/GTAGS
**/GTAGS

# clang cache
.cache
100 changes: 50 additions & 50 deletions src/real_dlsym.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,57 @@

#include <dlfcn.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}