Index: /trunk/source/level-0/l0-cfm-support.lisp
===================================================================
--- /trunk/source/level-0/l0-cfm-support.lisp	(revision 8586)
+++ /trunk/source/level-0/l0-cfm-support.lisp	(revision 8587)
@@ -67,5 +67,5 @@
   shlib.soname
   shlib.pathname
-  shlib.opened-by-lisp-kernel
+  shlib.handle                          ; if explicitly opened
   shlib.map
   shlib.base
@@ -263,11 +263,10 @@
   (when (null *shared-libraries*)
     (%walk-shared-libraries #'shlib-from-map-entry)
-    (dolist (l *shared-libraries*)
       ;;; On Linux, it seems to be necessary to open each of these
       ;;; libraries yet again, specifying the RTLD_GLOBAL flag.
       ;;; On FreeBSD, it seems desirable -not- to do that.
-      #+linux-target
-      (%dlopen-shlib l)
-      (setf (shlib.opened-by-lisp-kernel l) t))))
+    #+linux-target
+    (dolist (l *shared-libraries*)
+      (%dlopen-shlib l))))
 
 (init-shared-libraries)
@@ -297,27 +296,27 @@
 loaded, signal a SIMPLE-ERROR which contains an often-cryptic message from
 the operating system."
-  (let* ((link-map
-          (let* ((lib (with-cstrs ((name name))
+  (let* ((handle (with-cstrs ((name name))
                         (ff-call
                          (%kernel-import target::kernel-import-GetSharedLibrary)
                          :address name
                          :unsigned-fullword *dlopen-flags*
-                         :address))))
-            #+linux-target lib
-            #+freebsd-target (if (%null-ptr-p lib)
-                               lib
-                               (rlet ((p :address))
-                                 (if (eql 0 (ff-call
-                                             (foreign-symbol-entry "dlinfo")
-                                             :address lib
-                                             :int #$RTLD_DI_LINKMAP
-                                             :address p
-                                             :int))
-                                   (pref p :address)
-                                   (%null-ptr)))))))
+                         :address)))
+         (link-map #-freebsd-target handle
+                   #+freebsd-target (if (%null-ptr-p handle)
+                                      handle
+                                      (rlet ((p :address))
+                                        (if (eql 0 (ff-call
+                                                    (foreign-symbol-entry "dlinfo")
+                                                    :address handle
+                                                    :int #$RTLD_DI_LINKMAP
+                                                    :address p
+                                                    :int))
+                                          (pref p :address)
+                                          (%null-ptr))))))
     (if (%null-ptr-p link-map)
       (error "Error opening shared library ~s: ~a" name (dlerror))
       (prog1 (let* ((lib (shlib-from-map-entry link-map)))
 	       (incf (shlib.opencount lib))
+               (setf (shlib.handle lib) handle)
 	       lib)
 	(%walk-shared-libraries
Index: /trunk/source/level-1/linux-files.lisp
===================================================================
--- /trunk/source/level-1/linux-files.lisp	(revision 8586)
+++ /trunk/source/level-1/linux-files.lisp	(revision 8587)
@@ -596,5 +596,5 @@
 
 ;;; Kind of has something to do with files, and doesn't work in level-0.
-#+linux-target
+#+(or linux-target freebsd-target)
 (defun close-shared-library (lib &key (completely t))
   "If completely is T, set the reference count of library to 0. Otherwise,
@@ -606,12 +606,11 @@
 		    (error "Shared library ~s not found." lib))
 		(require-type lib 'shlib)))
-	 (map (shlib.map lib)))
-    (unless (shlib.opened-by-lisp-kernel lib)
-      (when map
+	 (handle (shlib.handle lib)))
+      (when handle
 	(let* ((found nil)
 	       (base (shlib.base lib)))
 	  (do* ()
 	       ((progn		  
-		  (#_dlclose map)
+		  (#_dlclose handle)
 		  (or (not (setq found (shlib-containing-address base)))
 		      (not completely)))))
@@ -619,7 +618,8 @@
 	    (setf (shlib.pathname lib) nil
 	      (shlib.base lib) nil
+              (shlib.handle lib) nil
 	      (shlib.map lib) nil)
             (unload-foreign-variables lib)
-	    (unload-library-entrypoints lib)))))))
+	    (unload-library-entrypoints lib))))))
 
 #+darwin-target
