Skip to content

Commit 1b02384

Browse files
committed
Add util_open_library, util_close_library, util_get_symbol_addr
Add util_open_library(), util_close_library() and util_get_symbol_addr() to utils_common.h Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 9ea6ac9 commit 1b02384

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/utils/utils_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ static inline int is_running_in_proxy_lib(void) {
6262
size_t util_get_page_size(void);
6363
char *util_strncpy(char *dest, size_t destSize, const char *src, size_t n);
6464

65+
void *util_open_library(const char *filename);
66+
int util_close_library(void *handle);
67+
void *util_get_symbol_addr(void *handle, const char *symbol);
68+
6569
#define NOFUNCTION \
6670
do { \
6771
} while (0)

src/utils/utils_posix_common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
*/
99

10+
#include <dlfcn.h>
11+
#include <pthread.h>
1012
#include <stdlib.h>
1113
#include <string.h>
1214
#include <sys/syscall.h>
@@ -55,3 +57,13 @@ char *util_strncpy(char *dest, size_t destSize, const char *src, size_t n) {
5557
(void)destSize; // unused
5658
return strncpy(dest, src, n);
5759
}
60+
61+
void *util_open_library(const char *filename) {
62+
return dlopen(filename, RTLD_LAZY);
63+
}
64+
65+
int util_close_library(void *handle) { return dlclose(handle); }
66+
67+
void *util_get_symbol_addr(void *handle, const char *symbol) {
68+
return dlsym(handle, symbol);
69+
}

src/utils/utils_windows_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <windows.h>
1111

12+
#include <libloaderapi.h>
1213
#include <processenv.h>
1314

1415
#include "utils_concurrency.h"
@@ -54,3 +55,17 @@ char *util_strncpy(char *dest, size_t destSize, const char *src, size_t n) {
5455

5556
return dest;
5657
}
58+
59+
void *util_open_library(const char *filename) {
60+
return LoadLibrary(TEXT(filename));
61+
}
62+
63+
int util_close_library(void *handle) {
64+
// If the FreeLibrary function succeeds, the return value is nonzero.
65+
// If the FreeLibrary function fails, the return value is zero.
66+
return (FreeLibrary(handle) == 0);
67+
}
68+
69+
void *util_get_symbol_addr(void *handle, const char *symbol) {
70+
return GetProcAddress(handle, symbol);
71+
}

0 commit comments

Comments
 (0)