 +    return &OpenedHandles;
    }
    
-   HMODULE a_handle = LoadLibrary(filename);
+-  HMODULE a_handle = LoadLibrary(filename);
++  HMODULE a_handle = LoadLibrary(Filename);
  
    if (a_handle == 0) {
-     MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
+-    MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
 -    return DynamicLibrary();
++    MakeErrMsg(ErrMsg, std::string(Filename) + ": Can't open : ");
 +    return NULL;
    }
  

+
+#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

-  // 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, 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) {
+    // 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
