git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / dynamic / dylib.c
1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (dylib.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <dynamic/dylib.h>
26 #include <encodings/utf.h>
27
28 #if defined(ORBIS)
29 #include <orbis/libkernel.h>
30 #endif
31
32 #ifdef NEED_DYNAMIC
33
34 #ifdef _WIN32
35 #include <compat/posix_string.h>
36 #include <windows.h>
37 #else
38 #if !defined(ORBIS)
39 #include <dlfcn.h>
40 #endif
41 #endif
42
43 /* Assume W-functions do not work below Win2K and Xbox platforms */
44 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
45
46 #ifndef LEGACY_WIN32
47 #define LEGACY_WIN32
48 #endif
49
50 #endif
51
52 #ifdef _WIN32
53 static char last_dyn_error[512];
54
55 static void set_dl_error(void)
56 {
57    DWORD err = GetLastError();
58
59    if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
60             FORMAT_MESSAGE_FROM_SYSTEM,
61             NULL,
62             err,
63             MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
64             last_dyn_error,
65             sizeof(last_dyn_error) - 1,
66             NULL) == 0)
67       snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
68             "unknown error %lu", err);
69 }
70 #endif
71
72 /**
73  * dylib_load:
74  * @path                         : Path to libretro core library.
75  *
76  * Platform independent dylib loading.
77  *
78  * @return Library handle on success, otherwise NULL.
79  **/
80 dylib_t dylib_load(const char *path)
81 {
82 #ifdef _WIN32
83 #ifndef __WINRT__
84    int prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
85 #endif
86 #ifdef __WINRT__
87    dylib_t lib;
88    /* On UWP, you can only load DLLs inside your install directory, using a special function that takes a relative path */
89    char relative_path_abbrev[PATH_MAX_LENGTH];
90    char *relative_path = relative_path_abbrev;
91    wchar_t *path_wide  = NULL;
92
93    relative_path_abbrev[0] = '\0';
94
95    if (!path_is_absolute(path))
96       RARCH_WARN("Relative path in dylib_load! This is likely an attempt to load a system library that will fail\n");
97
98    fill_pathname_abbreviate_special(relative_path_abbrev, path, sizeof(relative_path_abbrev));
99
100    /* Path to dylib_load is not inside app install directory.
101     * Loading will probably fail. */
102    if (relative_path[0] != ':' || !PATH_CHAR_IS_SLASH(relative_path[1])) { }
103    else
104       relative_path += 2;
105
106    path_wide = utf8_to_utf16_string_alloc(relative_path);
107    lib       = LoadPackagedLibrary(path_wide, 0);
108    free(path_wide);
109 #elif defined(LEGACY_WIN32)
110    dylib_t lib        = LoadLibrary(path);
111 #else
112    wchar_t *path_wide = utf8_to_utf16_string_alloc(path);
113    dylib_t lib        = LoadLibraryW(path_wide);
114    free(path_wide);
115 #endif
116
117 #ifndef __WINRT__
118    SetErrorMode(prevmode);
119 #endif
120
121    if (!lib)
122    {
123       set_dl_error();
124       return NULL;
125    }
126    last_dyn_error[0] = 0;
127 #elif defined(ORBIS)
128    int res;
129    dylib_t lib = (dylib_t)sceKernelLoadStartModule(path, 0, NULL, 0, NULL, &res);
130 #else
131    dylib_t lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
132 #endif
133    return lib;
134 }
135
136 char *dylib_error(void)
137 {
138 #ifdef _WIN32
139    if (last_dyn_error[0])
140       return last_dyn_error;
141    return NULL;
142 #else
143    return (char*)dlerror();
144 #endif
145 }
146
147 function_t dylib_proc(dylib_t lib, const char *proc)
148 {
149    function_t sym;
150
151 #ifdef _WIN32
152    HMODULE mod = (HMODULE)lib;
153    if (!mod)
154    {
155 #ifdef __WINRT__
156       /* GetModuleHandle is not available on UWP */
157       /* It's not possible to lookup symbols in current executable
158        * on UWP. */
159       DebugBreak();
160       return NULL;
161 #else
162       mod = GetModuleHandle(NULL);
163 #endif
164    }
165    if (!(sym = (function_t)GetProcAddress(mod, proc)))
166    {
167       set_dl_error();
168       return NULL;
169    }
170    last_dyn_error[0] = 0;
171 #elif defined(ORBIS)
172    void *ptr_sym = NULL;
173    sym = NULL;
174
175    if (lib)
176    {
177      sceKernelDlsym((SceKernelModule)lib, proc, &ptr_sym);
178      memcpy(&sym, &ptr_sym, sizeof(void*));
179    }
180 #else
181    void *ptr_sym = NULL;
182
183    if (lib)
184       ptr_sym = dlsym(lib, proc);
185    else
186    {
187       void *handle = dlopen(NULL, RTLD_LAZY);
188       if (handle)
189       {
190          ptr_sym = dlsym(handle, proc);
191          dlclose(handle);
192       }
193    }
194
195    /* Dirty hack to workaround the non-legality of
196     * (void*) -> fn-pointer casts. */
197    memcpy(&sym, &ptr_sym, sizeof(void*));
198 #endif
199
200    return sym;
201 }
202
203 /**
204  * dylib_close:
205  * @lib                          : Library handle.
206  *
207  * Frees library handle.
208  **/
209 void dylib_close(dylib_t lib)
210 {
211 #ifdef _WIN32
212    if (!FreeLibrary((HMODULE)lib))
213       set_dl_error();
214    last_dyn_error[0] = 0;
215 #elif defined(ORBIS)
216    int res;
217    sceKernelStopUnloadModule((SceKernelModule)lib, 0, NULL, 0, NULL, &res);
218 #else
219 #ifndef NO_DLCLOSE
220    dlclose(lib);
221 #endif
222 #endif
223 }
224
225 #endif