1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (dylib.c).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
25 #include <dynamic/dylib.h>
26 #include <encodings/utf.h>
29 #include <orbis/libkernel.h>
35 #include <compat/posix_string.h>
43 /* Assume W-functions do not work below Win2K and Xbox platforms */
44 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
53 static char last_dyn_error[512];
55 static void set_dl_error(void)
57 DWORD err = GetLastError();
59 if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
60 FORMAT_MESSAGE_FROM_SYSTEM,
63 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
65 sizeof(last_dyn_error) - 1,
67 snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
68 "unknown error %lu", err);
74 * @path : Path to libretro core library.
76 * Platform independent dylib loading.
78 * @return Library handle on success, otherwise NULL.
80 dylib_t dylib_load(const char *path)
84 int prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
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;
93 relative_path_abbrev[0] = '\0';
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");
98 fill_pathname_abbreviate_special(relative_path_abbrev, path, sizeof(relative_path_abbrev));
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])) { }
106 path_wide = utf8_to_utf16_string_alloc(relative_path);
107 lib = LoadPackagedLibrary(path_wide, 0);
109 #elif defined(LEGACY_WIN32)
110 dylib_t lib = LoadLibrary(path);
112 wchar_t *path_wide = utf8_to_utf16_string_alloc(path);
113 dylib_t lib = LoadLibraryW(path_wide);
118 SetErrorMode(prevmode);
126 last_dyn_error[0] = 0;
129 dylib_t lib = (dylib_t)sceKernelLoadStartModule(path, 0, NULL, 0, NULL, &res);
131 dylib_t lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
136 char *dylib_error(void)
139 if (last_dyn_error[0])
140 return last_dyn_error;
143 return (char*)dlerror();
147 function_t dylib_proc(dylib_t lib, const char *proc)
152 HMODULE mod = (HMODULE)lib;
156 /* GetModuleHandle is not available on UWP */
157 /* It's not possible to lookup symbols in current executable
162 mod = GetModuleHandle(NULL);
165 if (!(sym = (function_t)GetProcAddress(mod, proc)))
170 last_dyn_error[0] = 0;
172 void *ptr_sym = NULL;
177 sceKernelDlsym((SceKernelModule)lib, proc, &ptr_sym);
178 memcpy(&sym, &ptr_sym, sizeof(void*));
181 void *ptr_sym = NULL;
184 ptr_sym = dlsym(lib, proc);
187 void *handle = dlopen(NULL, RTLD_LAZY);
190 ptr_sym = dlsym(handle, proc);
195 /* Dirty hack to workaround the non-legality of
196 * (void*) -> fn-pointer casts. */
197 memcpy(&sym, &ptr_sym, sizeof(void*));
205 * @lib : Library handle.
207 * Frees library handle.
209 void dylib_close(dylib_t lib)
212 if (!FreeLibrary((HMODULE)lib))
214 last_dyn_error[0] = 0;
217 sceKernelStopUnloadModule((SceKernelModule)lib, 0, NULL, 0, NULL, &res);