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