 -  // keep the internal refcount at +1.
 -  if (!OpenedHandles->insert(a_handle).second)
 -    FreeLibrary(a_handle);
-+void CloseLibraryImpl(void *dlHandle) {
-+  FreeLibrary((HMODULE)dlHandle);
++bool CloseLibraryImpl(void *DlHandle, std::string *ErrMsg) {
++  if (!FreeLibrary((HMODULE)DlHandle)) {
++    MakeErrMsg(ErrMsg, std::string(Filename) + ": Cannot close: ");
++    return false;
++  }
++  return true;
 +}
  
 -  return DynamicLibrary(a_handle);
-+void *GetAddressOfSymbolImpl(void *dlHandle, const char *symbolName,
-+                               bool Global) {
-+  if (dlHandle == &OpenedHandles) {
++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.
++      // All libraries are already being searched, do not recurse.
 +      return 0;
 +    } else {
-+      // Perform a global symbol search.
-+      DynamicLibrary::SearchForAddressOfSymbol(symbolName);
++      // Perform a global symbol search through all libraries.
++      DynamicLibrary::SearchForLibrarySymbol(SymbolName);
 +    }
 +  }
-+  return (void *)(intptr_t) GetProcAddress((HMODULE)dlHandle, symbolName);
++  return (void *)(intptr_t) GetProcAddress((HMODULE)DlHandle, SymbolName);
  }
  
 -// Stack probing routines are in the support library (e.g. libgcc), but we don't

+
+#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) \
+    symMap[#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

===================================================================
--- lib/Support/Unix/DynamicLibrary.inc	(revision 0)
+++ lib/Support/Unix/DynamicLibrary.inc	(revision 0)
@@ -0,0 +1,118 @@
+//===--- Unix/DynamicLibrary.cpp - Unix DL Implementation -------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
