 -    FreeLibrary(a_handle);
 +bool CloseLibraryImpl(void *DlHandle, std::string *ErrMsg) {
 +  if (!FreeLibrary((HMODULE)DlHandle)) {
-+    MakeErrMsg(ErrMsg, "Cannot free DLL: ");
++    MakeErrMsg(ErrMsg, std::string(Filename) + ": Cannot close: ");
 +    return false;
 +  }
 +  return true;

===================================================================
--- include/llvm/Support/DynamicLibrary.h	(revision 167609)
+++ include/llvm/Support/DynamicLibrary.h	(working copy)
@@ -48,14 +48,19 @@ namespace sys {
     /// Returns true if the object refers to a valid library.
     bool isValid() { return Data != &Invalid; }
 
-    /// Searches through the library for the symbol \p symbolName. If it is
+    /// Searches through the library for the symbol \p SymbolName. If it is
     /// found, the address of that symbol is returned. If not, NULL is returned.
     /// Note that NULL will also be returned if the library failed to load.
     /// Use isValid() to distinguish these cases if it is important.
     /// Note that this will \e not search symbols explicitly registered by
     /// AddSymbol().
-    void *getAddressOfSymbol(const char *symbolName);
+    void *getAddressOfSymbol(const char *SymbolName);
 
+    /// Unload the dynamic library. \p *errMsg will be set to an error message
+    /// if the library fails to unload. Returns true on success.
+    ///
+    bool Unload(std::string *ErrMsg = 0);
+
     /// This function permanently loads the dynamic library at the given path.
     /// The library will only be unloaded when the program terminates.
     /// This returns a valid DynamicLibrary instance on success and an invalid
@@ -64,8 +69,10 @@ namespace sys {
     ///
     /// It is safe to call this function multiple times for the same library.
     /// @brief Open a dynamic library permanently.
-    static DynamicLibrary getPermanentLibrary(const char *filename,
-                                              std::string *errMsg = 0);
+    static DynamicLibrary getPermanentLibrary(const char *Filename,
+                                              std::string *ErrMsg = 0) {
+      return getLibrary(Filename, true /*permanent*/, ErrMsg);
+    }
 
     /// This function permanently loads the dynamic library at the given path.
     /// Use this instead of getPermanentLibrary() when you won't need to get
@@ -77,25 +84,58 @@ namespace sys {
       return !getPermanentLibrary(Filename, ErrMsg).isValid();
     }
 
-    /// This function will search through all previously loaded dynamic
-    /// libraries for the symbol \p symbolName. If it is found, the address of
-    /// that symbol is returned. If not, null is returned. Note that this will
-    /// search permanently loaded libraries (getPermanentLibrary()) as well
-    /// as explicitly registered symbols (AddSymbol()).
-    /// @throws std::string on error.
-    /// @brief Search through libraries for address of a symbol
-    static void *SearchForAddressOfSymbol(const char *symbolName);
+    /// This function loads the dynamic library at the given path. If 
+    /// !permanent, the library can be unloaded by UnloadLibrary().
+    /// This returns a valid DynamicLibrary instance on success and an invalid
+    /// instance on failure (see isValid()). \p *errMsg will only be modified
+    /// if the library fails to load.
+    ///
+    /// It is safe to call this function multiple times for the same library.
+    /// @brief Open a dynamic library.
+    static DynamicLibrary getLibrary(const char *Filename,
+                                     bool Permanent = false,
+                                     std::string *ErrMsg = 0);
 
+    /// This function loads the dynamic library at the given path. If 
+    /// !permanent, the library can be unloaded by UnloadLibrary().
+    /// Use this instead of getLibrary() when you won't need to get
+    /// symbols from the library itself.
+    ///
+    /// It is safe to call this function multiple times for the same library.
+    static bool LoadLibrary(const char *Filename, bool Permanent = false,
+                            std::string *ErrMsg = 0) {
+      return !getLibrary(Filename, Permanent, ErrMsg).isValid();
+    }
+
+    /// Unload the dynamic library at the given path. \p *errMsg will be
+    /// set to an error message if the library fails to unload. Returns
+    /// true on success.
+    ///
+    static bool Unload(const char *Filename, std::string *ErrMsg = 0);
+
+    /// Search through all previously loaded dynamic libraries and explicitly
+    /// registered symbols (AddSymbol()) for the symbol \p symbolName. If it is
+    /// found, the address of that symbol is returned. If not, null is returned.
+    /// @brief Search through libraries and added symbols for address of a symbol
+    static void *SearchForAddressOfSymbol(const char *SymbolName);
+
     /// @brief Convenience function for C++ophiles.
-    static void *SearchForAddressOfSymbol(const std::string &symbolName) {
-      return SearchForAddressOfSymbol(symbolName.c_str());
+    static void *SearchForAddressOfSymbol(const std::string &SymbolName) {
+      return SearchForAddressOfSymbol(SymbolName.c_str());
     }
 
+    /// This function will search through all previously loaded dynamic

-  return DoSearch(symbolName);
-}
-}  // namespace llvm
Index: lib/Support/Unix/DynamicLibrary.inc
===================================================================
--- 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
+//
+// 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>
+
+static void MakeDlErrMsg(std::string *ErrMsg) {
+  if (ErrMsg) {
+    *ErrMsg = dlerror();
+  } else {
+    // Flush error state whether or not ErrMsg is provided.
+    dlerror();
+  }
+}
+
+void *OpenLibraryImpl(const char *Filename, std::string *ErrMsg) {
+  void *Handle = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
+  if (Handle == 0) {
+    MakeDlErrMsg(ErrMsg);
+    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;
+}
+
+bool CloseLibraryImpl(void *DlHandle, std::string *ErrMsg) {
+  if (dlclose(DlHandle)) {
+    MakeDlErrMsg(ErrMsg);
+    return false;
+  }
+  return true;
+}
+
+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
+// 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
+}
