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."
29 (define-alien-variable undefined-alien-address unsigned)
30 (defvar *runtime-dlhandle*))
32 (defvar *shared-objects*)
34 (defstruct shared-object pathname namestring handle dont-save)
36 (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
73 (translate-logical-pathname pathname)
75 (setf (shared-object-dont-save obj) dont-save)
76 ;; FIXME: Why doesn's dlopen-or-lose on already loaded stuff work on
79 ;; Kovalenko 2010-11-24: It would work, but it does nothing
80 ;; useful on Windows: library reference count is increased
81 ;; after each LoadLibrary, making it harder to unload it, and
82 ;; that's all the effect. Also, equal pathnames on Windows
83 ;; always designate _exactly the same library image_; Unix
84 ;; tricks like deleting an open library and replacing it with
85 ;; another version just don't work here.
91 (setf *shared-objects* (append (remove obj *shared-objects*)
93 ;; FIXME: Why doesn't the linkage table work on Windows? (Or maybe it
94 ;; does and this can be just #!+linkage-table?) Note: remember to change
95 ;; FOREIGN-DEINIT as well then!
97 ;; Kovalenko 2010-11-24: I think so. Alien _data_ references
98 ;; are the only thing on win32 that is even slightly
99 ;; problematic. Handle function references in the same way as
100 ;; other linkage-table platforms is easy.
103 (when (or old (undefined-foreign-symbols-p))
104 (update-linkage-table))))
107 (defun unload-shared-object (pathname)
109 "Unloads the shared object loaded earlier using the designated PATHNAME with
110 LOAD-SHARED-OBJECT, to the degree supported on the platform.
113 (let ((pathname (pathname pathname)))
114 (sb!thread:with-mutex (*shared-objects-lock*)
115 (let ((old (find pathname *shared-objects*
116 :key #'shared-object-pathname
119 #!-hpux (dlclose-or-lose old)
120 (setf *shared-objects* (remove old *shared-objects*))
122 (update-linkage-table))))))
124 (defun try-reopen-shared-object (obj)
125 (declare (type shared-object obj))
130 :report "Skip this shared object and continue."
131 ;; By returning NIL the shared object is dropped from the list.
132 (setf (shared-object-handle obj) nil)
133 (return-from try-reopen-shared-object nil))
135 :report "Retry loading this shared object."
138 :report "Specify a different pathname to load the shared object from."
140 (format *query-io* "~&Enter pathname (evaluated):~%")
141 (force-output *query-io*)
142 (let ((pathname (ignore-errors (pathname (read *query-io*)))))
143 (unless (pathnamep pathname)
144 (format *query-io* "~&Error: invalid pathname.~%")
146 (setf (shared-object-pathname obj) pathname)
147 (setf (shared-object-namestring obj)
148 (native-namestring (translate-logical-pathname pathname)
153 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
155 (defun reopen-shared-objects ()
156 ;; Ensure that the runtime is open
157 (setf *runtime-dlhandle* (dlopen-or-lose))
159 (setf *shared-objects*
160 (remove nil (mapcar #'try-reopen-shared-object *shared-objects*))))
162 ;;; Close all dlopened libraries and clear out sap entries in
163 ;;; *SHARED-OBJECTS*, and drop the ones with DONT-SAVE set.
164 (defun close-shared-objects ()
166 (dolist (obj (reverse *shared-objects*))
167 #!-hpux (dlclose-or-lose obj)
168 (unless (shared-object-dont-save obj)
170 (setf *shared-objects* saved))
174 (let ((symbols (make-hash-table :test #'equal))
175 (undefineds (make-hash-table :test #'equal)))
176 (defun ensure-dynamic-foreign-symbol-address (symbol &optional datap)
177 "Returns the address of the foreign symbol as an integer. On linkage-table
178 ports if the symbols isn't found a special guard address is returned instead,
179 accesses to which will result in an UNDEFINED-ALIEN-ERROR. On other ports an
180 error is immediately signalled if the symbol isn't found. The returned address
181 is never in the linkage-table."
182 (declare (ignorable datap))
183 (let ((addr (find-dynamic-foreign-symbol-address symbol)))
184 (cond #!-linkage-table
186 (error 'undefined-alien-error :name symbol))
189 (style-warn 'sb!kernel:undefined-alien-style-warning
191 (setf (gethash symbol undefineds) t)
192 (remhash symbol symbols)
194 undefined-alien-address
195 (foreign-symbol-address "undefined_alien_function")))
197 (setf (gethash symbol symbols) t)
198 (remhash symbol undefineds)
200 (defun undefined-foreign-symbols-p ()
201 (plusp (hash-table-count undefineds)))
202 (defun dynamic-foreign-symbols-p ()
203 (plusp (hash-table-count symbols)))
204 (defun list-dynamic-foreign-symbols ()
205 (loop for symbol being each hash-key in symbols
207 (defun list-undefined-foreign-symbols ()
208 (loop for symbol being each hash-key in undefineds