1.0.21.15: LOAD-SHARED-OBJECT :DONT-SAVE and related
[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 #!-win32
29 (progn
30   (define-alien-variable undefined-alien-address unsigned-long)
31   (defvar *runtime-dlhandle*))
32
33 (defvar *shared-objects*)
34
35 (defstruct shared-object pathname namestring handle dont-save)
36
37 (defun load-shared-object (pathname &key dont-save)
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 pathname :as-file t)))))
73         (setf (shared-object-dont-save obj) dont-save)
74         ;; FIXME: Why doesn's dlopen-or-lose on already loaded stuff work on
75         ;; Windows?
76         #!-win32
77         (dlopen-or-lose obj)
78         #!+win32
79         (unless old
80           (dlopen-or-lose obj))
81         (setf *shared-objects* (append (remove obj *shared-objects*)
82                                        (list obj)))
83         ;; FIXME: Why doesn't the linkage table work on Windows? (Or maybe it
84         ;; does and this can be just #!+linkage-table?) Note: remember to change
85         ;; FOREIGN-DEINIT as well then!
86         #!+(and linkage-table (not win32))
87         (when (or old (undefined-foreign-symbols-p))
88           (update-linkage-table))))
89     pathname))
90
91 (defun try-reopen-shared-object (obj)
92   (declare (type shared-object obj))
93   (tagbody :dlopen
94      (restart-case
95          (dlopen-or-lose obj)
96        (continue ()
97          :report "Skip this shared object and continue."
98          ;; By returning NIL the shared object is dropped from the list.
99          (setf (shared-object-handle obj) nil)
100          (return-from try-reopen-shared-object nil))
101        (retry ()
102          :report "Retry loading this shared object."
103          (go :dlopen))
104        (change-pathname ()
105          :report "Specify a different pathname to load the shared object from."
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               (setf (shared-object-pathname obj) pathname
114                     (shared-object-namestring obj) (native-namestring pathname :as-file t))))
115          (go :dlopen))))
116   obj)
117
118 ;;; Open libraries in *SHARED-OBJECTS* and the runtime. Called during
119 ;;; initialization.
120 (defun reopen-shared-objects ()
121   ;; Ensure that the runtime is open
122   #!-win32
123   (setf *runtime-dlhandle* (dlopen-or-lose))
124   ;; Reopen stuff.
125   (setf *shared-objects*
126         (remove nil (mapcar #'try-reopen-shared-object *shared-objects*))))
127
128 ;;; Close all dlopened libraries and clear out sap entries in
129 ;;; *SHARED-OBJECTS*, and drop the ones with DONT-SAVE set.
130 (defun close-shared-objects ()
131   (let (saved)
132     (dolist (obj (reverse *shared-objects*))
133       (dlclose-or-lose obj)
134       (unless (shared-object-dont-save obj)
135         (push obj saved)))
136     (setf *shared-objects* saved))
137   #!-win32
138   (dlclose-or-lose))
139
140 (let ((symbols (make-hash-table :test #'equal))
141       (undefineds (make-hash-table :test #'equal)))
142   (defun ensure-dynamic-foreign-symbol-address (symbol &optional datap)
143     "Returns the address of the foreign symbol as an integer. On linkage-table
144 ports if the symbols isn't found a special guard address is returned instead,
145 accesses to which will result in an UNDEFINED-ALIEN-ERROR. On other ports an
146 error is immediately signalled if the symbol isn't found. The returned address
147 is never in the linkage-table."
148     (declare (ignorable datap))
149     (let ((addr (find-dynamic-foreign-symbol-address symbol)))
150       (cond  #!-(and linkage-table (not win32))
151              ((not addr)
152               (error 'undefined-alien-error :name symbol))
153              #!+(and linkage-table (not win32))
154              ((not addr)
155               (style-warn 'sb!kernel:undefined-alien-style-warning
156                           :symbol symbol)
157               (setf (gethash symbol undefineds) t)
158               (remhash symbol symbols)
159               (if datap
160                   undefined-alien-address
161                   (foreign-symbol-address "undefined_alien_function")))
162              (addr
163               (setf (gethash symbol symbols) t)
164               (remhash symbol undefineds)
165               addr))))
166   (defun undefined-foreign-symbols-p ()
167     (plusp (hash-table-count undefineds)))
168   (defun dynamic-foreign-symbols-p ()
169     (plusp (hash-table-count symbols)))
170   (defun list-dynamic-foreign-symbols ()
171     (loop for symbol being each hash-key in symbols
172          collect symbol)))
173