1 ;;;; Loading shared object files, Unix 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 (in-package "SB!ALIEN")
14 (define-alien-routine dlopen system-area-pointer
15 (file c-string) (mode int))
17 (define-alien-routine dlclose int
18 (handle system-area-pointer))
20 (define-alien-routine dlerror c-string)
24 #!+openbsd ("os_dlsym" dlsym)
26 (handle system-area-pointer)
29 (defun dlopen-or-lose (&optional (obj nil objp))
31 (dlclose-or-lose obj))
32 (dlerror) ; clear errors
33 (let* ((namestring (and obj (shared-object-namestring obj)))
34 (sap (dlopen namestring (logior rtld-global rtld-now))))
35 (when (zerop (sap-int sap))
37 (setf (shared-object-handle obj) nil)
38 (setf *runtime-dlhandle* nil))
39 (error "Error opening ~:[runtime~;shared object ~:*~S~]:~% ~A."
40 namestring (dlerror)))
42 (setf (shared-object-handle obj) sap))
45 (defun dlclose-or-lose (&optional (obj nil objp))
48 (cond ((and (not objp) *runtime-dlhandle*)
49 (dlclose *runtime-dlhandle*)
50 (setf dlerror (dlerror)
51 *runtime-dlhandle* nil))
52 ((and objp (shared-object-handle obj))
53 (dlclose (shared-object-handle obj))
54 (setf dlerror (dlerror)
55 (shared-object-handle obj) nil)))
57 (cerror "Ignore the error and continue as if closing succeeded."
58 "dlerror() returned an error while trying to close ~
59 ~:[runtime~;shared object ~:*~S~]: ~S"
60 (when obj (shared-object-namestring obj))
63 (defun find-dynamic-foreign-symbol-address (symbol)
64 (dlerror) ; clear old errors
65 (unless *runtime-dlhandle*
66 (bug "Cannot resolve foreign symbol: lost *runtime-dlhandle*"))
67 ;; On real ELF & dlsym platforms the EXTERN-ALIEN-NAME is a no-op,
68 ;; but on platforms where dlsym is simulated we use the mangled name.
69 (let* ((extern (extern-alien-name symbol))
70 (result (sap-int (dlsym *runtime-dlhandle* extern)))
72 (if (or (not (zerop result)) (not err))
74 (dolist (obj *shared-objects*)
75 (let ((sap (shared-object-handle obj)))
77 (setf result (sap-int (dlsym sap extern))
79 (when (or (not (zerop result)) (not err))
80 (return result))))))))