00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "load_dso.h"
00020
00021 #if defined(PENV_PS2)
00022
00023
00024
00025 void *
00026 load_dso(const Filename &)
00027 {
00028 return (void *) NULL;
00029 }
00030
00031 bool
00032 unload_dso(void *dso_handle) {
00033 return false;
00034 }
00035
00036 string
00037 load_dso_error()
00038 {
00039 ostringstream ps2errmsg;
00040 ps2errmsg << "load_dso_error() unsupported on PS2";
00041
00042 return ps2errmsg.str();
00043 }
00044
00045 #else
00046
00047
00048 #if defined(WIN32)
00049
00050
00051 #define WINDOWS_LEAN_AND_MEAN
00052 #include <windows.h>
00053 #undef WINDOWS_LEAN_AND_MEAN
00054
00055 void *
00056 load_dso(const Filename &filename) {
00057 string os_specific = filename.to_os_specific();
00058 return LoadLibrary(os_specific.c_str());
00059 }
00060
00061 bool
00062 unload_dso(void *dso_handle) {
00063 HMODULE dll_handle = (HMODULE) dso_handle;
00064
00065
00066 return (FreeLibrary(dll_handle)!=0);
00067 }
00068
00069 string
00070 load_dso_error() {
00071 DWORD last_error = GetLastError();
00072 switch (last_error) {
00073 case 2: return "File not found";
00074 case 3: return "Path not found";
00075 case 4: return "Too many open files";
00076 case 5: return "Access denied";
00077 case 14: return "Out of memory";
00078 case 18: return "No more files";
00079 case 126: return "Module not found";
00080 case 998: return "Invalid access to memory location";
00081 }
00082
00083
00084 ostringstream errmsg;
00085 errmsg << "Unknown error " << last_error;
00086 return errmsg.str();
00087 }
00088
00089
00090
00091 #elif defined(PENV_OSX)
00092
00093
00094 #include <mach-o/dyld.h>
00095
00096 void *
00097 load_dso(const Filename &filename) {
00098 enum DYLD_BOOL result;
00099 cerr << "_dyld_present() = " << _dyld_present() << endl;
00100 cerr << "_dyld_image_count() = " << _dyld_image_count() << endl;
00101 string os_specific = filename.to_os_specific();
00102 result = NSAddLibrary(os_specific().c_str());
00103 if (result == FALSE) {
00104 cerr << "Failed to load '" << filename << "'" << endl;
00105 }
00106
00107
00108 cerr << "_dyld_present() = " << _dyld_present() << endl;
00109 cerr << "_dyld_image_count() = " << _dyld_image_count() << endl;
00110 for (unsigned long i=0; i<_dyld_image_count(); ++i)
00111 cerr << "_dyld_get_image_name(" << i << ") = '" << _dyld_get_image_name(i)
00112 << "'" << endl;
00113 string stmp = filename;
00114 cerr << "filename = '" << filename << "'" << endl;
00115 int i = stmp.rfind(".dylib");
00116 stmp.erase(i, i+6);
00117 stmp += "_so";
00118 cerr << "filename with tail patched = '" << stmp << "'" << endl;
00119 i = stmp.rfind("lib");
00120 if (i != 0) {
00121 if (stmp[i-1] != '/')
00122 i = stmp.rfind("lib", i-1);
00123 stmp.erase(0, i);
00124 }
00125 cerr << "final patched filename = '" << stmp << "'" << endl;
00126 stmp = "___" + stmp + "_find_me__";
00127 cerr << "symbol name searching for = '" << stmp << "'" << endl;
00128 unsigned long foo1;
00129 void *foo2;
00130 _dyld_lookup_and_bind(stmp.c_str(), &foo1, &foo2);
00131 char *foo3 = (char*)foo1;
00132 cerr << "symbol value = '" << foo3 << "'" << endl;
00133 return (void*)0L;
00134 }
00135
00136 string
00137 load_dso_error() {
00138 return "No DSO loading yet!";
00139 }
00140
00141 #else
00142
00143
00144 #include <dlfcn.h>
00145
00146 void *
00147 load_dso(const Filename &filename) {
00148 string os_specific = filename.to_os_specific();
00149 return dlopen(os_specific.c_str(), RTLD_NOW);
00150 }
00151
00152 bool
00153 unload_dso(void *dso_handle) {
00154 return dlclose(dso_handle)==0;
00155 }
00156
00157 string
00158 load_dso_error() {
00159 return dlerror();
00160 }
00161
00162 #endif
00163
00164 #endif // PS2