-(defstruct shared-object file sap)
-
-(defun dlopen-or-lose (&optional (obj nil objp))
- (when objp
- (dlclose-or-lose obj))
- (dlerror) ; clear errors
- (let* ((file (and obj (shared-object-file obj)))
- (sap (dlopen file (logior rtld-global rtld-now))))
- (aver (or (not objp) file))
- (when (zerop (sap-int sap))
- (if objp
- (setf (shared-object-sap obj) nil)
- (setf *runtime-dlhandle* nil))
- (error "Could not open ~:[runtime~;~:*shared object ~S~]: ~A"
- obj (dlerror)))
- (when objp
- (setf (shared-object-sap obj) sap))
- sap))
-
-(defun dlclose-or-lose (&optional (obj nil objp))
- (dlerror)
- (let (dlerror)
- (cond ((and (not objp) *runtime-dlhandle*)
- (dlclose *runtime-dlhandle*)
- (setf dlerror (dlerror)
- *runtime-dlhandle* nil))
- ((and objp (shared-object-sap obj))
- (dlclose (shared-object-sap obj))
- (setf dlerror (dlerror)
- (shared-object-sap obj) nil)))
- (when dlerror
- (cerror "Ignore the error and continue anyway" "dlerror returned an error: ~S" dlerror))))
-
-(defun load-shared-object (file)
- "Load a shared library/dynamic shared object file/general dlopenable
-alien container, such as a .so on an ELF platform.
-
-Reloading the same shared object will replace the old definitions; if
-a symbol was previously referenced thru the object and is not present
-in the reloaded version an error will be signalled. Sameness is
-determined using the library filename. Reloading may not work as
-expected if user or library-code has called dlopen on FILE.
-
-References to foreign symbols in loaded shared objects do not survive
-intact through SB-EXT:SAVE-LISP-AND die on all platforms. See
-SB-EXT:SAVE-LISP-AND-DIE for details."
- (sb!thread:with-mutex (*foreign-lock*)
- (let* ((filename (or (unix-namestring file) file))
- (old (find filename *shared-objects* :key #'shared-object-file :test #'equal))
- (obj (or old (make-shared-object :file filename))))
- (dlopen-or-lose obj)
- (setf *shared-objects* (append (remove obj *shared-objects*)
- (list obj)))
- #!+linkage-table
- (when (or old (undefined-foreign-symbols))
- (update-linkage-table))
- (pathname filename))))
+(defstruct shared-object pathname namestring handle dont-save)
+
+(defun load-shared-object (pathname &key dont-save)
+ #!+sb-doc
+ "Load a shared library / dynamic shared object file / similar foreign
+container specified by designated PATHNAME, such as a .so on an ELF platform.
+
+Locating the shared object follows standard rules of the platform, consult the
+manual page for dlopen(3) for details. Typically paths speficied by
+environment variables such as LD_LIBRARY_PATH are searched if the PATHNAME has
+no directory, but on some systems (eg. Mac OS X) search may happen even if
+PATHNAME is absolute. (On Windows LoadLibrary is used instead of dlopen(3).)
+
+On non-Windows platoforms calling LOAD-SHARED-OBJECT again with an PATHNAME
+EQUAL to the designated pathname of a previous call will replace the old
+definitions; if a symbol was previously referenced thru the object and is not
+present in the reloaded version an error will be signalled. Reloading may not
+work as expected if user or library-code has called dlopen(3) on the same
+shared object.
+
+LOAD-SHARED-OBJECT interacts with SB-EXT:SAVE-LISP-AND-DIE:
+
+1. If DONT-SAVE is true (default is NIL), the shared object will be dropped
+when SAVE-LISP-AND-DIE is called -- otherwise shared objects are reloaded
+automatically when a saved core starts up. Specifying DONT-SAVE can be useful
+when the location of the shared object on startup is uncertain.
+
+2. On most platforms references in compiled code to foreign symbols in shared
+objects (such as those generated by DEFINE-ALIEN-ROUTINE) remain valid across
+SAVE-LISP-AND-DIE. On those platforms where this is not supported, a WARNING
+will be signalled when the core is saved -- this is orthogonal from DONT-SAVE."
+ (let ((pathname (pathname pathname)))
+ (sb!thread:with-mutex (*shared-objects-lock*)
+ (let* ((old (find pathname *shared-objects*
+ :key #'shared-object-pathname
+ :test #'equal))
+ (obj (or old (make-shared-object
+ :pathname pathname
+ :namestring (native-namestring
+ (translate-logical-pathname pathname)
+ :as-file t)))))
+ (setf (shared-object-dont-save obj) dont-save)
+ ;; FIXME: Why doesn's dlopen-or-lose on already loaded stuff work on
+ ;; Windows?
+ #!-win32
+ (dlopen-or-lose obj)
+ #!+win32
+ (unless old
+ (dlopen-or-lose obj))
+ (setf *shared-objects* (append (remove obj *shared-objects*)
+ (list obj)))
+ ;; FIXME: Why doesn't the linkage table work on Windows? (Or maybe it
+ ;; does and this can be just #!+linkage-table?) Note: remember to change
+ ;; FOREIGN-DEINIT as well then!
+ #!+(and linkage-table (not win32))
+ (when (or old (undefined-foreign-symbols-p))
+ (update-linkage-table))))
+ pathname))
+
+(defun unload-shared-object (pathname)
+ #!+sb-doc
+ "Unloads the shared object loaded earlier using the designated PATHNAME with
+LOAD-SHARED-OBJECT, to the degree supported on the platform.
+
+Experimental."
+ (let ((pathname (pathname pathname)))
+ (sb!thread:with-mutex (*shared-objects-lock*)
+ (let ((old (find pathname *shared-objects*
+ :key #'shared-object-pathname
+ :test #'equal)))
+ (when old
+ #!-hpux (dlclose-or-lose old)
+ (setf *shared-objects* (remove old *shared-objects*))
+ #!+(and linkage-table (not win32))
+ (update-linkage-table))))))