Skip to content

Commit

Permalink
Fixed library loading on Windows
Browse files Browse the repository at this point in the history
Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed May 2, 2024
1 parent 5e9ac64 commit 9237be3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 160 deletions.
1 change: 1 addition & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ libleech_la_SOURCES = leech.c \
table.h table.c \
utils.h utils.c \
sha1.h sha1.c \
module.h module.c \
definitions.h
libleech_la_LDFLAGS = -ldl -lm
77 changes: 77 additions & 0 deletions lib/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "module.h"

#include <stdlib.h>

#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif // HAVE_DLFCN_H

#if _WIN32
#include <libloaderapi.h>
#endif // _WIN32

#include "logger.h"

void *LCH_ModuleLoad(const char *const path) {
LCH_LOG_DEBUG("Loading dynamic shared library '%s'", path);
#if HAVE_DLFCN_H
void *handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
LCH_LOG_ERROR("Failed to load dynamic shared library '%s': %s", path,
dlerror());
return NULL;
}
return handle;
#elif defined(_WIN32)
void *handle = LoadLibraryA(path);
if (handle == NULL) {
LCH_LOG_ERROR("Failed to load dynamic shared library '%s': Error code %lu",
path, GetLastError());
}
return handle;
#else
LCH_LOG_ERROR("Failed to load dynamic shared library '%s'", path);
return NULL;
#endif
}

void *LCH_ModuleGetSymbol(void *const handle, const char *const symbol) {
LCH_LOG_DEBUG("Obtaining address of symbol '%s'", symbol);
#if HAVE_DLFCN_H
void *address = dlsym(handle, symbol);
if (address == NULL) {
LCH_LOG_ERROR("Failed to obtain address of symbol '%s': %s", symbol,
dlerror());
return NULL;
}
return address;
#elif defined(_WIN32)
void *address = GetProcAddress(handle, symbol);
if (address == NULL) {
LCH_LOG_ERROR("Failed to obtain address of symbol '%s': Error code %lu",
symbol, GetLastError());
}
return address;
#else
LCH_LOG_ERROR("Failed to obtain address of symbol '%s'", symbol);
return NULL;
#endif
}

void LCH_ModuleDestroy(void *const handle) {
if (handle == NULL) {
return;
}
#if HAVE_DLFCN_H
if (dlclose(handle) != 0) {
LCH_LOG_WARNING("Failed to release handle to dynamic shared library: %s",
dlerror());
}
#elif defined(_WIN32)
if (FreeLibrary(handle) == 0) {
LCH_LOG_WARNING(
"Failed to release handle to dynamic shared library: Error code %lu",
GetLastError());
}
#endif
}
12 changes: 12 additions & 0 deletions lib/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _LEECH_MODULE_H
#define _LEECH_MODULE_H

#include <stdbool.h>

void *LCH_ModuleLoad(const char *path);

void *LCH_ModuleGetSymbol(void *handle, const char *const symbol);

void LCH_ModuleDestroy(void *handle);

#endif // _LEECH_MODULE_H
Loading

0 comments on commit 9237be3

Please sign in to comment.