X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Fforeign.lisp;h=f6101db16f56ed471609fd43966332b91be72911;hb=c716f6ea5255afeb33a1181535b5c067aa9d6aaa;hp=ee6029568652a74786c4c2af664a36d154af95b9;hpb=5108495b13b99452d5a85c4600f68432ff8894b2;p=sbcl.git diff --git a/src/code/foreign.lisp b/src/code/foreign.lisp index ee60295..f6101db 100644 --- a/src/code/foreign.lisp +++ b/src/code/foreign.lisp @@ -1,4 +1,5 @@ -;;;; support for dynamically loading foreign object files +;;;; support for dynamically loading foreign object files and +;;;; resolving symbols therein ;;;; This software is part of the SBCL system. See the README file for ;;;; more information. @@ -9,7 +10,7 @@ ;;;; provided with absolutely no warranty. See the COPYING and CREDITS ;;;; files for more information. -(in-package "SB-SYS") +(in-package "SB-ALIEN") ; (SB-ALIEN, not SB!ALIEN, since we're in warm load.) (defun pick-temporary-file-name (&optional ;; KLUDGE: There are various security @@ -52,13 +53,27 @@ ;;; (On any OS which *does* support foreign object file loading, this ;;; placeholder implementation is overwritten by a subsequent real ;;; implementation.) +;;; +;;; You may want to use SB-SYS:FOREIGN-SYMBOL-ADDRESS instead of +;;; calling this directly; see code/target-load.lisp. (defun get-dynamic-foreign-symbol-address (symbol) (declare (type simple-string symbol) (ignore symbol)) nil) -;;; Linux implementation of GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS -;;; and functions (e.g. LOAD-FOREIGN) which affect it -#+(or linux FreeBSD) +;;; dlsym()-based implementation of GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS +;;; and functions (e.g. LOAD-FOREIGN) which affect it. This should +;;; work on any ELF system with dlopen(3) and dlsym(3) +;;; It also works on OpenBSD, which isn't ELF, but is otherwise modern +;;; enough to have a fairly well working dlopen/dlsym implementation. +#-(or linux FreeBSD OpenBSD) +(macrolet ((define-unsupported-fun (fun-name) + `(defun ,fun-name (&rest rest) + "unsupported on this system" + (declare (ignore rest)) + (error 'unsupported-operator :name ',fun-name)))) + (define-unsupported-fun load-1-foreign) + (define-unsupported-fun load-foreign)) +#+(or linux FreeBSD OpenBSD) (progn ;;; flags for dlopen() @@ -70,15 +85,16 @@ ; obj file were linked directly ; into the program)? -;;; a list of tables returned from dlopen(3) (or possibly some +;;; a list of handles returned from dlopen(3) (or possibly some ;;; bogus value temporarily during initialization) -(defvar *tables-from-dlopen* nil) +(defvar *handles-from-dlopen* nil) + ;;; Dynamically loaded stuff isn't there upon restoring from a save. ;;; Clearing the variable this way was originally done primarily for ;;; Irix, which resolves tzname at runtime, resulting in -;;; *TABLES-FROM-DLOPEN* being set in the saved core image, resulting -;;; in havoc upon restart; but it seems harmless and tidy for other -;;; OSes too. +;;; *HANDLES-FROM-DLOPEN* (which was then called *TABLES-FROM-DLOPEN*) +;;; being set in the saved core image, resulting in havoc upon +;;; restart; but it seems harmless and tidy for other OSes too. ;;; ;;; Of course, it can be inconvenient that dynamically loaded stuff ;;; goes away when we save and restore. However, @@ -91,35 +107,44 @@ ;;; dynamic loading of foreign files and saving/restoring cores, ;;; he probably has the sophistication to write his own after-save ;;; code to reload the libraries without much difficulty. -(push (lambda () (setq *tables-from-dlopen* nil)) + +;;; dan 2001.05.10 suspects that objection (1) is bogus for +;;; dlsym()-enabled systems + +(push (lambda () (setq *handles-from-dlopen* nil)) *after-save-initializations*) (defvar *dso-linker* "/usr/bin/ld") -(defvar *dso-linker-options* '("-G" "-o")) +(defvar *dso-linker-options* '("-shared" "-o")) -(sb-alien:def-alien-routine dlopen system-area-pointer - (file sb-c-call:c-string) (mode sb-c-call:int)) -(sb-alien:def-alien-routine dlsym system-area-pointer +(sb-alien:define-alien-routine dlopen system-area-pointer + (file sb-alien:c-string) (mode sb-alien:int)) +(sb-alien:define-alien-routine dlsym system-area-pointer (lib system-area-pointer) - (name sb-c-call:c-string)) -(sb-alien:def-alien-routine dlerror sb-c-call:c-string) - -;;; Ensure that we've opened our own binary so we can resolve global -;;; variables in the Lisp image that come from libraries. This used to -;;; happen only in GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS, and only if no -;;; libraries were dlopen()ed already, but that didn't work if -;;; something was dlopen()ed before any problem global vars were used. -;;; So now we do this in any function that can add to the -;;; *TABLES-FROM-DLOPEN*, as well as in -;;; GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS. -(defun ensure-lisp-table-opened () - (unless *tables-from-dlopen* + (name sb-alien:c-string)) +(sb-alien:define-alien-routine dlerror sb-alien:c-string) + +;;; Ensure that we've opened our own binary so we can dynamically resolve +;;; symbols in the C runtime. + +;;; Old comment: This used to happen only in +;;; GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS, and only if no libraries were +;;; dlopen()ed already, but that didn't work if something was +;;; dlopen()ed before any problem global vars were used. So now we do +;;; this in any function that can add to the *HANDLES-FROM-DLOPEN*, as +;;; well as in GET-DYNAMIC-FOREIGN-SYMBOL-ADDRESS. + +;;; FIXME: It would work just as well to do it once at startup, actually. +;;; Then at least we know it's done. -dan 2001.05.10 + +(defun ensure-runtime-symbol-table-opened () + (unless *handles-from-dlopen* ;; Prevent recursive call if dlopen() isn't defined. - (setf *tables-from-dlopen* (int-sap 0)) - (setf *tables-from-dlopen* (list (dlopen nil rtld-lazy))) - (when (zerop (sb-sys:sap-int (first *tables-from-dlopen*))) - (error "can't open global symbol table: ~S" (dlerror))))) + (setf *handles-from-dlopen* (int-sap 0)) + (setf *handles-from-dlopen* (list (dlopen nil rtld-lazy))) + (when (zerop (sb-sys:sap-int (first *handles-from-dlopen*))) + (error "can't open our own binary's symbol table: ~S" (dlerror))))) (defun load-1-foreign (file) "the primitive upon which the more general LOAD-FOREIGN is built: load @@ -131,10 +156,10 @@ ld -shared -o /tmp/ffi-test.so /tmp/ffi-test.o then in SBCL do this: (LOAD-1-FOREIGN \"/tmp/ffi-test.so\") - (DEF-ALIEN-ROUTINE SUMMISH INT (X INT) (Y INT)) + (DEFINE-ALIEN-ROUTINE SUMMISH INT (X INT) (Y INT)) Now running (SUMMISH 10 20) should return 31. " - (ensure-lisp-table-opened) + (ensure-runtime-symbol-table-opened) ;; Note: We use RTLD-GLOBAL so that it can find all the symbols ;; previously loaded. We use RTLD-NOW so that dlopen() will fail if ;; not all symbols are defined. @@ -142,11 +167,11 @@ (sap (dlopen real-file (logior rtld-now rtld-global)))) (if (zerop (sap-int sap)) (error "can't open object ~S: ~S" real-file (dlerror)) - (pushnew sap *tables-from-dlopen* :test #'sap=))) + (pushnew sap *handles-from-dlopen* :test #'sap=))) (values)) (defun get-dynamic-foreign-symbol-address (symbol) - (ensure-lisp-table-opened) + (ensure-runtime-symbol-table-opened) ;; Find the symbol in any of the loaded object files. Search in ;; reverse order of loading, so that later loadings take precedence. ;; @@ -154,11 +179,11 @@ ;; that the list isn't guaranteed to be in reverse order of loading, ;; at least not if a file is loaded more than once. Is this the ;; right thing? (In what cases does it matter?) - (dolist (table *tables-from-dlopen*) + (dolist (handle *handles-from-dlopen*) ;; KLUDGE: We implicitly exclude the possibility that the variable ;; could actually be NULL, but the man page for dlsym(3) ;; recommends doing a more careful test. -- WHN 20000825 - (let ((possible-result (sap-int (dlsym table symbol)))) + (let ((possible-result (sap-int (dlsym handle symbol)))) (unless (zerop possible-result) (return possible-result)))))