Merge the DARWIN branch onto the trunk.

I tried using 'svn merge' to do the merge but it did a terrible job and
there were bazillions of conflicts.  So instead I just took the diff between
the branch and trunk  at r10155, applied the diff to the trunk, 'svn add'ed
the added files (no files needed to be 'svn remove'd) and committed.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10156 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0350f0d..2cf5a82 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,3 +27,7 @@
 AM_CFLAGS   += $(AM_FLAG_M3264_PRI)
 AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
 
+if VGCONF_OS_IS_DARWIN
+x86_amd64_features_CFLAGS = $(AM_CFLAGS) -mdynamic-no-pic
+endif
+
diff --git a/tests/arch_test.c b/tests/arch_test.c
index f6206e3..6771f4a 100644
--- a/tests/arch_test.c
+++ b/tests/arch_test.c
@@ -32,10 +32,10 @@
 
 static Bool go(char* arch)
 { 
-#if defined(VGP_x86_linux)
+#if defined(VGP_x86_linux) || defined(VGP_x86_darwin)
    if ( 0 == strcmp( arch, "x86"   ) ) return True;
 
-#elif defined(VGP_amd64_linux)
+#elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin)
    if ( 0 == strcmp( arch, "x86"   ) ) return True;
    if ( 0 == strcmp( arch, "amd64" ) ) return True;
 
diff --git a/tests/asm.h b/tests/asm.h
index 99f9cc5..e2c2524 100644
--- a/tests/asm.h
+++ b/tests/asm.h
@@ -5,7 +5,15 @@
 // general, any symbol named in asm code should be wrapped by VG_SYM.
 
 // This one is for use in inline asm in C files.
+#if defined(VGO_darwin)
+#define VG_SYM(x) "_"#x
+#else
 #define VG_SYM(x) #x
+#endif
 
 // This one is for use in asm files.
+#if defined(VGO_darwin)
+#define VG_SYM_ASM(x) _##x
+#else
 #define VG_SYM_ASM(x) x
+#endif
diff --git a/tests/filter_libc b/tests/filter_libc
index a89fb89..48c443f 100755
--- a/tests/filter_libc
+++ b/tests/filter_libc
@@ -15,8 +15,9 @@
     s/ __GI___/ __/;
     s/ __([a-z]*)_nocancel / $1 /;
 
-    s/\(in \/.*libc.*\)$/(in \/...libc...)/;
-    s/\(within \/.*libc.*\)$/(within \/...libc...)/;
+    # "libSystem*" occurs on Darwin.
+    s/\(in \/.*(libc|libSystem).*\)$/(in \/...libc...)/;
+    s/\(within \/.*(libc|libSystem).*\)$/(within \/...libc...)/;
 
     # Remove the filename -- on some platforms (eg. Linux) it will be in
     # libc, on some (eg. Darwin) it will be in the main executable.
diff --git a/tests/malloc.h b/tests/malloc.h
index 454d2cf..0179b38 100644
--- a/tests/malloc.h
+++ b/tests/malloc.h
@@ -1,7 +1,11 @@
 // Replacement for malloc.h which factors out platform differences.
 
 #include <stdlib.h>
-#include <malloc.h>
+#if defined(VGO_darwin)
+#  include <malloc/malloc.h>
+#else
+#  include <malloc.h>
+#endif
 
 #include <assert.h>
 
@@ -10,7 +14,12 @@
 static void* memalign16(size_t szB)
 {
    void* x;
+#if defined(VGO_darwin)
+   // Darwin lacks memalign, but its malloc is always 16-aligned anyway.
+   x = malloc(szB);
+#else
    x = memalign(16, szB);
+#endif
    assert(x);
    assert(0 == ((16-1) & (unsigned long)x));
    return x;
diff --git a/tests/os_test.c b/tests/os_test.c
index 065dbe5..a65115c 100644
--- a/tests/os_test.c
+++ b/tests/os_test.c
@@ -22,6 +22,7 @@
 char* all_OSes[] = {
    "linux",
    "aix5",
+   "darwin",
    NULL
 };
 
@@ -31,7 +32,10 @@
    if ( 0 == strcmp( OS, "linux" ) ) return True;
 
 #elif defined(VGO_aix5)
-   if ( 0 == strcmp( OS, "aix5"  ) ) return True;
+   if ( 0 == strcmp( OS, "aix5" ) ) return True;
+
+#elif defined(VGO_darwin)
+   if ( 0 == strcmp( OS, "darwin" ) ) return True;
 
 #else
 #  error Unknown OS
diff --git a/tests/platform_test b/tests/platform_test
index e91b9f7..4d57901 100644
--- a/tests/platform_test
+++ b/tests/platform_test
@@ -13,6 +13,7 @@
 all_platforms=
 all_platforms="$all_platforms x86-linux amd64-linux ppc32-linux ppc64-linux"
 all_platforms="$all_platforms ppc32-aix5 ppc64-aix5"
+all_platforms="$all_platforms x86-darwin amd64-darwin"
 
 if [ $# -ne 2 ] ; then
     echo "usage: platform_test <arch-type> <OS-type>"
diff --git a/tests/sys_mman.h b/tests/sys_mman.h
index 862bccc..7ac64d5 100644
--- a/tests/sys_mman.h
+++ b/tests/sys_mman.h
@@ -2,6 +2,10 @@
 
 #include <sys/mman.h>
 
+#if defined(VGO_darwin)
+#  define MAP_ANONYMOUS MAP_ANON
+#endif
+
 
 #include <assert.h>
 #include <unistd.h>
diff --git a/tests/x86_amd64_features.c b/tests/x86_amd64_features.c
index 9d17805..06ebe5d 100644
--- a/tests/x86_amd64_features.c
+++ b/tests/x86_amd64_features.c
@@ -83,7 +83,7 @@
    return 1;                                          // Feature not present.
 }
 
-#else    // defined(VGA_x86)  || defined(VGA_amd64)
+#else
 
 static Bool go(char* cpu)
 {