2009-03-28 02:57:34 -06:00
|
|
|
// Copyright (C) 2003-2009 Dolphin Project.
|
2008-12-07 22:30:24 -07: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-27 18:26:56 -07:00
|
|
|
/*
|
|
|
|
All plugins from Core > Plugins are loaded and unloaded with this class when
|
|
|
|
Dolpin is started and stopped.
|
|
|
|
*/
|
2009-01-06 19:59:19 -07:00
|
|
|
|
2009-01-22 04:18:46 -07:00
|
|
|
#include <string.h> // System
|
2009-01-15 19:58:34 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2008-12-07 22:30:24 -07:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-22 04:18:46 -07:00
|
|
|
#include "Common.h" // Local
|
2008-12-25 08:56:36 -07:00
|
|
|
#include "FileUtil.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
#include "StringUtil.h"
|
|
|
|
#include "DynamicLibrary.h"
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
DynamicLibrary::DynamicLibrary()
|
|
|
|
{
|
|
|
|
library = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Gets the last dll error as string
|
|
|
|
const char *DllGetLastError()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2009-02-20 15:04:52 -07:00
|
|
|
#ifdef _WIN32
|
2009-02-27 18:26:56 -07:00
|
|
|
return GetLastErrorMsg();
|
|
|
|
#else // not win32
|
|
|
|
return dlerror();
|
|
|
|
#endif // WIN32
|
2008-12-17 09:24:03 -07:00
|
|
|
}
|
|
|
|
|
2009-02-27 18:26:56 -07: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-07 22:30:24 -07:00
|
|
|
int DynamicLibrary::Load(const char* filename)
|
|
|
|
{
|
2009-02-27 18:26:56 -07: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 15:04:52 -07:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
if (IsLoaded()) {
|
|
|
|
INFO_LOG(COMMON, "DL: library %s already loaded", filename);
|
|
|
|
return 1;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
library = LoadLibrary(filename);
|
2008-12-17 08:34:27 -07:00
|
|
|
#else
|
2009-02-27 18:26:56 -07:00
|
|
|
// RTLD_NOW: resolve all symbols on load
|
|
|
|
// RTLD_LOCAL: don't resolve symbols for other libraries
|
2008-12-17 08:34:27 -07:00
|
|
|
library = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
#endif
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
DEBUG_LOG(COMMON, "DL: LoadLibrary: %s(%p)", filename, library);
|
|
|
|
|
|
|
|
if (!library) {
|
2009-03-18 11:17:58 -06:00
|
|
|
fprintf(stderr, "DL: Error loading DLL %s: %s", filename,
|
|
|
|
DllGetLastError());
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON, "DL: Error loading DLL %s: %s", filename,
|
|
|
|
DllGetLastError());
|
2009-02-20 15:04:52 -07:00
|
|
|
return 0;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2008-12-17 08:34:27 -07:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
library_file = filename;
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
INFO_LOG(COMMON, "DL: Done loading dynamic library %s", filename);
|
2008-12-07 22:30:24 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-27 18:26:56 -07: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 08:34:27 -07:00
|
|
|
int DynamicLibrary::Unload()
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2009-02-27 18:26:56 -07: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-07 22:30:24 -07:00
|
|
|
#ifdef _WIN32
|
2009-02-27 18:26:56 -07:00
|
|
|
retval = FreeLibrary(library);
|
2008-12-07 22:30:24 -07:00
|
|
|
#else
|
2009-02-27 18:26:56 -07:00
|
|
|
retval = dlclose(library)?0:1;
|
2008-12-07 22:30:24 -07:00
|
|
|
#endif
|
2009-01-31 16:47:45 -07:00
|
|
|
|
2009-02-27 18:26:56 -07: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-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Returns the address where symbol funcname is loaded or NULL on failure
|
2008-12-07 22:30:24 -07:00
|
|
|
void* DynamicLibrary::Get(const char* funcname) const
|
|
|
|
{
|
|
|
|
void* retval;
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
INFO_LOG(COMMON, "DL: Getting symbol %s: %s", library_file.c_str(),
|
|
|
|
funcname);
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
if (!library)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON, "DL: Get failed %s - Library not loaded");
|
2008-12-25 08:56:36 -07:00
|
|
|
return NULL;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2009-01-18 20:45:28 -07:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
retval = GetProcAddress(library, funcname);
|
|
|
|
#else
|
|
|
|
retval = dlsym(library, funcname);
|
|
|
|
#endif
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2009-01-06 19:59:19 -07:00
|
|
|
if (!retval)
|
|
|
|
{
|
2009-04-12 04:21:40 -06:00
|
|
|
WARN_LOG(COMMON, "DL: Symbol %s missing in %s (error: %s)\n",
|
2009-02-27 18:26:56 -07:00
|
|
|
funcname, library_file.c_str(),
|
|
|
|
DllGetLastError());
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2008-12-17 08:34:27 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
INFO_LOG(COMMON, "DL: Done getting symbol %s: %s", library_file.c_str(),
|
|
|
|
funcname);
|
2008-12-07 22:30:24 -07:00
|
|
|
return retval;
|
|
|
|
}
|