0.9.10.12:
[sbcl.git] / src / code / win32-foreign-load.lisp
1 ;;;; Loading shared object files
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!ALIEN")
13
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"))
19
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."
23   (load-foreign))
24
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."
28   (load-1-foreign))
29
30 (define-alien-type hinstance long)
31
32 (define-alien-routine ("LoadLibraryA@4" loadlibrary) hinstance
33   (file c-string))
34
35 (define-alien-routine ("FreeLibrary@4" freelibrary) int
36   (handle hinstance))
37
38 (define-alien-routine ("GetProcAddress@8" getprocaddress) system-area-pointer
39   (handle hinstance)
40   (symbol c-string))
41
42 (define-alien-routine ("GetLastError@0" getlasterror) unsigned-int)
43
44 (defvar *shared-objects*)
45
46 (defstruct shared-object file handle)
47
48 (defun dlopen-or-lose (obj)
49   (let* ((file (shared-object-file obj))
50          (handle (loadlibrary file)))
51     (aver file)
52     (when (zerop handle)
53       (setf (shared-object-handle obj) nil)
54       (error "Error opening shared object ~S:~%  ~A."
55              file (getlasterror)))
56     (setf (shared-object-handle obj) handle)
57     handle))
58
59 (defun dlclose-or-lose (&optional (obj nil objp))
60   (let (dlerror)
61     (cond ((and objp (shared-object-handle obj))
62            (setf dlerror (if (freelibrary (shared-object-handle obj))
63                              nil
64                              (getlasterror))
65                  (shared-object-handle obj) nil)))
66     (when dlerror
67       (cerror "Ignore the error and continue anyway" "dlerror returned an error: ~S" dlerror))))
68
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.
72
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.
78
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))))
86       (unless old
87         (dlopen-or-lose obj))
88       (setf *shared-objects* (append (remove obj *shared-objects*)
89                                      (list obj)))
90       (pathname filename))))
91
92 (defun try-reopen-shared-object (obj)
93   (declare (type shared-object obj))
94   (tagbody :dlopen
95      (restart-case
96          (dlopen-or-lose obj)
97        (continue ()
98          :report "Skip this shared object and continue."
99          (setf (shared-object-handle obj) nil))
100        (retry ()
101          :report "Retry loading this shared object."
102          (go :dlopen))
103        (load-other ()
104          :report "Specify an alternate shared object file to load."
105          (setf (shared-object-file obj)
106                (tagbody :query
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.~%")
112                       (go :query))
113                     (unix-namestring pathname)))))))
114   obj)
115
116 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
117 ;;; initialization.
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*)))
121
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*)))
126
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))
139          (result nil))
140     (dolist (obj *shared-objects*)
141       (let ((handle (shared-object-handle obj)))
142         (when handle
143           (setf result (sap-int (getprocaddress handle extern)))
144           (when (not (zerop result))
145             (return result)))))))
146
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)))
157       (cond  ((not addr)
158               (error 'undefined-alien-error :name symbol))
159              (addr
160               (setf (gethash symbol symbols) t)
161               (remhash symbol undefineds)
162               addr))))
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
169          collect symbol)))
170