185d95849f6f3220414124350dc7e74acf327620
[sbcl.git] / src / code / 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 *shared-objects*.
15 (defvar *shared-objects-lock*
16   (sb!thread:make-mutex :name "shared object list lock"))
17
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."
21   (load-foreign))
22
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."
26   (load-1-foreign))
27
28 (progn
29   (define-alien-variable undefined-alien-address unsigned)
30   (defvar *runtime-dlhandle*))
31
32 (defvar *shared-objects*)
33
34 (defstruct shared-object pathname namestring handle dont-save)
35
36 (defun load-shared-object (pathname &key dont-save)
37   #!+sb-doc
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.
40
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).)
46
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
52 shared object.
53
54 LOAD-SHARED-OBJECT interacts with SB-EXT:SAVE-LISP-AND-DIE:
55
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.
60
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
69                         :test #'equal))
70              (obj (or old (make-shared-object
71                            :pathname pathname
72                            :namestring (native-namestring
73                                         (translate-logical-pathname pathname)
74                                         :as-file t)))))
75         (setf (shared-object-dont-save obj) dont-save)
76         ;; FIXME: Why doesn's dlopen-or-lose on already loaded stuff work on
77         ;; Windows?
78         ;;
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.
86         #!-win32
87         (dlopen-or-lose obj)
88         #!+win32
89         (unless old
90           (dlopen-or-lose obj))
91         (setf *shared-objects* (append (remove obj *shared-objects*)
92                                        (list obj)))
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!
96         ;;
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.
101         ;;
102         #!+linkage-table
103         (when (or old (undefined-foreign-symbols-p))
104           (update-linkage-table))))
105     pathname))
106
107 (defun unload-shared-object (pathname)
108   #!+sb-doc
109   "Unloads the shared object loaded earlier using the designated PATHNAME with
110 LOAD-SHARED-OBJECT, to the degree supported on the platform.
111
112 Experimental."
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
117                        :test #'equal)))
118         (when old
119           #!-hpux (dlclose-or-lose old)
120           (setf *shared-objects* (remove old *shared-objects*))
121           #!+linkage-table
122           (update-linkage-table))))))
123
124 (defun try-reopen-shared-object (obj)
125   (declare (type shared-object obj))
126   (tagbody :dlopen
127      (restart-case
128          (dlopen-or-lose obj)
129        (continue ()
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))
134        (retry ()
135          :report "Retry loading this shared object."
136          (go :dlopen))
137        (change-pathname ()
138          :report "Specify a different pathname to load the shared object from."
139          (tagbody :query
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.~%")
145                 (go :query))
146               (setf (shared-object-pathname obj) pathname)
147               (setf (shared-object-namestring obj)
148                     (native-namestring (translate-logical-pathname pathname)
149                                        :as-file t))))
150          (go :dlopen))))
151   obj)
152
153 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
154 ;;; initialization.
155 (defun reopen-shared-objects ()
156   ;; Ensure that the runtime is open
157   (setf *runtime-dlhandle* (dlopen-or-lose))
158   ;; Reopen stuff.
159   (setf *shared-objects*
160         (remove nil (mapcar #'try-reopen-shared-object *shared-objects*))))
161
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 ()
165   (let (saved)
166     (dolist (obj (reverse *shared-objects*))
167       #!-hpux (dlclose-or-lose obj)
168       (unless (shared-object-dont-save obj)
169         (push obj saved)))
170     (setf *shared-objects* saved))
171   #!-hpux
172   (dlclose-or-lose))
173
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
185              ((not addr)
186               (error 'undefined-alien-error :name symbol))
187              #!+linkage-table
188              ((not addr)
189               (style-warn 'sb!kernel:undefined-alien-style-warning
190                           :symbol symbol)
191               (setf (gethash symbol undefineds) t)
192               (remhash symbol symbols)
193               (if datap
194                   undefined-alien-address
195                   (foreign-symbol-address "undefined_alien_function")))
196              (addr
197               (setf (gethash symbol symbols) t)
198               (remhash symbol undefineds)
199               addr))))
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
206          collect symbol))
207   (defun list-undefined-foreign-symbols ()
208     (loop for symbol being each hash-key in undefineds
209           collect symbol)))