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 *linkage-info*,
15 ;;; *shared-objects* and the linkage-table proper. Calls thru
16 ;;; linkage-table are unaffected.
17 (defvar *foreign-lock
*
18 (sb!thread
:make-mutex
:name
"foreign definition lock"))
20 (define-unsupported-fun load-foreign
21 "Unsupported as of SBCL 0.8.13. See LOAD-SHARED-OBJECT."
22 "~S is unsupported as of SBCL 0.8.13. See LOAD-SHARED-OBJECT."
25 (define-unsupported-fun load-1-foreign
26 "Unsupported as of SBCL 0.8.13. Please use LOAD-SHARED-OBJECT."
27 "~S is unsupported as of SBCL 0.8.13. Please use LOAD-SHARED-OBJECT."
30 (define-alien-type hinstance long
)
32 (define-alien-routine ("LoadLibraryA@4" loadlibrary
) hinstance
35 (define-alien-routine ("FreeLibrary@4" freelibrary
) int
38 (define-alien-routine ("GetProcAddress@8" getprocaddress
) system-area-pointer
42 (define-alien-routine ("GetLastError@0" getlasterror
) unsigned-int
)
44 (defvar *shared-objects
*)
46 (defstruct shared-object file handle
)
48 (defun dlopen-or-lose (obj)
49 (let* ((file (shared-object-file obj
))
50 (handle (loadlibrary file
)))
53 (setf (shared-object-handle obj
) nil
)
54 (error "Error opening shared object ~S:~% ~A."
56 (setf (shared-object-handle obj
) handle
)
59 (defun dlclose-or-lose (&optional
(obj nil objp
))
61 (cond ((and objp
(shared-object-handle obj
))
62 (setf dlerror
(if (freelibrary (shared-object-handle obj
))
65 (shared-object-handle obj
) nil
)))
67 (cerror "Ignore the error and continue anyway" "dlerror returned an error: ~S" dlerror
))))
69 (defun load-shared-object (file)
70 "Load a shared library/dynamic shared object file/general dlopenable
71 alien container, such as a .so on an ELF platform.
73 Reloading the same shared object will replace the old definitions; if
74 a symbol was previously referenced thru the object and is not present
75 in the reloaded version an error will be signalled. Sameness is
76 determined using the library filename. Reloading may not work as
77 expected if user or library-code has called dlopen on FILE.
79 References to foreign symbols in loaded shared objects do not survive
80 intact through SB-EXT:SAVE-LISP-AND-DIE on all platforms. See
81 SB-EXT:SAVE-LISP-AND-DIE for details."
82 (sb!thread
:with-mutex
(*foreign-lock
*)
83 (let* ((filename (or (unix-namestring file
) file
))
84 (old (find filename
*shared-objects
* :key
#'shared-object-file
:test
#'equal
))
85 (obj (or old
(make-shared-object :file filename
))))
88 (setf *shared-objects
* (append (remove obj
*shared-objects
*)
90 (pathname filename
))))
92 (defun try-reopen-shared-object (obj)
93 (declare (type shared-object obj
))
98 :report
"Skip this shared object and continue."
99 (setf (shared-object-handle obj
) nil
))
101 :report
"Retry loading this shared object."
104 :report
"Specify an alternate shared object file to load."
105 (setf (shared-object-file obj
)
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 (unix-namestring pathname
)))))))
116 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
118 ;;; Note that, so long as we don't have linkage-table, this is braindead.
119 (defun reopen-shared-objects ()
120 (setf *shared-objects
* (mapcar #'try-reopen-shared-object
*shared-objects
*)))
122 ;;; Close all dlopened libraries and clear out sap entries in
123 ;;; *SHARED-OBJECTS*.
124 (defun close-shared-objects ()
125 (mapc #'dlclose-or-lose
(reverse *shared-objects
*)))
127 (defun find-dynamic-foreign-symbol-address (symbol)
128 ;; On real ELF & dlsym platforms the EXTERN-ALIEN-NAME is a no-op,
129 ;; but on platforms where dlsym is simulated we use the mangled name.
130 ;; Win32 is a special case. It needs EXTERN-ALIEN-NAME to mangle the
131 ;; name for static linkage, but also needs unmangled symbols for
132 ;; GetProcAddress(). So we coerce to base-string instead.
133 ;; Oh, and we assume that all runtime symbols are static-linked.
134 ;; No *runtime-dlhandle* for us.
135 ;; Also, GetProcAddress doesn't call SetLastError(0) on success,
136 ;; and GetLastError() doesn't either. For now, we assume that
137 ;; GetProcAddress() won't return NULL on success.
138 (let* ((extern (coerce symbol
'base-string
))
140 (dolist (obj *shared-objects
*)
141 (let ((handle (shared-object-handle obj
)))
143 (setf result
(sap-int (getprocaddress handle extern
)))
144 (when (not (zerop result
))
145 (return result
)))))))
147 (let ((symbols (make-hash-table :test
#'equal
))
148 (undefineds (make-hash-table :test
#'equal
)))
149 (defun ensure-dynamic-foreign-symbol-address (symbol &optional datap
)
150 "Returns the address of the foreign symbol as an integer. On linkage-table
151 ports if the symbols isn't found a special guard address is returned instead,
152 accesses to which will result in an UNDEFINED-ALIEN-ERROR. On other ports an
153 error is immediately signalled if the symbol isn't found. The returned address
154 is never in the linkage-table."
155 (declare (ignorable datap
))
156 (let ((addr (find-dynamic-foreign-symbol-address symbol
)))
158 (error 'undefined-alien-error
:name symbol
))
160 (setf (gethash symbol symbols
) t
)
161 (remhash symbol undefineds
)
163 (defun undefined-foreign-symbols-p ()
164 (plusp (hash-table-count undefineds
)))
165 (defun dynamic-foreign-symbols-p ()
166 (plusp (hash-table-count symbols
)))
167 (defun list-dynamic-foreign-symbols ()
168 (loop for symbol being each hash-key in symbols