-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils_load_library.c
More file actions
115 lines (90 loc) · 2.91 KB
/
Copy pathutils_load_library.c
File metadata and controls
115 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
*
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#ifdef _WIN32
// Include "windows.h" first to ensure that all necessary types and function prototypes
// are available to other system headers that depend on it, such as "libloaderapi.h".
// clang-format off
#include <windows.h>
#include <libloaderapi.h>
// clang-format on
#else // _WIN32
#define _GNU_SOURCE 1
#include <dlfcn.h> // forces linking with libdl on Linux
#endif // !_WIN32
#include <stddef.h>
#include "utils_load_library.h"
#include "utils_log.h"
#ifdef _WIN32
void *utils_open_library(const char *filename, int userFlags) {
if (userFlags & UMF_UTIL_OPEN_LIBRARY_NO_LOAD) {
HMODULE hModule;
BOOL ret = GetModuleHandleEx(0, TEXT(filename), &hModule);
return ret ? hModule : NULL;
}
return LoadLibrary(TEXT(filename));
}
int utils_close_library(void *handle) {
// If the FreeLibrary function succeeds, the return value is nonzero.
// If the FreeLibrary function fails, the return value is zero.
return (FreeLibrary((HMODULE)handle) == 0);
}
void *utils_get_symbol_addr(void *handle, const char *symbol,
const char *libname) {
if (!handle) {
if (libname == NULL) {
return NULL;
}
handle = GetModuleHandle(libname);
}
void *addr = (void *)GetProcAddress((HMODULE)handle, symbol);
if (addr == NULL) {
LOG_ERR("Required symbol not found: %s", symbol);
}
return addr;
}
#else /* Linux */
void *utils_open_library(const char *filename, int userFlags) {
int dlopenFlags = RTLD_LAZY;
if (userFlags & UMF_UTIL_OPEN_LIBRARY_GLOBAL) {
dlopenFlags |= RTLD_GLOBAL;
}
if (userFlags & UMF_UTIL_OPEN_LIBRARY_NO_LOAD) {
dlopenFlags = RTLD_NOLOAD | RTLD_NOW;
}
void *handle = dlopen(filename, dlopenFlags);
if (handle == NULL) {
if (userFlags & UMF_UTIL_OPEN_LIBRARY_NO_LOAD) {
// if we are not loading the library, just return NULL if it is not
// found
LOG_DEBUG("Library %s not found, returning NULL", filename);
return NULL;
}
LOG_FATAL("dlopen(%s) failed with error: %s", filename, dlerror());
return NULL;
}
LOG_DEBUG("Opened library %s with handle %p", filename, handle);
return handle;
}
int utils_close_library(void *handle) {
LOG_DEBUG("Closing library handle %p", handle);
return dlclose(handle);
}
void *utils_get_symbol_addr(void *handle, const char *symbol,
const char *libname) {
(void)libname; //unused
if (!handle) {
handle = RTLD_DEFAULT;
}
void *addr = dlsym(handle, symbol);
if (addr == NULL) {
LOG_ERR("required symbol not found: %s (error: %s)", symbol, dlerror());
}
return addr;
}
#endif