Index: /trunk/ccl/level-0/l0-cfm-support.lisp
===================================================================
--- /trunk/ccl/level-0/l0-cfm-support.lisp	(revision 4822)
+++ /trunk/ccl/level-0/l0-cfm-support.lisp	(revision 4823)
@@ -27,6 +27,6 @@
 
 
-; Bootstrapping. Real version is in l1-aprims.
-; Called by expansion of with-pstrs
+;;; Bootstrapping. Real version is in l1-aprims.
+;;; Called by expansion of with-pstrs
 
 (defun byte-length (string &optional script start end)
@@ -78,7 +78,8 @@
 (defvar *rtld-default*)
 (setq *rtld-next* (%incf-ptr (%null-ptr) -1)
-      *rtld-default* (%int-to-ptr 0))
-
-#+linux-target
+      *rtld-default* (%int-to-ptr #+linux-target 0
+				  #-linux-target -2))
+
+#+(or linux-target freebsd-target)
 (progn
 
@@ -129,5 +130,5 @@
 (defvar *shared-libraries* nil)
 
-#+linux-target
+#+(or linux-target freebsd-target)
 (progn
 
@@ -136,9 +137,9 @@
 		 (dynamic-entries (pref map :link_map.l_ld)))
     (let* ((soname-offset nil))
-      ;;; Walk over the entries in the file's dynamic segment;
-      ;;; the last such entry will have a tag of #$DT_NULL.
-      ;;; Note the (loaded) address of the dynamic string table
-      ;;; and the offset of the #$DT_SONAME string in that string
-      ;;; table.
+      ;;; Walk over the entries in the file's dynamic segment; the
+      ;;; last such entry will have a tag of #$DT_NULL.  Note the
+      ;;; (loaded,on Linux; relative to link_map.l_addr on FreeBSD)
+      ;;; address of the dynamic string table and the offset of the
+      ;;; #$DT_SONAME string in that string table.
       (loop
 	  (case #+32-bit-target (pref dynamic-entries :<E>lf32_<D>yn.d_tag)
@@ -157,6 +158,11 @@
 				    :<E>lf32_<D>yn.d_un.d_ptr)
                               #+64-bit-target
+			      #+linux-target
                               (pref dynamic-entries
-				    :<E>lf64_<D>yn.d_un.d_ptr))))
+				    :<E>lf64_<D>yn.d_un.d_ptr)
+			      #+freebsd-target
+			      (%inc-ptr (pref map :link_map.l_addr)
+					(pref dynamic-entries
+					      :<E>lf64_<D>yn.d_un.d_val)))))
 	  (%setf-macptr dynamic-entries
 			(%inc-ptr dynamic-entries
@@ -214,6 +220,12 @@
 
 
+(defun %get-r-debug ()
+  (let* ((addr (ff-call (%kernel-import target::kernel-import-get-r-debug)
+			address)))
+    (unless (%null-ptr-p addr)
+      addr)))
+
 (defun %link-map-address ()
-  (let* ((r_debug (foreign-symbol-address "_r_debug")))
+  (let* ((r_debug (%get-r-debug)))
     (if r_debug
       (pref r_debug :r_debug.r_map)
@@ -238,4 +250,5 @@
   
 (defun init-shared-libraries ()
+  #+freebsd-target (dbg)
   (setq *dladdr-entry* (foreign-symbol-entry "dladdr"))
   (when (null *shared-libraries*)
@@ -244,4 +257,5 @@
       ;;; It seems to be necessary to open each of these libraries
       ;;; yet again, specifying the RTLD_GLOBAL flag.
+      #+linux-target
       (%dlopen-shlib l)
       (setf (shlib.opened-by-lisp-kernel l) t))))
@@ -466,5 +480,5 @@
 (defvar *statically-linked* nil)
 
-#+linux-target
+#+(or linux-target freebsd-target)
 (progn
 
@@ -520,6 +534,8 @@
 ;;; if not found in the OS list it returns nil
 ;;;
-;;; got this error before putting in the call to NSIsObjectNameDefinedInImage
-;;; dyld: /usr/local/lisp/ccl/dppccl dead lock (dyld operation attempted in a thread already doing a dyld operation)
+;;; got this error before putting in the call to
+;;; NSIsObjectNameDefinedInImage dyld: /usr/local/lisp/ccl/dppccl dead
+;;; lock (dyld operation attempted in a thread already doing a dyld
+;;; operation)
 ;;;
 
@@ -570,5 +586,5 @@
 )
 
-#-(or linux-target darwinppc-target)
+#-(or linux-target darwinppc-target freebsd-target)
 (defun shlib-containing-entry (entry &optional name)
   (declare (ignore entry name))
