-Index: lib/Support/Windows/DynamicLibrary.inc
-===================================================================
---- lib/Support/Windows/DynamicLibrary.inc	(revision 167609)
-+++ lib/Support/Windows/DynamicLibrary.inc	(working copy)
-@@ -1,4 +1,4 @@
--//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
-+//===- Windows/DynamicLibrary.cpp - Win32 DL Implementation -----*- C++ -*-===//
- //
- //                     The LLVM Compiler Infrastructure
- //
-@@ -32,15 +32,9 @@
- #endif
- 
- namespace llvm {
-+
- using namespace sys;
- 
--//===----------------------------------------------------------------------===//
--//=== 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,
-@@ -48,6 +42,8 @@ extern "C" {
-                                     ULONG ModuleSize,
-                                     PVOID UserContext)
-   {
-+    static DenseSet<HMODULE> ProcessDynLibs;
-+
-     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
-     // into the process.
-     if (stricmp(ModuleName, "msvci70") != 0 &&
-@@ -63,101 +59,63 @@ extern "C" {
- #endif
-         stricmp(ModuleName, "msvcrt20") != 0 &&
-         stricmp(ModuleName, "msvcrt40") != 0) {
--      OpenedHandles->insert((HMODULE)ModuleBase);
-+      if (ProcessDynLibs.insert((HMODULE)ModuleBase)->second)
-+        ++(*OpenedHandles)[(HMODULE)ModuleBase];
-     }
-     return TRUE;
-   }
- }
- 
--DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
--                                                   std::string *errMsg) {
--  SmartScopedLock<true> lock(getMutex());
--
-+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 DenseMap<void *, unsigned>();
- 
-     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);
- 
-   if (a_handle == 0) {
-     MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
--    return DynamicLibrary();
-+    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);
-+void CloseLibraryImpl(void *dlHandle) {
-+  FreeLibrary((HMODULE)dlHandle);
-+}
- 
--  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.
-+      return 0;
-+    } else {
-+      // Perform a global symbol search.
-+      DynamicLibrary::SearchForAddressOfSymbol(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.
-+void PopulateSpecialSymbolsImpl(StringMap<void *>& symMap) {
-+
- #define EXPLICIT_SYMBOL(SYM)                    \
--  extern "C" { extern void *SYM; }
--#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
-+  extern "C" void *SYM; symMap[#SYM] = &SYM;
-+#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
-+  symMap[#SYMFROM] = &SYMTO;
- 
- #include "explicit_symbols.inc"
- 
- #undef EXPLICIT_SYMBOL
- #undef EXPLICIT_SYMBOL2
--
--void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
--  SmartScopedLock<true> Lock(getMutex());
--
--  // First check symbols added via AddSymbol().
--  if (ExplicitSymbols) {
--    StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
--
--    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;
- }
--
--
--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/Unix/DynamicLibrary.inc
-===================================================================
---- lib/Support/Unix/DynamicLibrary.inc	(revision 0)
-+++ lib/Support/Unix/DynamicLibrary.inc	(revision 0)
-@@ -0,0 +1,105 @@
-+//===--- Unix/DynamicLibrary.cpp - Unix DL Implementation -------*- C++ -*-===//
-+//
-+//                     The LLVM Compiler Infrastructure
-+//
-+// This file is distributed under the University of Illinois Open Source
-+// License. See LICENSE.TXT for details.
-+//
-+//===----------------------------------------------------------------------===//
-+//
-+// This file provides the dlsym specific implementation of DynamicLibrary.
-+//
-+//===----------------------------------------------------------------------===//
-+
-+#include <dlfcn.h>
-+
-+void *OpenLibraryImpl(const char *filename, std::string *errMsg) {
-+  void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
-+  if (handle == 0) {
-+    if (errMsg) *errMsg = dlerror();
-+    return NULL;
-+  }
-+
-+#ifdef __CYGWIN__
-+  // Cygwin searches symbols only in the main
-+  // with the handle of dlopen(NULL, RTLD_GLOBAL).
-+  if (filename == NULL)
-+    handle = RTLD_DEFAULT;
-+#endif
-+  return handle;
-+}
-+
-+void CloseLibraryImpl(void *dlHandle) {
-+   dlclose(dlHandle);
-+}
-+
-+void *GetAddressOfSymbolImpl(void *dlHandle, const char *symbolName,
-+                             bool /*Global*/) {
-+  return dlsym(dlHandle, symbolName);
-+}
-+
-+#include <string.h>
-+
-+// Must declare the symbols in the global namespace.
-+void PopulateSpecialSymbolsImpl(StringMap<void *>& symMap) {
-+// This macro returns the address of a well-known, explicit symbol
-+#define EXPLICIT_SYMBOL(SYM) \
-+    Map[#SYM] = &SYM
-+
-+// On linux we have a weird situation. The stderr/out/in symbols are both
-+// macros and global variables because of standards requirements. So, we
-+// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
-+#if defined(__linux__) and !defined(__ANDROID__)
-+  {
-+    EXPLICIT_SYMBOL(stderr);
-+    EXPLICIT_SYMBOL(stdout);
-+    EXPLICIT_SYMBOL(stdin);
-+  }
-+#else
-+  // For everything else, we want to check to make sure the symbol isn't defined
-+  // as a macro before using EXPLICIT_SYMBOL.
-+  {
-+#ifndef stdin
-+    EXPLICIT_SYMBOL(stdin);
-+#endif
-+#ifndef stdout
-+    EXPLICIT_SYMBOL(stdout);
-+#endif
-+#ifndef stderr
-+    EXPLICIT_SYMBOL(stderr);
-+#endif
-+  }
-+#endif
-+#undef EXPLICIT_SYMBOL
-+
-+#define EXPLICIT_SYMBOL(SYM) \
-+   extern "C" void *SYM; symMap[#SYM] = &SYM
-+
-+  // If this is darwin, it has some funky issues, try to solve them here.  Some
-+  // important symbols are marked 'private external' which doesn't allow
-+  // SearchForAddressOfSymbol to find them.  As such, we special case them here,
-+  // there is only a small handful of them.
-+
-+#ifdef __APPLE__
-+  {
-+    // __eprintf is sometimes used for assert() handling on x86.
-+    //
-+    // FIXME: Currently disabled when using Clang, as we don't always have our
-+    // runtime support libraries available.
-+#ifndef __clang__
-+#ifdef __i386__
-+    EXPLICIT_SYMBOL(__eprintf);
-+#endif
-+#endif
-+  }
-+#endif
-+
-+#ifdef __CYGWIN__
-+  {
-+    EXPLICIT_SYMBOL(_alloca);
-+    EXPLICIT_SYMBOL(__main);
-+  }
-+#endif
-+
-+#undef EXPLICIT_SYMBOL
-+}
 Index: lib/Support/SearchForAddressOfSpecialSymbol.cpp
 ===================================================================
 --- lib/Support/SearchForAddressOfSpecialSymbol.cpp	(revision 167609)

-  return DoSearch(symbolName);
-}
-}  // namespace llvm
Index: lib/Support/DynamicLibrary.cpp
===================================================================
--- lib/Support/DynamicLibrary.cpp	(revision 167609)

+
+
 #endif // LLVM_ON_WIN32
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)
@@ -1,4 +1,4 @@
-//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
+//===- Windows/DynamicLibrary.cpp - Win32 DL Implementation -----*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -32,15 +32,9 @@
 #endif
 
 namespace llvm {
+
 using namespace sys;
 
-//===----------------------------------------------------------------------===//
-//=== 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,
@@ -48,6 +42,8 @@ extern "C" {
                                     ULONG ModuleSize,
                                     PVOID UserContext)
   {
+    static DenseSet<HMODULE> ProcessDynLibs;
+
     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
     // into the process.
     if (stricmp(ModuleName, "msvci70") != 0 &&
@@ -63,101 +59,63 @@ extern "C" {
 #endif
         stricmp(ModuleName, "msvcrt20") != 0 &&
         stricmp(ModuleName, "msvcrt40") != 0) {
-      OpenedHandles->insert((HMODULE)ModuleBase);
+      if (ProcessDynLibs.insert((HMODULE)ModuleBase)->second)
+        ++(*OpenedHandles)[(HMODULE)ModuleBase];
     }
     return TRUE;
   }
 }
 
-DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
-                                                   std::string *errMsg) {
-  SmartScopedLock<true> lock(getMutex());
-
+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 DenseMap<void *, unsigned>();
 
     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);
 
   if (a_handle == 0) {
     MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
-    return DynamicLibrary();
+    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);
+void CloseLibraryImpl(void *dlHandle) {
+  FreeLibrary((HMODULE)dlHandle);
+}
 
-  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.
+      return 0;
+    } else {
+      // Perform a global symbol search.
+      DynamicLibrary::SearchForAddressOfSymbol(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.
+void PopulateSpecialSymbolsImpl(StringMap<void *>& symMap) {
+
 #define EXPLICIT_SYMBOL(SYM)                    \
-  extern "C" { extern void *SYM; }
-#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
+  extern "C" void *SYM; symMap[#SYM] = &SYM;
+#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
+  symMap[#SYMFROM] = &SYMTO;
 
 #include "explicit_symbols.inc"
 
 #undef EXPLICIT_SYMBOL
 #undef EXPLICIT_SYMBOL2
-
-void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
-  SmartScopedLock<true> Lock(getMutex());
-
-  // First check symbols added via AddSymbol().
-  if (ExplicitSymbols) {
-    StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
-
-    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;
 }
-
-
-void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
-  if (!isValid())
-    return NULL;
-  if (Data == &OpenedHandles)
-    return SearchForAddressOfSymbol(symbolName);
-  return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
-}
-
-
-}
