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