1 ;;;; Linkage table specifics
3 ;;;; This software is part of the SBCL system. See the README file for
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.
12 ;;;; Linkage table itself is a mmapped memory area in C-land, which is
13 ;;;; initialized by INIT-LINKAGE-TABLE once all shared objects have
14 ;;;; been reopened, based on the information stored in *LINKAGE-INFO*.
16 ;;;; For data entries the linkage table holds the real address
17 ;;;; of the foreign symbol, and for code the entries are jumps
18 ;;;; to the real addresses.
20 (in-package "SB!IMPL")
22 (defvar *foreign-lock*) ; initialized in foreign-load.lisp
24 (define-alien-routine arch-write-linkage-table-jmp void
25 (table-address system-area-pointer)
26 (real-address system-area-pointer))
28 (define-alien-routine arch-write-linkage-table-ref void
29 (table-address system-area-pointer)
30 (real-address system-area-pointer))
32 (defvar *linkage-info* (make-hash-table :test 'equal))
34 (defstruct linkage-info datap address)
36 (defun write-linkage-table-entry (table-address real-address datap)
37 (/show0 "write-linkage-table-entry")
38 (let ((reloc (int-sap table-address))
39 (target (int-sap real-address)))
41 (arch-write-linkage-table-ref reloc target)
42 (arch-write-linkage-table-jmp reloc target))))
44 ;;; Add the linkage information about a foreign symbol in the
45 ;;; persistent table, and write the linkage-table entry.
46 (defun link-foreign-symbol (name datap)
47 (/show0 "link-foreign-symbol")
48 (let ((table-address (+ (* (hash-table-count *linkage-info*)
49 sb!vm:linkage-table-entry-size)
50 sb!vm:linkage-table-space-start))
51 (real-address (ensure-dynamic-foreign-symbol-address name datap)))
53 (unless (< table-address sb!vm:linkage-table-space-end)
54 (error "Linkage-table full (~D entries): cannot link ~S."
55 (hash-table-count *linkage-info*)
57 (write-linkage-table-entry table-address real-address datap)
58 (setf (gethash (cons name datap) *linkage-info*)
59 (make-linkage-info :address table-address :datap datap))))
61 ;;; Add a foreign linkage entry if none exists, return the address
62 ;;; in the linkage table.
63 (defun ensure-foreign-symbol-linkage (name datap)
64 (/show0 "ensure-foreign-symbol-linkage")
65 (sb!thread:with-mutex (*foreign-lock*)
66 (let ((info (or (gethash (cons name datap) *linkage-info*)
67 (link-foreign-symbol name datap))))
68 (linkage-info-address info))))
70 ;;; Update the linkage-table. Called during initialization after all
71 ;;; shared libraries have been reopened, and after a previously loaded
72 ;;; shared object is reloaded.
73 (defun update-linkage-table ()
74 ;; Doesn't take care of its own locking -- callers are responsible
75 (maphash (lambda (name-and-datap info)
76 (let* ((name (car name-and-datap))
77 (datap (cdr name-and-datap))
78 (table-address (linkage-info-address info))
80 (ensure-dynamic-foreign-symbol-address name datap)))
81 (aver (and table-address real-address))
82 (write-linkage-table-entry table-address