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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (dylib.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <dynamic/dylib.h>
#include <encodings/utf.h>
#include <string/stdstring.h>
#include <retro_miscellaneous.h>
#include <file/file_path.h>
#if defined(ORBIS)
#include <orbis/libkernel.h>
#endif
#ifdef NEED_DYNAMIC
#ifdef _WIN32
#include <compat/posix_string.h>
#include <windows.h>
#else
#if !defined(ORBIS)
#include <dlfcn.h>
#endif
#endif
/* Assume W-functions do not work below Win2K and Xbox platforms */
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif
#ifdef _WIN32
static char last_dyn_error[512];
static void set_dl_error(void)
{
DWORD err = GetLastError();
if (FormatMessage(
FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
last_dyn_error, sizeof(last_dyn_error) - 1,
NULL) == 0)
snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
"unknown error %lu", err);
}
#endif
/**
* dylib_load:
* @path : Path to libretro core library.
*
* Platform independent dylib loading.
*
* @return Library handle on success, otherwise NULL.
**/
dylib_t dylib_load(const char *path)
{
#ifdef _WIN32
#ifndef __WINRT__
int prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
#endif
#ifdef __WINRT__
dylib_t lib;
/* On UWP, you can only load DLLs inside your install directory, using a special function that takes a relative path */
char relative_path_abbrev[PATH_MAX_LENGTH];
char *relative_path = relative_path_abbrev;
wchar_t *path_wide = NULL;
relative_path_abbrev[0] = '\0';
if (!path_is_absolute(path))
RARCH_WARN("Relative path in dylib_load! This is likely an attempt to load a system library that will fail\n");
fill_pathname_abbreviate_special(relative_path_abbrev, path, sizeof(relative_path_abbrev));
/* Path to dylib_load is not inside app install directory.
* Loading will probably fail. */
if (relative_path[0] != ':' || !PATH_CHAR_IS_SLASH(relative_path[1])) { }
else
relative_path += 2;
path_wide = utf8_to_utf16_string_alloc(relative_path);
lib = LoadPackagedLibrary(path_wide, 0);
free(path_wide);
#elif defined(LEGACY_WIN32)
dylib_t lib = LoadLibrary(path);
#else
wchar_t *path_wide = utf8_to_utf16_string_alloc(path);
dylib_t lib = LoadLibraryW(path_wide);
free(path_wide);
#endif
#ifndef __WINRT__
SetErrorMode(prevmode);
#endif
if (!lib)
{
set_dl_error();
return NULL;
}
last_dyn_error[0] = 0;
#elif defined(ORBIS)
int res;
dylib_t lib = (dylib_t)sceKernelLoadStartModule(path, 0, NULL, 0, NULL, &res);
#elif defined(IOS) || defined(OSX)
dylib_t lib;
static const char fw_suffix[] = ".framework";
if (string_ends_with(path, fw_suffix))
{
char fw_path[PATH_MAX_LENGTH];
const char *fw_name = path_basename(path);
size_t sz = strlcpy(fw_path, path, sizeof(fw_path));
sz += strlcpy(fw_path + sz, "/", sizeof(fw_path) - sz);
/* Assume every framework binary is named for the framework. Not always
* a great assumption but correct enough for our uses. */
strlcpy(fw_path + sz, fw_name, strlen(fw_name) - STRLEN_CONST(fw_suffix) + 1);
lib = dlopen(fw_path, RTLD_LAZY | RTLD_LOCAL);
}
else
lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
#else
dylib_t lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
#endif
return lib;
}
char *dylib_error(void)
{
#ifdef _WIN32
if (last_dyn_error[0])
return last_dyn_error;
return NULL;
#else
return (char*)dlerror();
#endif
}
function_t dylib_proc(dylib_t lib, const char *proc)
{
function_t sym;
#ifdef _WIN32
HMODULE mod = (HMODULE)lib;
if (!mod)
{
#ifdef __WINRT__
/* GetModuleHandle is not available on UWP */
/* It's not possible to lookup symbols in current executable
* on UWP. */
DebugBreak();
return NULL;
#else
mod = GetModuleHandle(NULL);
#endif
}
if (!(sym = (function_t)GetProcAddress(mod, proc)))
{
set_dl_error();
return NULL;
}
last_dyn_error[0] = 0;
#elif defined(ORBIS)
void *ptr_sym = NULL;
sym = NULL;
if (lib)
{
sceKernelDlsym((SceKernelModule)lib, proc, &ptr_sym);
memcpy(&sym, &ptr_sym, sizeof(void*));
}
#else
void *ptr_sym = NULL;
if (lib)
ptr_sym = dlsym(lib, proc);
else
{
void *handle = dlopen(NULL, RTLD_LAZY);
if (handle)
{
ptr_sym = dlsym(handle, proc);
dlclose(handle);
}
}
/* Dirty hack to workaround the non-legality of
* (void*) -> fn-pointer casts. */
memcpy(&sym, &ptr_sym, sizeof(void*));
#endif
return sym;
}
/**
* dylib_close:
* @lib : Library handle.
*
* Frees library handle.
**/
void dylib_close(dylib_t lib)
{
#ifdef _WIN32
if (!FreeLibrary((HMODULE)lib))
set_dl_error();
last_dyn_error[0] = 0;
#elif defined(ORBIS)
int res;
sceKernelStopUnloadModule((SceKernelModule)lib, 0, NULL, 0, NULL, &res);
#else
#ifndef NO_DLCLOSE
dlclose(lib);
#endif
#endif
}
#endif
|