Skip to content

Commit

Permalink
Store linkage table addresses as offsets from the table base
Browse files Browse the repository at this point in the history
If we want to make linkage space relocatable (not to suggest that it's
easy or even possible), then table-relative values will make more sense.

And for detecting overflow, check if the entry being written ends at or
below the space end, not whether it starts strictly below the end,
in case the table size is not an exact multiple of the entry size.
  • Loading branch information
snuglas committed May 11, 2018
1 parent bf47692 commit 7342029
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/code/foreign.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ if the symbol isn't found."
(dovector (symbol *!initial-foreign-symbols*)
(setf (gethash (car symbol) *static-foreign-symbols*) (cdr symbol)))
#!+sb-dynamic-core
(loop for table-address from sb!vm:linkage-table-space-start
by sb!vm::linkage-table-entry-size
and reference across (symbol-value 'sb!vm::+required-foreign-symbols+)
do (setf (gethash reference *linkage-info*) table-address))
(loop for table-offset from 0 by sb!vm::linkage-table-entry-size
and reference across (symbol-value 'sb!vm::+required-foreign-symbols+)
do (setf (gethash reference *linkage-info*) table-offset))
#!+os-provides-dlopen
(setf *runtime-dlhandle* (dlopen-or-lose))
#!+os-provides-dlopen
Expand Down
36 changes: 19 additions & 17 deletions src/code/linkage-table.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@
;;; in the linkage table.
(defun ensure-foreign-symbol-linkage (name datap)
(let ((key (if datap (list name) name))
(table-base sb!vm:linkage-table-space-start)
(ht *linkage-info*))
(or (with-locked-system-table (ht)
(or (gethash key ht)
(let* ((real-address
(ensure-dynamic-foreign-symbol-address name datap))
(table-base sb!vm:linkage-table-space-start)
(table-address
(+ (* (hash-table-count ht) sb!vm:linkage-table-entry-size)
table-base)))
(aver real-address)
(when (< table-address sb!vm:linkage-table-space-end)
(arch-write-linkage-table-entry
table-address real-address (if datap 1 0))
(let ((str (logically-readonlyize name)))
(setf (gethash (if datap (list str) str) ht)
table-address))))))
(or (awhen (with-locked-system-table (ht)
(or (gethash key ht)
(let* ((table-offset
(* (hash-table-count ht) sb!vm:linkage-table-entry-size))
(table-address (+ table-base table-offset)))
(when (<= (+ table-address sb!vm:linkage-table-entry-size)
sb!vm:linkage-table-space-end)
(let ((real-address
(ensure-dynamic-foreign-symbol-address name datap)))
(aver real-address)
(arch-write-linkage-table-entry
table-address real-address (if datap 1 0))
(logically-readonlyize name)
(setf (gethash key ht) table-offset))))))
(+ table-base it))
(error "Linkage-table full (~D entries): cannot link ~S."
(hash-table-count ht) name))))

Expand All @@ -57,8 +58,9 @@
(defun update-linkage-table ()
;; This symbol is of course itself a prelinked symbol.
(let ((n-prelinked (extern-alien "lisp_linkage_table_n_prelinked" int)))
(dohash ((key table-address) *linkage-info* :locked t)
(dohash ((key table-offset) *linkage-info* :locked t)
(let* ((datap (listp key))
(table-address (+ table-offset sb!vm:linkage-table-space-start))
(name (if datap (car key) key))
(index (floor (- table-address sb!vm:linkage-table-space-start)
sb!vm:linkage-table-entry-size)))
Expand All @@ -67,6 +69,6 @@
;; Nor will those referenced by ELF core.
(when (>= index n-prelinked)
(let ((real-address (ensure-dynamic-foreign-symbol-address name datap)))
(aver (and table-address real-address))
(aver real-address)
(arch-write-linkage-table-entry table-address real-address
(if datap 1 0))))))))

0 comments on commit 7342029

Please sign in to comment.