2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
/*
|
|
|
|
All plugins from Core > Plugins are loaded and unloaded with this class when
|
|
|
|
Dolpin is started and stopped.
|
|
|
|
*/
|
2009-01-07 02:59:19 +00:00
|
|
|
|
2009-01-22 11:18:46 +00:00
|
|
|
#include <string.h> // System
|
2009-01-16 02:58:34 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2008-12-08 05:30:24 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-22 11:18:46 +00:00
|
|
|
#include "Common.h" // Local
|
2008-12-25 15:56:36 +00:00
|
|
|
#include "FileUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "StringUtil.h"
|
|
|
|
#include "DynamicLibrary.h"
|
2009-02-28 01:26:56 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
DynamicLibrary::DynamicLibrary()
|
|
|
|
{
|
|
|
|
library = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Gets the last dll error as string
|
|
|
|
const char *DllGetLastError()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-02-20 22:04:52 +00:00
|
|
|
#ifdef _WIN32
|
2009-02-28 01:26:56 +00:00
|
|
|
return GetLastErrorMsg();
|
|
|
|
#else // not win32
|
|
|
|
return dlerror();
|
|
|
|
#endif // WIN32
|
2008-12-17 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
/* Loads the dll with LoadLibrary() or dlopen. This function is called on
|
|
|
|
start to scan for plugin, and before opening the Config and Debugging
|
|
|
|
windows. It is also called from core to load the plugins when the
|
|
|
|
emulation starts.
|
|
|
|
|
|
|
|
Returns 0 on failure and 1 on success
|
|
|
|
|
|
|
|
TODO: think about implementing some sort of cache for the plugin info.
|
|
|
|
*/
|
2008-12-08 05:30:24 +00:00
|
|
|
int DynamicLibrary::Load(const char* filename)
|
|
|
|
{
|
2009-02-28 01:26:56 +00:00
|
|
|
INFO_LOG(COMMON, "DL: Loading dynamic library %s", filename);
|
|
|
|
|
|
|
|
if (!filename || strlen(filename) == 0) {
|
|
|
|
ERROR_LOG(COMMON, "DL: Missing filename to load");
|
2009-02-20 22:04:52 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-28 01:26:56 +00:00
|
|
|
|
|
|
|
if (IsLoaded()) {
|
|
|
|
INFO_LOG(COMMON, "DL: library %s already loaded", filename);
|
|
|
|
return 1;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
library = LoadLibrary(filename);
|
2008-12-17 15:34:27 +00:00
|
|
|
#else
|
2009-02-28 01:26:56 +00:00
|
|
|
// RTLD_NOW: resolve all symbols on load
|
|
|
|
// RTLD_LOCAL: don't resolve symbols for other libraries
|
2008-12-17 15:34:27 +00:00
|
|
|
library = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
#endif
|
2009-02-28 01:26:56 +00:00
|
|
|
|
|
|
|
DEBUG_LOG(COMMON, "DL: LoadLibrary: %s(%p)", filename, library);
|
|
|
|
|
|
|
|
if (!library) {
|
2009-03-18 17:17:58 +00:00
|
|
|
fprintf(stderr, "DL: Error loading DLL %s: %s", filename,
|
|
|
|
DllGetLastError());
|
2009-02-28 01:26:56 +00:00
|
|
|
ERROR_LOG(COMMON, "DL: Error loading DLL %s: %s", filename,
|
|
|
|
DllGetLastError());
|
2009-02-20 22:04:52 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2008-12-17 15:34:27 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
library_file = filename;
|
2009-02-28 01:26:56 +00:00
|
|
|
|
|
|
|
INFO_LOG(COMMON, "DL: Done loading dynamic library %s", filename);
|
2008-12-08 05:30:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
/* Frees one instances of the dynamic library. Note that on most systems use
|
|
|
|
reference count to decide when to actually remove the library from memory.
|
|
|
|
|
|
|
|
Return 0 on failure and 1 on success
|
|
|
|
*/
|
2008-12-17 15:34:27 +00:00
|
|
|
int DynamicLibrary::Unload()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-02-28 01:26:56 +00:00
|
|
|
INFO_LOG(COMMON, "DL: Unloading dynamic library %s", library_file.c_str());
|
|
|
|
int retval;
|
|
|
|
if (!IsLoaded()) { // library != null
|
|
|
|
ERROR_LOG(COMMON, "DL: Unload failed for %s: not loaded",
|
|
|
|
library_file.c_str());
|
|
|
|
PanicAlert("DL: Unload failed %s: not loaded",
|
|
|
|
library_file.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG(COMMON, "DL: FreeLibrary: %s %p\n",
|
|
|
|
library_file.c_str(), library);
|
2008-12-08 05:30:24 +00:00
|
|
|
#ifdef _WIN32
|
2009-02-28 01:26:56 +00:00
|
|
|
retval = FreeLibrary(library);
|
2008-12-08 05:30:24 +00:00
|
|
|
#else
|
2009-02-28 01:26:56 +00:00
|
|
|
retval = dlclose(library)?0:1;
|
2008-12-08 05:30:24 +00:00
|
|
|
#endif
|
2009-01-31 23:47:45 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
if (! retval) {
|
|
|
|
ERROR_LOG(COMMON, "DL: Unload failed %s: %s",
|
|
|
|
library_file.c_str(),
|
|
|
|
DllGetLastError());
|
|
|
|
}
|
|
|
|
library = 0;
|
|
|
|
|
|
|
|
INFO_LOG(COMMON, "DL: Done unloading dynamic library %s",
|
|
|
|
library_file.c_str());
|
|
|
|
return retval;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Returns the address where symbol funcname is loaded or NULL on failure
|
2008-12-08 05:30:24 +00:00
|
|
|
void* DynamicLibrary::Get(const char* funcname) const
|
|
|
|
{
|
|
|
|
void* retval;
|
2009-02-28 01:26:56 +00:00
|
|
|
|
|
|
|
INFO_LOG(COMMON, "DL: Getting symbol %s: %s", library_file.c_str(),
|
|
|
|
funcname);
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
if (!library)
|
|
|
|
{
|
2009-02-28 01:26:56 +00:00
|
|
|
ERROR_LOG(COMMON, "DL: Get failed %s - Library not loaded");
|
2008-12-25 15:56:36 +00:00
|
|
|
return NULL;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-01-19 03:45:28 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
retval = GetProcAddress(library, funcname);
|
|
|
|
#else
|
|
|
|
retval = dlsym(library, funcname);
|
|
|
|
#endif
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-01-07 02:59:19 +00:00
|
|
|
if (!retval)
|
|
|
|
{
|
2009-04-12 10:21:40 +00:00
|
|
|
WARN_LOG(COMMON, "DL: Symbol %s missing in %s (error: %s)\n",
|
2009-02-28 01:26:56 +00:00
|
|
|
funcname, library_file.c_str(),
|
|
|
|
DllGetLastError());
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2008-12-17 15:34:27 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
INFO_LOG(COMMON, "DL: Done getting symbol %s: %s", library_file.c_str(),
|
|
|
|
funcname);
|
2008-12-08 05:30:24 +00:00
|
|
|
return retval;
|
|
|
|
}
|