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 #ifdef NEED_DYNAMIC
29 
30 #ifdef _WIN32
31 #include <compat/posix_string.h>
32 #include <windows.h>
33 #else
34 #include <dlfcn.h>
35 #endif
36 
37 /* Assume W-functions do not work below Win2K and Xbox platforms */
38 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
39 
40 #ifndef LEGACY_WIN32
41 #define LEGACY_WIN32
42 #endif
43 
44 #endif
45 
46 #ifdef _WIN32
47 static char last_dyn_error[512];
48 
set_dl_error(void)49 static void set_dl_error(void)
50 {
51    DWORD err = GetLastError();
52 
53    if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
54             FORMAT_MESSAGE_FROM_SYSTEM,
55             NULL,
56             err,
57             MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
58             last_dyn_error,
59             sizeof(last_dyn_error) - 1,
60             NULL) == 0)
61       snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
62             "unknown error %lu", err);
63 }
64 #endif
65 
66 /**
67  * dylib_load:
68  * @path                         : Path to libretro core library.
69  *
70  * Platform independent dylib loading.
71  *
72  * Returns: library handle on success, otherwise NULL.
73  **/
dylib_load(const char * path)74 dylib_t dylib_load(const char *path)
75 {
76 #ifdef _WIN32
77 #ifndef __WINRT__
78    int prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
79 #endif
80 #ifdef __WINRT__
81    dylib_t lib;
82    /* On UWP, you can only load DLLs inside your install directory, using a special function that takes a relative path */
83    char relative_path_abbrev[PATH_MAX_LENGTH];
84    char *relative_path = relative_path_abbrev;
85    wchar_t *path_wide  = NULL;
86 
87    relative_path_abbrev[0] = '\0';
88 
89    if (!path_is_absolute(path))
90       RARCH_WARN("Relative path in dylib_load! This is likely an attempt to load a system library that will fail\n");
91 
92    fill_pathname_abbreviate_special(relative_path_abbrev, path, sizeof(relative_path_abbrev));
93 
94    /* Path to dylib_load is not inside app install directory.
95     * Loading will probably fail. */
96    if (relative_path[0] != ':' || !PATH_CHAR_IS_SLASH(relative_path[1])) { }
97    else
98       relative_path += 2;
99 
100    path_wide = utf8_to_utf16_string_alloc(relative_path);
101    lib       = LoadPackagedLibrary(path_wide, 0);
102    free(path_wide);
103 #elif defined(LEGACY_WIN32)
104    dylib_t lib        = LoadLibrary(path);
105 #else
106    wchar_t *path_wide = utf8_to_utf16_string_alloc(path);
107    dylib_t lib        = LoadLibraryW(path_wide);
108    free(path_wide);
109 #endif
110 
111 #ifndef __WINRT__
112    SetErrorMode(prevmode);
113 #endif
114 
115    if (!lib)
116    {
117       set_dl_error();
118       return NULL;
119    }
120    last_dyn_error[0] = 0;
121 #else
122    dylib_t lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
123 #endif
124    return lib;
125 }
126 
dylib_error(void)127 char *dylib_error(void)
128 {
129 #ifdef _WIN32
130    if (last_dyn_error[0])
131       return last_dyn_error;
132    return NULL;
133 #else
134    return (char*)dlerror();
135 #endif
136 }
137 
dylib_proc(dylib_t lib,const char * proc)138 function_t dylib_proc(dylib_t lib, const char *proc)
139 {
140    function_t sym;
141 
142 #ifdef _WIN32
143    HMODULE mod = (HMODULE)lib;
144 #ifndef __WINRT__
145    if (!mod)
146       mod = GetModuleHandle(NULL);
147 #else
148    /* GetModuleHandle is not available on UWP */
149    if (!mod)
150    {
151       /* It's not possible to lookup symbols in current executable
152        * on UWP. */
153       DebugBreak();
154       return NULL;
155    }
156 #endif
157    sym = (function_t)GetProcAddress(mod, proc);
158    if (!sym)
159    {
160       set_dl_error();
161       return NULL;
162    }
163    last_dyn_error[0] = 0;
164 #else
165    void *ptr_sym = NULL;
166 
167    if (lib)
168       ptr_sym = dlsym(lib, proc);
169    else
170    {
171       void *handle = dlopen(NULL, RTLD_LAZY);
172       if (handle)
173       {
174          ptr_sym = dlsym(handle, proc);
175          dlclose(handle);
176       }
177    }
178 
179    /* Dirty hack to workaround the non-legality of
180     * (void*) -> fn-pointer casts. */
181    memcpy(&sym, &ptr_sym, sizeof(void*));
182 #endif
183 
184    return sym;
185 }
186 
187 /**
188  * dylib_close:
189  * @lib                          : Library handle.
190  *
191  * Frees library handle.
192  **/
dylib_close(dylib_t lib)193 void dylib_close(dylib_t lib)
194 {
195 #ifdef _WIN32
196    if (!FreeLibrary((HMODULE)lib))
197       set_dl_error();
198    last_dyn_error[0] = 0;
199 #else
200 #ifndef NO_DLCLOSE
201    dlclose(lib);
202 #endif
203 #endif
204 }
205 
206 #endif
207