 -  return DoSearch(symbolName);
 -}
 -}  // namespace llvm
-Index: lib/Support/CMakeLists.txt
-===================================================================
---- lib/Support/CMakeLists.txt	(revision 167609)
-+++ lib/Support/CMakeLists.txt	(working copy)
-@@ -80,6 +80,7 @@ add_llvm_library(LLVMSupport
-   Threading.cpp
-   TimeValue.cpp
-   Valgrind.cpp
-+  Unix/DynamicLibrary.inc
-   Unix/Host.inc
-   Unix/Memory.inc
-   Unix/Mutex.inc
-Index: lib/Support/Windows/DynamicLibrary.inc
-===================================================================
---- lib/Support/Windows/DynamicLibrary.inc	(revision 167609)
-+++ lib/Support/Windows/DynamicLibrary.inc	(working copy)
-@@ -31,16 +31,8 @@
-  #pragma comment(lib, "dbghelp.lib")
- #endif
- 
--namespace llvm {
--using namespace sys;
-+using namespace llvm;
- 
--//===----------------------------------------------------------------------===//
--//=== WARNING: Implementation here must contain only Win32 specific code
--//===          and must not be UNIX code.
--//===----------------------------------------------------------------------===//
--
--static DenseSet<HMODULE> *OpenedHandles;
--
- extern "C" {
- 
-   static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
-@@ -63,49 +55,61 @@ extern "C" {
- #endif
-         stricmp(ModuleName, "msvcrt20") != 0 &&
-         stricmp(ModuleName, "msvcrt40") != 0) {
--      OpenedHandles->insert((HMODULE)ModuleBase);
-+       OpenedHandles->insert((HMODULE)ModuleBase);
-     }
-     return TRUE;
-   }
--}
-+} // extern "C"
- 
--DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
--                                                   std::string *errMsg) {
--  SmartScopedLock<true> lock(getMutex());
--
--  if (!filename) {
-+void *OpenLibraryImpl(const char *Filename, std::string *ErrMsg) {
-+  if (!Filename) {
-     // When no file is specified, enumerate all DLLs and EXEs in the process.
-     if (OpenedHandles == 0)
--      OpenedHandles = new DenseSet<HMODULE>();
-+      OpenedHandles = new DenseSet<void *>();
- 
-     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
-     // Dummy library that represents "search all handles".
-     // This is mostly to ensure that the return value still shows up as "valid".
--    return DynamicLibrary(&OpenedHandles);
-+    return &OpenedHandles;
-   }
-   
--  HMODULE a_handle = LoadLibrary(filename);
-+  HMODULE a_handle = LoadLibrary(Filename);
- 
-   if (a_handle == 0) {
--    MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
--    return DynamicLibrary();
-+    MakeErrMsg(ErrMsg, std::string(Filename) + ": Can't open : ");
-+    return NULL;
-   }
- 
--  if (OpenedHandles == 0)
--    OpenedHandles = new DenseSet<HMODULE>();
-+  return (void *)a_handle;
-+}
- 
--  // If we've already loaded this library, FreeLibrary() the handle in order to
--  // keep the internal refcount at +1.
--  if (!OpenedHandles->insert(a_handle).second)
--    FreeLibrary(a_handle);
-+bool CloseLibraryImpl(void *DlHandle, std::string *ErrMsg) {
-+  if (!FreeLibrary((HMODULE)DlHandle)) {
-+    MakeErrMsg(ErrMsg, "Cannot free DLL: ");
-+    return false;
-+  }
-+  return true;
-+}
- 
--  return DynamicLibrary(a_handle);
-+void *GetAddressOfSymbolImpl(void *DlHandle, const char *SymbolName,
-+                             bool Global) {
-+  if (DlHandle == &OpenedHandles) {
-+    // Dummy library that represents "search all handles".
-+    if (Global) {
-+      // All libraries are already being searched, do not recurse.
-+      return 0;
-+    } else {
-+      // Perform a global symbol search through all libraries.
-+      DynamicLibrary::SearchForLibrarySymbol(SymbolName);
-+    }
-+  }
-+  return (void *)(intptr_t) GetProcAddress((HMODULE)DlHandle, SymbolName);
- }
- 
- // Stack probing routines are in the support library (e.g. libgcc), but we don't
- // have dynamic linking on windows. Provide a hook.
- #define EXPLICIT_SYMBOL(SYM)                    \
--  extern "C" { extern void *SYM; }
-+  extern "C" void *SYM;
- #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
- 
- #include "explicit_symbols.inc"
-@@ -113,51 +117,13 @@ extern "C" {
- #undef EXPLICIT_SYMBOL
- #undef EXPLICIT_SYMBOL2
- 
--void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
--  SmartScopedLock<true> Lock(getMutex());
-+void PopulateSpecialSymbolsImpl(StringMap<void *>& SymMap) {
-+#define EXPLICIT_SYMBOL(SYM)                    \
-+  SymMap[#SYM] = &SYM;
-+#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
- 
--  // First check symbols added via AddSymbol().
--  if (ExplicitSymbols) {
--    StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
-+#include "explicit_symbols.inc"
- 
--    if (i != ExplicitSymbols->end())
--      return i->second;
--  }
--
--  // Now search the libraries.
--  if (OpenedHandles) {
--    for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
--         E = OpenedHandles->end(); I != E; ++I) {
--      FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
--      if (ptr) {
--        return (void *)(intptr_t)ptr;
--      }
--    }
--  }
--
--  #define EXPLICIT_SYMBOL(SYM)                    \
--    if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
--  #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
--    if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
--
--  {
--    #include "explicit_symbols.inc"
--  }
--
--  #undef EXPLICIT_SYMBOL
--  #undef EXPLICIT_SYMBOL2
--
--  return 0;
-+#undef EXPLICIT_SYMBOL
-+#undef EXPLICIT_SYMBOL2
- }
--
--
--void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
--  if (!isValid())
--    return NULL;
--  if (Data == &OpenedHandles)
--    return SearchForAddressOfSymbol(symbolName);
--  return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
--}
--
--
--}

     /// This functions permanently adds the symbol \p symbolName with the
     /// value \p symbolValue.  These symbols are searched before any
     /// libraries.
Index: lib/Support/DynamicLibrary.cpp
===================================================================
--- lib/Support/DynamicLibrary.cpp	(revision 167609)

+}
+ 
 #endif // LLVM_ON_WIN32
Index: lib/Support/Windows/DynamicLibrary.inc
===================================================================
--- lib/Support/Windows/DynamicLibrary.inc	(revision 167609)
+++ lib/Support/Windows/DynamicLibrary.inc	(working copy)
@@ -31,16 +31,8 @@
  #pragma comment(lib, "dbghelp.lib")
 #endif
 
-namespace llvm {
-using namespace sys;
+using namespace llvm;
 
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only Win32 specific code
-//===          and must not be UNIX code.
-//===----------------------------------------------------------------------===//
-
-static DenseSet<HMODULE> *OpenedHandles;
-
 extern "C" {
 
   static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
@@ -63,49 +55,61 @@ extern "C" {
 #endif
         stricmp(ModuleName, "msvcrt20") != 0 &&
         stricmp(ModuleName, "msvcrt40") != 0) {
-      OpenedHandles->insert((HMODULE)ModuleBase);
+       OpenedHandles->insert((HMODULE)ModuleBase);
     }
     return TRUE;
   }
-}
+} // extern "C"
 
-DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
-                                                   std::string *errMsg) {
-  SmartScopedLock<true> lock(getMutex());
-
-  if (!filename) {
+void *OpenLibraryImpl(const char *Filename, std::string *ErrMsg) {
+  if (!Filename) {
     // When no file is specified, enumerate all DLLs and EXEs in the process.
     if (OpenedHandles == 0)
-      OpenedHandles = new DenseSet<HMODULE>();
+      OpenedHandles = new DenseSet<void *>();
 
     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
     // Dummy library that represents "search all handles".
     // This is mostly to ensure that the return value still shows up as "valid".
-    return DynamicLibrary(&OpenedHandles);
+    return &OpenedHandles;
   }
   
-  HMODULE a_handle = LoadLibrary(filename);
+  HMODULE a_handle = LoadLibrary(Filename);
 
   if (a_handle == 0) {
-    MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
-    return DynamicLibrary();
+    MakeErrMsg(ErrMsg, std::string(Filename) + ": Can't open : ");
+    return NULL;
   }
 
-  if (OpenedHandles == 0)
-    OpenedHandles = new DenseSet<HMODULE>();
+  return (void *)a_handle;
+}
 
-  // If we've already loaded this library, FreeLibrary() the handle in order to
-  // keep the internal refcount at +1.
-  if (!OpenedHandles->insert(a_handle).second)
-    FreeLibrary(a_handle);
+bool CloseLibraryImpl(void *DlHandle, std::string *ErrMsg) {
+  if (!FreeLibrary((HMODULE)DlHandle)) {
+    MakeErrMsg(ErrMsg, "Cannot free DLL: ");
+    return false;
+  }
+  return true;
+}
 
-  return DynamicLibrary(a_handle);
+void *GetAddressOfSymbolImpl(void *DlHandle, const char *SymbolName,
+                             bool Global) {
+  if (DlHandle == &OpenedHandles) {
+    // Dummy library that represents "search all handles".
+    if (Global) {
+      // All libraries are already being searched, do not recurse.
+      return 0;
+    } else {
+      // Perform a global symbol search through all libraries.
+      DynamicLibrary::SearchForLibrarySymbol(SymbolName);
+    }
+  }
+  return (void *)(intptr_t) GetProcAddress((HMODULE)DlHandle, SymbolName);
 }
 
 // Stack probing routines are in the support library (e.g. libgcc), but we don't
 // have dynamic linking on windows. Provide a hook.
 #define EXPLICIT_SYMBOL(SYM)                    \
-  extern "C" { extern void *SYM; }
+  extern "C" void *SYM;
 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
 
 #include "explicit_symbols.inc"
@@ -113,51 +117,13 @@ extern "C" {
 #undef EXPLICIT_SYMBOL
 #undef EXPLICIT_SYMBOL2
 
-void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
-  SmartScopedLock<true> Lock(getMutex());
+void PopulateSpecialSymbolsImpl(StringMap<void *>& SymMap) {
+#define EXPLICIT_SYMBOL(SYM)                    \
+  SymMap[#SYM] = &SYM;
+#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
 
-  // First check symbols added via AddSymbol().
-  if (ExplicitSymbols) {
-    StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
+#include "explicit_symbols.inc"
 
-    if (i != ExplicitSymbols->end())
-      return i->second;
-  }
-
-  // Now search the libraries.
-  if (OpenedHandles) {
-    for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
-         E = OpenedHandles->end(); I != E; ++I) {
-      FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
-      if (ptr) {
-        return (void *)(intptr_t)ptr;
-      }
-    }
-  }
-
-  #define EXPLICIT_SYMBOL(SYM)                    \
-    if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
-  #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
-    if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
-
-  {
-    #include "explicit_symbols.inc"
-  }
-
-  #undef EXPLICIT_SYMBOL
-  #undef EXPLICIT_SYMBOL2
-
-  return 0;
+#undef EXPLICIT_SYMBOL
+#undef EXPLICIT_SYMBOL2
 }
-
-
-void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
-  if (!isValid())
-    return NULL;
-  if (Data == &OpenedHandles)
-    return SearchForAddressOfSymbol(symbolName);
-  return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
-}
-
-
-}
Index: lib/Support/CMakeLists.txt
===================================================================
--- lib/Support/CMakeLists.txt	(revision 167609)
+++ lib/Support/CMakeLists.txt	(working copy)
@@ -80,6 +80,7 @@ add_llvm_library(LLVMSupport
   Threading.cpp
   TimeValue.cpp
   Valgrind.cpp
+  Unix/DynamicLibrary.inc
   Unix/Host.inc
   Unix/Memory.inc
   Unix/Mutex.inc
Index: lib/Support/SearchForAddressOfSpecialSymbol.cpp
===================================================================
--- lib/Support/SearchForAddressOfSpecialSymbol.cpp	(revision 167609)
