Index: trunk/base/src/darwintracelib1.0/access.c
===================================================================
--- trunk/base/src/darwintracelib1.0/access.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/access.c	(revision 117751)
@@ -42,5 +42,5 @@
 #include <unistd.h>
 
-int access(const char *path, int amode) {
+int _dt_access(const char *path, int amode) {
 #define access(x, y) syscall(SYS_access, (x), (y))
 	__darwintrace_setup();
@@ -60,2 +60,4 @@
 #undef access
 }
+
+DARWINTRACE_INTERPOSE(_dt_access, access);
Index: trunk/base/src/darwintracelib1.0/close.c
===================================================================
--- trunk/base/src/darwintracelib1.0/close.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/close.c	(revision 117751)
@@ -51,5 +51,5 @@
  * will be set to the FD to be closed when closing should be allowed.
  */
-int close(int fd) {
+int _dt_close(int fd) {
 #define close(x) syscall(SYS_close, (x))
 	__darwintrace_setup();
@@ -67,2 +67,4 @@
 #undef close
 }
+
+DARWINTRACE_INTERPOSE(_dt_close, close);
Index: trunk/base/src/darwintracelib1.0/darwintrace.h
===================================================================
--- trunk/base/src/darwintracelib1.0/darwintrace.h	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/darwintrace.h	(revision 117751)
@@ -43,4 +43,20 @@
 #include <stdbool.h>
 #include <stdio.h>
+
+/**
+ * DARWINTRACE_INTERPOSE: provides a way to override standard library functions
+ * with your own implementations.
+ */
+#ifndef DARWINTRACE_INTERPOSE
+#define DARWINTRACE_INTERPOSE(_replacement, _replacee) \
+__attribute__((used)) static struct { \
+	const void *replacement; \
+	const void *replacee; \
+} _interpose_##_replacee \
+__attribute__((section ("__DATA,__interpose"))) = { \
+	(const void *) (unsigned long) &_replacement, \
+	(const void *) (unsigned long) &_replacee \
+}
+#endif
 
 /**
Index: trunk/base/src/darwintracelib1.0/dup2.c
===================================================================
--- trunk/base/src/darwintracelib1.0/dup2.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/dup2.c	(revision 117751)
@@ -49,5 +49,5 @@
  * FDs are numbered in ascending order.
  */
-int dup2(int filedes, int filedes2) {
+int _dt_dup2(int filedes, int filedes2) {
 #define dup2(x, y) syscall(SYS_dup2, (x), (y))
 	__darwintrace_setup();
@@ -76,2 +76,4 @@
 #undef dup2
 }
+
+DARWINTRACE_INTERPOSE(_dt_dup2, dup2);
Index: trunk/base/src/darwintracelib1.0/mkdir.c
===================================================================
--- trunk/base/src/darwintracelib1.0/mkdir.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/mkdir.c	(revision 117751)
@@ -54,5 +54,5 @@
  * outside the sandbox that already exist.
  */
-int mkdir(const char *path, mode_t mode) {
+int _dt_mkdir(const char *path, mode_t mode) {
 #define mkdir(x,y) syscall(SYS_mkdir, (x), (y))
 #define lstat(x,y) syscall(LSTATSYSNUM, (x), (y))
@@ -79,2 +79,4 @@
 #undef mkdir
 }
+
+DARWINTRACE_INTERPOSE(_dt_mkdir, mkdir);
Index: trunk/base/src/darwintracelib1.0/open.c
===================================================================
--- trunk/base/src/darwintracelib1.0/open.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/open.c	(revision 117751)
@@ -49,5 +49,5 @@
  * when attempting to create a file, i.e., when \c O_CREAT is set.
  */
-int open(const char *path, int flags, ...) {
+int _dt_open(const char *path, int flags, ...) {
 #define open(x,y,z) syscall(SYS_open, (x), (y), (z))
 	__darwintrace_setup();
@@ -71,2 +71,4 @@
 #undef open
 }
+
+DARWINTRACE_INTERPOSE(_dt_open, open);
Index: trunk/base/src/darwintracelib1.0/proc.c
===================================================================
--- trunk/base/src/darwintracelib1.0/proc.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/proc.c	(revision 117751)
@@ -255,5 +255,5 @@
  * using \c check_interpreter.
  */
-int execve(const char *path, char *const argv[], char *const envp[]) {
+int _dt_execve(const char *path, char *const argv[], char *const envp[]) {
 #define execve(x,y,z) syscall(SYS_execve, (x), (y), (z))
 	__darwintrace_setup();
@@ -290,4 +290,6 @@
 }
 
+DARWINTRACE_INTERPOSE(_dt_execve, execve);
+
 #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
 // Let's save some typing work...
@@ -304,9 +306,8 @@
  * using \c check_interpreter.
  */
-int posix_spawn(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *file_actions,
+int _dt_posix_spawn(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *file_actions,
 		const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]) {
 	__darwintrace_setup();
 
-	static posix_spawn_t prev_posix_spawn = NULL;
 	int result = 0;
 
@@ -340,11 +341,7 @@
 			 * either, because that will fail with an invalid argument. Thus,
 			 * we need to call the original posix_spawn from here. */
-			// retrieve the original posix_spawn function
-			if (prev_posix_spawn == NULL) {
-				prev_posix_spawn = (posix_spawn_t) dlsym(RTLD_NEXT, "posix_spawn");
-			}
 			// call the original posix_spawn function, but restore environment
 			char **newenv = restore_env(envp);
-			result = prev_posix_spawn(pid, path, file_actions, attrp, argv, newenv);
+			result = posix_spawn(pid, path, file_actions, attrp, argv, newenv);
 			free(newenv);
 		}
@@ -355,3 +352,5 @@
 	return result;
 }
+
+DARWINTRACE_INTERPOSE(_dt_posix_spawn, posix_spawn);
 #endif
Index: trunk/base/src/darwintracelib1.0/readdir.c
===================================================================
--- trunk/base/src/darwintracelib1.0/readdir.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/readdir.c	(revision 117751)
@@ -67,5 +67,5 @@
 };
 
-size_t __getdirentries64(int fd, void *buf, size_t bufsize, __darwin_off_t *basep) {
+size_t _dt_getdirentries64(int fd, void *buf, size_t bufsize, __darwin_off_t *basep) {
 #define __getdirentries64(w,x,y,z) syscall(SYS_getdirentries64, (w), (x), (y), (z))
 	__darwintrace_setup();
@@ -106,4 +106,8 @@
 }
 
+// __getdirentries64(2) is private API. There's no header for it.
+size_t __getdirentries64(int fd, void *buf, size_t bufsize, __darwin_off_t *basep);
+DARWINTRACE_INTERPOSE(_dt_getdirentries64, __getdirentries64);
+
 #endif /* defined(__DARWIN_64_BIT_INO_T) */
 
@@ -118,5 +122,5 @@
 #pragma pack()
 
-int getdirentries(int fd, char *buf, int nbytes, long *basep) {
+int _dt_getdirentries(int fd, char *buf, int nbytes, long *basep) {
 #define getdirentries(w,x,y,z) syscall(SYS_getdirentries, (w), (x), (y), (z))
 	__darwintrace_setup();
@@ -155,2 +159,5 @@
 #undef getdirentries
 }
+
+int getdirentries(int fd, char *buf, int nbytes, long *basep);
+DARWINTRACE_INTERPOSE(_dt_getdirentries, getdirentries);
Index: trunk/base/src/darwintracelib1.0/readlink.c
===================================================================
--- trunk/base/src/darwintracelib1.0/readlink.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/readlink.c	(revision 117751)
@@ -47,7 +47,7 @@
  */
 #ifdef READLINK_IS_NOT_P1003_1A
-int readlink(const char *path, char *buf, int bufsiz) {
+int _dt_readlink(const char *path, char *buf, int bufsiz) {
 #else
-ssize_t readlink(const char *path, char *buf, size_t bufsiz) {
+ssize_t _dt_readlink(const char *path, char *buf, size_t bufsiz) {
 #endif
 #define readlink(x,y,z) syscall(SYS_readlink, (x), (y), (z))
@@ -70,2 +70,4 @@
 #undef readlink
 }
+
+DARWINTRACE_INTERPOSE(_dt_readlink, readlink);
Index: trunk/base/src/darwintracelib1.0/rename.c
===================================================================
--- trunk/base/src/darwintracelib1.0/rename.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/rename.c	(revision 117751)
@@ -47,5 +47,5 @@
  * sandbox.
  */
-int rename(const char *from, const char *to) {
+int _dt_rename(const char *from, const char *to) {
 #define rename(x,y) syscall(SYS_rename, (x), (y))
 	__darwintrace_setup();
@@ -68,2 +68,4 @@
 #undef rename
 }
+
+DARWINTRACE_INTERPOSE(_dt_rename, rename);
Index: trunk/base/src/darwintracelib1.0/rmdir.c
===================================================================
--- trunk/base/src/darwintracelib1.0/rmdir.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/rmdir.c	(revision 117751)
@@ -47,5 +47,5 @@
  * sandbox.
  */
-int rmdir(const char *path) {
+int _dt_rmdir(const char *path) {
 #define rmdir(x) syscall(SYS_rmdir, (x))
 	__darwintrace_setup();
@@ -65,2 +65,4 @@
 #undef rmdir
 }
+
+DARWINTRACE_INTERPOSE(_dt_rmdir, rmdir);
Index: trunk/base/src/darwintracelib1.0/stat.c
===================================================================
--- trunk/base/src/darwintracelib1.0/stat.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/stat.c	(revision 117751)
@@ -48,5 +48,5 @@
  * sandbox.
  */
-int stat(const char *path, void *sb) {
+int _dt_stat(const char *path, void *sb) {
 #define stat(path, sb) syscall(SYS_stat, path, sb)
 	__darwintrace_setup();
@@ -67,7 +67,12 @@
 }
 
+// We don't include sys/stat.h because it would rewrite all stat function
+// calls, but we need the declaration of stat here.
+int stat(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_stat, stat);
+
 // Don't provide stat64 on systems that have no stat64 syscall
 #ifdef SYS_stat64
-int stat64(const char *path, void *sb) {
+int _dt_stat64(const char *path, void *sb) {
 #define stat64(path, sb) syscall(SYS_stat64, path, sb)
 	__darwintrace_setup();
@@ -87,11 +92,13 @@
 #undef stat64
 }
+int stat64(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_stat64, stat64);
 
-int stat$INODE64(const char *path, void *sb) {
-	return stat64(path, sb);
-}
+int stat$INODE64(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_stat64, stat$INODE64);
+
 #endif /* defined(SYS_stat64) */
 
-int lstat(const char *path, void *sb) {
+int _dt_lstat(const char *path, void *sb) {
 #define lstat(path, sb) syscall(SYS_lstat, path, sb)
 	__darwintrace_setup();
@@ -113,7 +120,10 @@
 }
 
+int lstat(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_lstat, lstat);
+
 // Don't provide lstat64 on systems that have no lstat64 syscall
 #ifdef SYS_lstat64
-int lstat64(const char *path, void *sb) {
+int _dt_lstat64(const char *path, void *sb) {
 #define lstat64(path, sb) syscall(SYS_lstat64, path, sb)
 	__darwintrace_setup();
@@ -135,6 +145,9 @@
 }
 
-int lstat$INODE64(const char *path, void *sb) {
-	return lstat64(path, sb);
-}
+int lstat64(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_lstat64, lstat64);
+
+int lstat$INODE64(const char *path, void *sb);
+DARWINTRACE_INTERPOSE(_dt_lstat64, lstat$INODE64);
+
 #endif /* defined(SYS_lstat64) */
Index: trunk/base/src/darwintracelib1.0/unlink.c
===================================================================
--- trunk/base/src/darwintracelib1.0/unlink.c	(revision 117731)
+++ trunk/base/src/darwintracelib1.0/unlink.c	(revision 117751)
@@ -47,5 +47,5 @@
  * of the sandbox and simulate non-existence of the file instead.
  */
-int unlink(const char *path) {
+int _dt_unlink(const char *path) {
 #define unlink(x) syscall(SYS_unlink, (x))
 	__darwintrace_setup();
@@ -65,2 +65,4 @@
 #undef unlink
 }
+
+DARWINTRACE_INTERPOSE(_dt_unlink, unlink);
Index: trunk/base/src/port1.0/porttrace.tcl
===================================================================
--- trunk/base/src/port1.0/porttrace.tcl	(revision 117731)
+++ trunk/base/src/port1.0/porttrace.tcl	(revision 117751)
@@ -67,5 +67,4 @@
                 set env(DYLD_INSERT_LIBRARIES) ${tracelib_path}
             }
-            set env(DYLD_FORCE_FLAT_NAMESPACE) 1
             set env(DARWINTRACE_LOG) "$trace_fifo"
             # The sandbox is limited to:
@@ -171,5 +170,5 @@
     if {${os.platform} == "darwin"} {
         global env trace_fifo macosx_version
-        foreach var {DYLD_INSERT_LIBRARIES DYLD_FORCE_FLAT_NAMESPACE DARWINTRACE_LOG DARWINTRACE_SANDBOX_BOUNDS} {
+        foreach var {DYLD_INSERT_LIBRARIES DARWINTRACE_LOG} {
             array unset env $var
             if {$macosx_version eq "10.5"} {
