1.0.39.15: delete all traces of the darwin dlshim
[sbcl.git] / src / code / foreign.lisp
1 ;;;; Foreign symbol linkage
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!IMPL")
13
14 #!-(or elf mach-o win32)
15 (error "Not an ELF, Mach-O, or Win32 platform?")
16
17 (defun extern-alien-name (name)
18   (handler-case
19       (coerce name 'base-string)
20     (error ()
21       (error "invalid external alien name: ~S" name))))
22
23 ;;; *STATIC-FOREIGN-SYMBOLS* are static as opposed to "dynamic" (not
24 ;;; as opposed to C's "extern"). The table contains symbols known at
25 ;;; the time that the program was built, but not symbols defined in
26 ;;; object files which have been loaded dynamically since then.
27 (declaim (type hash-table *static-foreign-symbols*))
28 (defvar *static-foreign-symbols* (make-hash-table :test 'equal))
29
30 (declaim
31  (ftype (sfunction (string hash-table) (or integer null)) find-foreign-symbol-in-table))
32 (defun find-foreign-symbol-in-table (name table)
33   (let ((extern (extern-alien-name name)))
34     (values
35      (or (gethash extern table)
36          (gethash (concatenate 'base-string "ldso_stub__" extern) table)))))
37
38 (defun find-foreign-symbol-address (name)
39   "Returns the address of the foreign symbol NAME, or NIL. Does not enter the
40 symbol in the linkage table, and never returns an address in the linkage-table."
41   (or (find-foreign-symbol-in-table name *static-foreign-symbols*)
42       (find-dynamic-foreign-symbol-address name)))
43
44 (defun foreign-symbol-address (name &optional datap)
45   "Returns the address of the foreign symbol NAME. DATAP must be true if the
46 symbol designates a variable (used only on linkage-table platforms). Returns a
47 secondary value that is true if DATAP was true and the symbol is a dynamic
48 foreign symbol.
49
50 On linkage-table ports the returned address is always static: either direct
51 address of a static symbol, or the linkage-table address of a dynamic one.
52 Dynamic symbols are entered into the linkage-table if they aren't there already.
53
54 On non-linkage-table ports signals an error if the symbol isn't found."
55   (declare (ignorable datap))
56   (let ((static (find-foreign-symbol-in-table name  *static-foreign-symbols*)))
57     (if static
58         (values static nil)
59         #!+os-provides-dlopen
60         (progn
61           #-sb-xc-host
62           (values #!-linkage-table
63                   (ensure-dynamic-foreign-symbol-address name)
64                   #!+linkage-table
65                   (ensure-foreign-symbol-linkage name datap)
66                   t)
67           #+sb-xc-host
68           (error 'undefined-alien-error :name name))
69         #!-os-provides-dlopen
70         (error 'undefined-alien-error :name name))))
71
72 (defun foreign-symbol-sap (symbol &optional datap)
73   "Returns a SAP corresponding to the foreign symbol. DATAP must be true if the
74 symbol designates a variable (used only on linkage-table platforms). May enter
75 the symbol into the linkage-table. On non-linkage-table ports signals an error
76 if the symbol isn't found."
77   (declare (ignorable datap))
78   #!-linkage-table
79   (int-sap (foreign-symbol-address symbol))
80   #!+linkage-table
81   (multiple-value-bind (addr sharedp)
82       (foreign-symbol-address symbol datap)
83     #+sb-xc-host
84     (aver (not sharedp))
85     ;; If the address is from linkage-table and refers to data
86     ;; we need to do a bit of juggling. It is not the address of the
87     ;; variable, but the address where the real address is stored.
88     (if (and sharedp datap)
89         (int-sap (sap-ref-word (int-sap addr) 0))
90         (int-sap addr))))
91
92 #-sb-xc-host
93 (defun foreign-reinit ()
94   #!+os-provides-dlopen
95   (reopen-shared-objects)
96   #!+linkage-table
97   ;; Don't warn about undefined aliens on startup. The same core can
98   ;; reasonably be expected to work with different versions of the
99   ;; same library.
100   (handler-bind ((style-warning #'muffle-warning))
101     (update-linkage-table)))
102
103 ;;; Cleanups before saving a core
104 #-sb-xc-host
105 (defun foreign-deinit ()
106   #!+(and os-provides-dlopen (or (not linkage-table) win32))
107   (when (dynamic-foreign-symbols-p)
108     (warn "~@<Saving cores with alien definitions referring to non-static ~
109            foreign symbols is unsupported on this platform: references to ~
110            such foreign symbols from the restarted core will not work. You ~
111            may be able to work around this limitation by reloading all ~
112            foreign definitions and code using them in the restarted core, ~
113            but no guarantees.~%~%Dynamic foreign symbols in this core: ~
114            ~{~A~^, ~}~:@>" (list-dynamic-foreign-symbols)))
115   #!+os-provides-dlopen
116   (close-shared-objects))
117
118 (declaim (maybe-inline sap-foreign-symbol))
119 (defun sap-foreign-symbol (sap)
120   (declare (ignorable sap))
121   #-sb-xc-host
122   (let ((addr (sap-int sap)))
123     (declare (ignorable addr))
124     #!+linkage-table
125     (when (<= sb!vm:linkage-table-space-start
126               addr
127               sb!vm:linkage-table-space-end)
128       (dohash ((name-and-datap info) *linkage-info* :locked t)
129         (let ((table-addr (linkage-info-address info)))
130           (when (and (<= table-addr addr)
131                      (< addr (+ table-addr sb!vm:linkage-table-entry-size)))
132             (return-from sap-foreign-symbol (car name-and-datap))))))
133     #!+os-provides-dladdr
134     (with-alien ((info (struct dl-info
135                                (filename c-string)
136                                (base unsigned)
137                                (symbol c-string)
138                                (symbol-address unsigned)))
139                  (dladdr (function unsigned unsigned (* (struct dl-info)))
140                          :extern "dladdr"))
141       (let ((err (alien-funcall dladdr addr (addr info))))
142         (if (zerop err)
143             nil
144             (slot info 'symbol))))
145     ;; FIXME: Even in the absence of dladdr we could search the
146     ;; static foreign symbols (and *linkage-info*, for that matter).
147     ))
148
149 ;;; How we learn about foreign symbols and dlhandles initially
150 (defvar *!initial-foreign-symbols*)
151
152 #-sb-xc-host
153 (defun !foreign-cold-init ()
154   (dolist (symbol *!initial-foreign-symbols*)
155     (setf (gethash (car symbol) *static-foreign-symbols*) (cdr symbol)))
156   #!+(and os-provides-dlopen (not win32))
157   (setf *runtime-dlhandle* (dlopen-or-lose))
158   #!+os-provides-dlopen
159   (setf *shared-objects* nil))
160
161 #!-os-provides-dlopen
162 (define-unsupported-fun load-shared-object)