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."
28 (define-alien-routine dlopen system-area-pointer
29 (file c-string) (mode int))
31 (define-alien-routine dlclose int
32 (handle system-area-pointer))
34 (define-alien-routine dlerror c-string)
36 (define-alien-routine dlsym system-area-pointer
37 (handle system-area-pointer)
40 (define-alien-variable undefined-alien-address unsigned-long)
42 (defvar *runtime-dlhandle*)
43 (defvar *shared-objects*)
45 (defstruct shared-object file sap)
47 (defun dlopen-or-lose (&optional (obj nil objp))
49 (dlclose-or-lose obj))
50 (dlerror) ; clear errors
51 (let* ((file (and obj (shared-object-file obj)))
52 (sap (dlopen file (logior rtld-global rtld-now))))
53 (aver (or (not objp) file))
54 (when (zerop (sap-int sap))
56 (setf (shared-object-sap obj) nil)
57 (setf *runtime-dlhandle* nil))
58 (error "Error opening ~:[runtime~;shared object ~:*~S~]:~% ~A."
61 (setf (shared-object-sap obj) sap))
64 (defun dlclose-or-lose (&optional (obj nil objp))
67 (cond ((and (not objp) *runtime-dlhandle*)
68 (dlclose *runtime-dlhandle*)
69 (setf dlerror (dlerror)
70 *runtime-dlhandle* nil))
71 ((and objp (shared-object-sap obj))
72 (dlclose (shared-object-sap obj))
73 (setf dlerror (dlerror)
74 (shared-object-sap obj) nil)))
76 (cerror "Ignore the error and continue anyway" "dlerror returned an error: ~S" dlerror))))
78 (defun load-shared-object (file)
79 "Load a shared library/dynamic shared object file/general dlopenable
80 alien container, such as a .so on an ELF platform.
82 Reloading the same shared object will replace the old definitions; if
83 a symbol was previously referenced thru the object and is not present
84 in the reloaded version an error will be signalled. Sameness is
85 determined using the library filename. Reloading may not work as
86 expected if user or library-code has called dlopen on FILE.
88 References to foreign symbols in loaded shared objects do not survive
89 intact through SB-EXT:SAVE-LISP-AND-DIE on all platforms. See
90 SB-EXT:SAVE-LISP-AND-DIE for details."
91 (let ((filename (or (unix-namestring file) file))
93 (sb!thread:with-mutex (*shared-objects-lock*)
94 (setf old (find filename *shared-objects* :key #'shared-object-file :test #'equal))
95 (let* ((obj (or old (make-shared-object :file filename))))
97 (setf *shared-objects* (append (remove obj *shared-objects*)
100 (when (or old (undefined-foreign-symbols-p))
101 (update-linkage-table))
102 (pathname filename)))
104 (defun try-reopen-shared-object (obj)
105 (declare (type shared-object obj))
110 :report "Skip this shared object and continue."
111 (setf (shared-object-sap obj) nil))
113 :report "Retry loading this shared object."
116 :report "Specify an alternate shared object file to load."
117 (setf (shared-object-file obj)
119 (format *query-io* "~&Enter pathname (evaluated):~%")
120 (force-output *query-io*)
121 (let ((pathname (ignore-errors (pathname (read *query-io*)))))
122 (unless (pathnamep pathname)
123 (format *query-io* "~&Error: invalid pathname.~%")
125 (unix-namestring pathname)))))))
128 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
130 (defun reopen-shared-objects ()
131 ;; Ensure that the runtime is open
132 (setf *runtime-dlhandle* (dlopen-or-lose)
133 *shared-objects* (mapcar #'try-reopen-shared-object *shared-objects*)))
135 ;;; Close all dlopened libraries and clear out sap entries in
136 ;;; *SHARED-OBJECTS*.
137 (defun close-shared-objects ()
138 (mapc #'dlclose-or-lose (reverse *shared-objects*))
141 (defun find-dynamic-foreign-symbol-address (symbol)
142 (dlerror) ; clear old errors
143 (unless *runtime-dlhandle*
144 (bug "Cannot resolve foreign symbol: lost *runtime-dlhandle*"))
145 ;; On real ELF & dlsym platforms the EXTERN-ALIEN-NAME is a no-op,
146 ;; but on platforms where dlsym is simulated we use the mangled name.
147 (let* ((extern (extern-alien-name symbol))
148 (result (sap-int (dlsym *runtime-dlhandle* extern)))
150 (if (or (not (zerop result)) (not err))
152 (dolist (obj *shared-objects*)
153 (let ((sap (shared-object-sap obj)))
155 (setf result (sap-int (dlsym sap extern))
157 (when (or (not (zerop result)) (not err))
158 (return result))))))))
160 (let ((symbols (make-hash-table :test #'equal))
161 (undefineds (make-hash-table :test #'equal)))
162 (defun ensure-dynamic-foreign-symbol-address (symbol &optional datap)
163 "Returns the address of the foreign symbol as an integer. On linkage-table
164 ports if the symbols isn't found a special guard address is returned instead,
165 accesses to which will result in an UNDEFINED-ALIEN-ERROR. On other ports an
166 error is immediately signalled if the symbol isn't found. The returned address
167 is never in the linkage-table."
168 (declare (ignorable datap))
169 (let ((addr (find-dynamic-foreign-symbol-address symbol)))
170 (cond #!-linkage-table
172 (error 'undefined-alien-error :name symbol))
175 (style-warn "Undefined alien: ~S" symbol)
176 (setf (gethash symbol undefineds) t)
177 (remhash symbol symbols)
179 undefined-alien-address
180 (foreign-symbol-address "undefined_alien_function")))
182 (setf (gethash symbol symbols) t)
183 (remhash symbol undefineds)
185 (defun undefined-foreign-symbols-p ()
186 (plusp (hash-table-count undefineds)))
187 (defun dynamic-foreign-symbols-p ()
188 (plusp (hash-table-count symbols)))
189 (defun list-dynamic-foreign-symbols ()
190 (loop for symbol being each hash-key in symbols