1 ;;;; Loading shared object files
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!ALIEN")
14 ;;; Used to serialize modifications to *shared-objects*.
15 (defvar *shared-objects-lock*
16 (sb!thread:make-mutex :name "shared object list lock"))
18 (define-unsupported-fun load-foreign
19 "Unsupported as of SBCL 0.8.13. See LOAD-SHARED-OBJECT."
20 "~S is unsupported as of SBCL 0.8.13. See LOAD-SHARED-OBJECT."
23 (define-unsupported-fun load-1-foreign
24 "Unsupported as of SBCL 0.8.13. Please use LOAD-SHARED-OBJECT."
25 "~S is unsupported as of SBCL 0.8.13. Please use LOAD-SHARED-OBJECT."
30 (define-alien-variable undefined-alien-address unsigned-long)
31 (defvar *runtime-dlhandle*))
33 (defvar *shared-objects*)
35 (defstruct shared-object pathname namestring handle dont-save)
37 (defun load-shared-object (pathname &key dont-save)
38 "Load a shared library / dynamic shared object file / similar foreign
39 container specified by designated PATHNAME, such as a .so on an ELF platform.
41 Locating the shared object follows standard rules of the platform, consult the
42 manual page for dlopen(3) for details. Typically paths speficied by
43 environment variables such as LD_LIBRARY_PATH are searched if the PATHNAME has
44 no directory, but on some systems (eg. Mac OS X) search may happen even if
45 PATHNAME is absolute. (On Windows LoadLibrary is used instead of dlopen(3).)
47 On non-Windows platoforms calling LOAD-SHARED-OBJECT again with an PATHNAME
48 EQUAL to the designated pathname of a previous call will replace the old
49 definitions; if a symbol was previously referenced thru the object and is not
50 present in the reloaded version an error will be signalled. Reloading may not
51 work as expected if user or library-code has called dlopen(3) on the same
54 LOAD-SHARED-OBJECT interacts with SB-EXT:SAVE-LISP-AND-DIE:
56 1. If DONT-SAVE is true (default is NIL), the shared object will be dropped
57 when SAVE-LISP-AND-DIE is called -- otherwise shared objects are reloaded
58 automatically when a saved core starts up. Specifying DONT-SAVE can be useful
59 when the location of the shared object on startup is uncertain.
61 2. On most platforms references in compiled code to foreign symbols in shared
62 objects (such as those generated by DEFINE-ALIEN-ROUTINE) remain valid across
63 SAVE-LISP-AND-DIE. On those platforms where this is not supported, a WARNING
64 will be signalled when the core is saved -- this is orthogonal from DONT-SAVE."
65 (let ((pathname (pathname pathname)))
66 (sb!thread:with-mutex (*shared-objects-lock*)
67 (let* ((old (find pathname *shared-objects*
68 :key #'shared-object-pathname
70 (obj (or old (make-shared-object
72 :namestring (native-namestring pathname :as-file t)))))
73 (setf (shared-object-dont-save obj) dont-save)
74 ;; FIXME: Why doesn's dlopen-or-lose on already loaded stuff work on
81 (setf *shared-objects* (append (remove obj *shared-objects*)
83 ;; FIXME: Why doesn't the linkage table work on Windows? (Or maybe it
84 ;; does and this can be just #!+linkage-table?) Note: remember to change
85 ;; FOREIGN-DEINIT as well then!
86 #!+(and linkage-table (not win32))
87 (when (or old (undefined-foreign-symbols-p))
88 (update-linkage-table))))
91 (defun try-reopen-shared-object (obj)
92 (declare (type shared-object obj))
97 :report "Skip this shared object and continue."
98 ;; By returning NIL the shared object is dropped from the list.
99 (setf (shared-object-handle obj) nil)
100 (return-from try-reopen-shared-object nil))
102 :report "Retry loading this shared object."
105 :report "Specify a different pathname to load the shared object from."
107 (format *query-io* "~&Enter pathname (evaluated):~%")
108 (force-output *query-io*)
109 (let ((pathname (ignore-errors (pathname (read *query-io*)))))
110 (unless (pathnamep pathname)
111 (format *query-io* "~&Error: invalid pathname.~%")
113 (setf (shared-object-pathname obj) pathname
114 (shared-object-namestring obj) (native-namestring pathname :as-file t))))
118 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
120 (defun reopen-shared-objects ()
121 ;; Ensure that the runtime is open
123 (setf *runtime-dlhandle* (dlopen-or-lose))
125 (setf *shared-objects*
126 (remove nil (mapcar #'try-reopen-shared-object *shared-objects*))))
128 ;;; Close all dlopened libraries and clear out sap entries in
129 ;;; *SHARED-OBJECTS*, and drop the ones with DONT-SAVE set.
130 (defun close-shared-objects ()
132 (dolist (obj (reverse *shared-objects*))
133 (dlclose-or-lose obj)
134 (unless (shared-object-dont-save obj)
136 (setf *shared-objects* saved))
140 (let ((symbols (make-hash-table :test #'equal))
141 (undefineds (make-hash-table :test #'equal)))
142 (defun ensure-dynamic-foreign-symbol-address (symbol &optional datap)
143 "Returns the address of the foreign symbol as an integer. On linkage-table
144 ports if the symbols isn't found a special guard address is returned instead,
145 accesses to which will result in an UNDEFINED-ALIEN-ERROR. On other ports an
146 error is immediately signalled if the symbol isn't found. The returned address
147 is never in the linkage-table."
148 (declare (ignorable datap))
149 (let ((addr (find-dynamic-foreign-symbol-address symbol)))
150 (cond #!-(and linkage-table (not win32))
152 (error 'undefined-alien-error :name symbol))
153 #!+(and linkage-table (not win32))
155 (style-warn 'sb!kernel:undefined-alien-style-warning
157 (setf (gethash symbol undefineds) t)
158 (remhash symbol symbols)
160 undefined-alien-address
161 (foreign-symbol-address "undefined_alien_function")))
163 (setf (gethash symbol symbols) t)
164 (remhash symbol undefineds)
166 (defun undefined-foreign-symbols-p ()
167 (plusp (hash-table-count undefineds)))
168 (defun dynamic-foreign-symbols-p ()
169 (plusp (hash-table-count symbols)))
170 (defun list-dynamic-foreign-symbols ()
171 (loop for symbol being each hash-key in symbols