LLVM API Documentation

DynamicLibrary.inc

Go to the documentation of this file.
00001 //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides the Win32 specific implementation of DynamicLibrary.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Win32.h"
00015 
00016 #ifdef __MINGW32__
00017  #include <imagehlp.h>
00018 #else
00019  #include <dbghelp.h>
00020 #endif
00021 
00022 #ifdef _MSC_VER
00023  #include <ntverp.h>
00024 #endif
00025 
00026 #ifdef __MINGW32__
00027  #if (HAVE_LIBIMAGEHLP != 1)
00028   #error "libimagehlp.a should be present"
00029  #endif
00030 #else
00031  #pragma comment(lib, "dbghelp.lib")
00032 #endif
00033 
00034 namespace llvm {
00035 using namespace sys;
00036 
00037 //===----------------------------------------------------------------------===//
00038 //=== WARNING: Implementation here must contain only Win32 specific code 
00039 //===          and must not be UNIX code.
00040 //===----------------------------------------------------------------------===//
00041 
00042 static std::vector<HMODULE> OpenedHandles;
00043 
00044 #ifdef _WIN64
00045   typedef DWORD64 ModuleBaseType;
00046 #else
00047   typedef ULONG ModuleBaseType;
00048 #endif
00049 
00050 extern "C" {
00051 // Use old callback if:
00052 //  - Not using Visual Studio
00053 //  - Visual Studio 2005 or earlier but only if we are not using the Windows SDK 
00054 //    or Windows SDK version is older than 6.0
00055 // Use new callback if:
00056 //  - Newer Visual Studio (comes with newer SDK).
00057 //  - Visual Studio 2005 with Windows SDK 6.0+
00058 #if !defined(_MSC_VER) || _MSC_VER < 1500 && (!defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 6000)
00059   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
00060                                     ModuleBaseType ModuleBase,
00061                                     ULONG ModuleSize,
00062                                     PVOID UserContext)
00063 #else
00064   static BOOL CALLBACK ELM_Callback(PCSTR  ModuleName,
00065                                     ModuleBaseType ModuleBase,
00066                                     ULONG ModuleSize,
00067                                     PVOID UserContext)
00068 #endif
00069   {
00070     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
00071     // into the process.
00072     if (stricmp(ModuleName, "msvci70") != 0 &&
00073         stricmp(ModuleName, "msvcirt") != 0 &&
00074         stricmp(ModuleName, "msvcp50") != 0 &&
00075         stricmp(ModuleName, "msvcp60") != 0 &&
00076         stricmp(ModuleName, "msvcp70") != 0 &&
00077         stricmp(ModuleName, "msvcr70") != 0 &&
00078 #ifndef __MINGW32__
00079         // Mingw32 uses msvcrt.dll by default. Don't ignore it.
00080         // Otherwise, user should be aware, what he's doing :)
00081         stricmp(ModuleName, "msvcrt") != 0 &&
00082 #endif        
00083         stricmp(ModuleName, "msvcrt20") != 0 &&
00084         stricmp(ModuleName, "msvcrt40") != 0) {
00085       OpenedHandles.push_back((HMODULE)ModuleBase);
00086     }
00087     return TRUE;
00088   }
00089 }
00090 
00091 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
00092                                             std::string *ErrMsg) {                                            
00093   if (filename) {
00094     HMODULE a_handle = LoadLibrary(filename);
00095 
00096     if (a_handle == 0)
00097       return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
00098 
00099     OpenedHandles.push_back(a_handle);
00100   } else {
00101     // When no file is specified, enumerate all DLLs and EXEs in the
00102     // process.
00103     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
00104   }
00105 
00106   // Because we don't remember the handle, we will never free it; hence,
00107   // it is loaded permanently.
00108   return false;
00109 }
00110 
00111 // Stack probing routines are in the support library (e.g. libgcc), but we don't
00112 // have dynamic linking on windows. Provide a hook.
00113 #if defined(__MINGW32__) || defined (_MSC_VER)
00114   #define EXPLICIT_SYMBOL(SYM)                    \
00115     if (!strcmp(symbolName, #SYM)) return (void*)&SYM
00116   #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
00117     if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO
00118   #define EXPLICIT_SYMBOL_DEF(SYM)                \
00119     extern "C" { extern void *SYM; }
00120 
00121   #if defined(__MINGW32__)
00122     EXPLICIT_SYMBOL_DEF(_alloca);
00123     EXPLICIT_SYMBOL_DEF(__main);
00124     EXPLICIT_SYMBOL_DEF(__ashldi3);
00125     EXPLICIT_SYMBOL_DEF(__ashrdi3);
00126     EXPLICIT_SYMBOL_DEF(__cmpdi2);
00127     EXPLICIT_SYMBOL_DEF(__divdi3);
00128     EXPLICIT_SYMBOL_DEF(__fixdfdi);
00129     EXPLICIT_SYMBOL_DEF(__fixsfdi);
00130     EXPLICIT_SYMBOL_DEF(__fixunsdfdi);
00131     EXPLICIT_SYMBOL_DEF(__fixunssfdi);
00132     EXPLICIT_SYMBOL_DEF(__floatdidf);
00133     EXPLICIT_SYMBOL_DEF(__floatdisf);
00134     EXPLICIT_SYMBOL_DEF(__lshrdi3);
00135     EXPLICIT_SYMBOL_DEF(__moddi3);
00136     EXPLICIT_SYMBOL_DEF(__udivdi3);
00137     EXPLICIT_SYMBOL_DEF(__umoddi3);
00138   #elif defined(_MSC_VER)
00139     EXPLICIT_SYMBOL_DEF(_alloca_probe);
00140   #endif
00141 #endif
00142 
00143 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
00144   // First check symbols added via AddSymbol().
00145   if (ExplicitSymbols) {
00146     std::map<std::string, void *>::iterator I = 
00147       ExplicitSymbols->find(symbolName);
00148     std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
00149     if (I != E)
00150       return I->second;
00151   }
00152 
00153   // Now search the libraries.
00154   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
00155        E = OpenedHandles.end(); I != E; ++I) {
00156     FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
00157     if (ptr) {
00158       return (void *) ptr;
00159     }
00160   }
00161 
00162 #if defined(__MINGW32__)
00163   {
00164     EXPLICIT_SYMBOL(_alloca);
00165     EXPLICIT_SYMBOL(__main);
00166     EXPLICIT_SYMBOL(__ashldi3);
00167     EXPLICIT_SYMBOL(__ashrdi3);
00168     EXPLICIT_SYMBOL(__cmpdi2);
00169     EXPLICIT_SYMBOL(__divdi3);
00170     EXPLICIT_SYMBOL(__fixdfdi);
00171     EXPLICIT_SYMBOL(__fixsfdi);
00172     EXPLICIT_SYMBOL(__fixunsdfdi);
00173     EXPLICIT_SYMBOL(__fixunssfdi);
00174     EXPLICIT_SYMBOL(__floatdidf);
00175     EXPLICIT_SYMBOL(__floatdisf);
00176     EXPLICIT_SYMBOL(__lshrdi3);
00177     EXPLICIT_SYMBOL(__moddi3);
00178     EXPLICIT_SYMBOL(__udivdi3);
00179     EXPLICIT_SYMBOL(__umoddi3);
00180 
00181     EXPLICIT_SYMBOL2(alloca, _alloca);
00182 #undef EXPLICIT_SYMBOL
00183 #undef EXPLICIT_SYMBOL2
00184 #undef EXPLICIT_SYMBOL_DEF    
00185   }
00186 #elif defined(_MSC_VER)
00187   {
00188     EXPLICIT_SYMBOL2(alloca, _alloca_probe);
00189     EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
00190 #undef EXPLICIT_SYMBOL
00191 #undef EXPLICIT_SYMBOL2
00192 #undef EXPLICIT_SYMBOL_DEF    
00193   }  
00194 #endif
00195 
00196   return 0;
00197 }
00198 
00199 }
00200