merge from open-source master

Change-Id: Iafcb89bdab1495c869569f7063e6c775898dedfc
diff --git a/Android.mk b/Android.mk
index dd70dc8..2352581 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,43 +1,47 @@
 # Copyright 2007 The Android Open Source Project
 #
-# The libffi code is organized primarily by architecture, but at some
-# point OS-specific issues started to creep in.  In some cases there are
-# OS-specific source files, in others there are just #ifdefs in the code.
-# We need to generate the appropriate defines and select the right set of
-# source files for the OS and architecture.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
 
-ifneq ($(TARGET_ARCH),arm)
+# This makefile builds both for host and target, and so all the
+# common definitions are factored out into a separate file to
+# minimize duplication between the build rules.
 
 LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
 
-LOCAL_C_INCLUDES := \
-	external/libffi/include \
-	external/libffi/$(TARGET_OS)-$(TARGET_ARCH)
 
-LOCAL_SRC_FILES := src/debug.c src/prep_cif.c src/types.c \
-        src/raw_api.c src/java_raw_api.c
+#
+# Build rules for the target.
+#
 
-ifeq ($(TARGET_OS)-$(TARGET_ARCH),linux-arm)
-  LOCAL_SRC_FILES += src/arm/sysv.S src/arm/ffi.c
-endif
-ifeq ($(TARGET_OS)-$(TARGET_ARCH),linux-x86)
-  LOCAL_SRC_FILES += src/x86/ffi.c src/x86/sysv.S
-endif
-ifeq ($(TARGET_OS)-$(TARGET_ARCH),linux-sh)
-  LOCAL_SRC_FILES += src/sh/ffi.c src/sh/sysv.S
+# We only build ffi at all for non-arm, non-x86 targets.
+ifneq ($(TARGET_ARCH),arm)
+    ifneq ($(TARGET_ARCH),x86)
+
+       include $(CLEAR_VARS)
+
+       ffi_arch := $(TARGET_ARCH)
+       ffi_os := $(TARGET_OS)
+
+       # This include just keeps the nesting a bit saner.
+       include $(LOCAL_PATH)/Libffi.mk
+
+       LOCAL_MODULE := libffi
+
+       include $(BUILD_SHARED_LIBRARY)
+
+    endif
 endif
 
-ifeq ($(LOCAL_SRC_FILES),)
-  LOCAL_SRC_FILES := your-architecture-not-supported-by-ffi-makefile.c
-endif
-
-LOCAL_MODULE := libffi
-
-
-include $(BUILD_SHARED_LIBRARY)
-
-
+# Also include the rules for the test suite.
 include external/libffi/testsuite/Android.mk
 
-endif
diff --git a/CleanSpec.mk b/CleanSpec.mk
new file mode 100644
index 0000000..b84e1b6
--- /dev/null
+++ b/CleanSpec.mk
@@ -0,0 +1,49 @@
+# Copyright (C) 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list.  These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+#     $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+#     $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list.  E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
diff --git a/Libffi.mk b/Libffi.mk
new file mode 100644
index 0000000..b2d6a32
--- /dev/null
+++ b/Libffi.mk
@@ -0,0 +1,51 @@
+# Copyright 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# The libffi code is organized primarily by architecture, but at some
+# point OS-specific issues started to creep in. In some cases there
+# are OS-specific source files, in others there are just #ifdefs in
+# the code. We need to generate the appropriate defines and select the
+# right set of source files for the OS and architecture.
+
+LOCAL_C_INCLUDES := \
+	external/libffi/include \
+	external/libffi/$(ffi_os)-$(ffi_arch)
+
+ifeq ($(ffi_os)-$(ffi_arch),linux-arm)
+  LOCAL_SRC_FILES := src/arm/sysv.S src/arm/ffi.c
+endif
+
+ifeq ($(ffi_os)-$(ffi_arch),linux-x86)
+  LOCAL_SRC_FILES := src/x86/ffi.c src/x86/sysv.S
+endif
+
+ifeq ($(ffi_os)-$(ffi_arch),linux-sh)
+  LOCAL_SRC_FILES := src/sh/ffi.c src/sh/sysv.S
+endif
+
+ifeq ($(ffi_os)-$(ffi_arch),darwin-x86)
+  LOCAL_SRC_FILES := src/x86/ffi.c src/x86/darwin.S
+endif
+
+ifeq ($(LOCAL_SRC_FILES),)
+  $(info The os/architecture $(ffi_os)-$(ffi_arch) is not supported by libffi.)
+  LOCAL_SRC_FILES := your-architecture-not-supported-by-ffi-makefile.c
+endif
+
+LOCAL_SRC_FILES += \
+	src/debug.c \
+	src/java_raw_api.c \
+	src/prep_cif.c \
+        src/raw_api.c \
+	src/types.c
diff --git a/darwin-x86/ffi.h b/darwin-x86/ffi.h
new file mode 100644
index 0000000..4aab31a
--- /dev/null
+++ b/darwin-x86/ffi.h
@@ -0,0 +1,393 @@
+/* -----------------------------------------------------------------*-C-*-
+   libffi 3.0.6 - Copyright (c) 1996-2003, 2007, 2008  Red Hat, Inc.
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+   DEALINGS IN THE SOFTWARE.
+
+   ----------------------------------------------------------------------- */
+
+/* -------------------------------------------------------------------
+   The basic API is described in the README file.
+
+   The raw API is designed to bypass some of the argument packing
+   and unpacking on architectures for which it can be avoided.
+
+   The closure API allows interpreted functions to be packaged up
+   inside a C function pointer, so that they can be called as C functions,
+   with no understanding on the client side that they are interpreted.
+   It can also be used in other cases in which it is necessary to package
+   up a user specified parameter and a function pointer as a single
+   function pointer.
+
+   The closure API must be implemented in order to get its functionality,
+   e.g. for use by gij.  Routines are provided to emulate the raw API
+   if the underlying platform doesn't allow faster implementation.
+
+   More details on the raw and cloure API can be found in:
+
+   http://gcc.gnu.org/ml/java/1999-q3/msg00138.html
+
+   and
+
+   http://gcc.gnu.org/ml/java/1999-q3/msg00174.html
+   -------------------------------------------------------------------- */
+
+#ifndef LIBFFI_H
+#define LIBFFI_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Specify which architecture libffi is configured for. */
+#ifndef X86_DARWIN
+#define X86_DARWIN
+#endif
+
+/* ---- System configuration information --------------------------------- */
+
+#include <ffitarget.h>
+
+#ifndef LIBFFI_ASM
+
+#include <stddef.h>
+#include <limits.h>
+
+/* LONG_LONG_MAX is not always defined (not if STRICT_ANSI, for example).
+   But we can find it either under the correct ANSI name, or under GNU
+   C's internal name.  */
+#ifdef LONG_LONG_MAX
+# define FFI_LONG_LONG_MAX LONG_LONG_MAX
+#else
+# ifdef LLONG_MAX
+#  define FFI_LONG_LONG_MAX LLONG_MAX
+# else
+#  ifdef __GNUC__
+#   define FFI_LONG_LONG_MAX __LONG_LONG_MAX__
+#  endif
+# endif
+#endif
+
+/* The closure code assumes that this works on pointers, i.e. a size_t	*/
+/* can hold a pointer.							*/
+
+typedef struct _ffi_type
+{
+  size_t size;
+  unsigned short alignment;
+  unsigned short type;
+  struct _ffi_type **elements;
+} ffi_type;
+
+#ifndef LIBFFI_HIDE_BASIC_TYPES
+#if SCHAR_MAX == 127
+# define ffi_type_uchar                ffi_type_uint8
+# define ffi_type_schar                ffi_type_sint8
+#else
+ #error "char size not supported"
+#endif
+
+#if SHRT_MAX == 32767
+# define ffi_type_ushort       ffi_type_uint16
+# define ffi_type_sshort       ffi_type_sint16
+#elif SHRT_MAX == 2147483647
+# define ffi_type_ushort       ffi_type_uint32
+# define ffi_type_sshort       ffi_type_sint32
+#else
+ #error "short size not supported"
+#endif
+
+#if INT_MAX == 32767
+# define ffi_type_uint         ffi_type_uint16
+# define ffi_type_sint         ffi_type_sint16
+#elif INT_MAX == 2147483647
+# define ffi_type_uint         ffi_type_uint32
+# define ffi_type_sint         ffi_type_sint32
+#elif INT_MAX == 9223372036854775807
+# define ffi_type_uint         ffi_type_uint64
+# define ffi_type_sint         ffi_type_sint64
+#else
+ #error "int size not supported"
+#endif
+
+#if LONG_MAX == 2147483647
+# if FFI_LONG_LONG_MAX != 9223372036854775807
+ #error "no 64-bit data type supported"
+# endif
+#elif LONG_MAX != 9223372036854775807
+ #error "long size not supported"
+#endif
+
+#if LONG_MAX == 2147483647
+# define ffi_type_ulong        ffi_type_uint32
+# define ffi_type_slong        ffi_type_sint32
+#elif LONG_MAX == 9223372036854775807
+# define ffi_type_ulong        ffi_type_uint64
+# define ffi_type_slong        ffi_type_sint64
+#else
+ #error "long size not supported"
+#endif
+
+/* These are defined in types.c */
+extern ffi_type ffi_type_void;
+extern ffi_type ffi_type_uint8;
+extern ffi_type ffi_type_sint8;
+extern ffi_type ffi_type_uint16;
+extern ffi_type ffi_type_sint16;
+extern ffi_type ffi_type_uint32;
+extern ffi_type ffi_type_sint32;
+extern ffi_type ffi_type_uint64;
+extern ffi_type ffi_type_sint64;
+extern ffi_type ffi_type_float;
+extern ffi_type ffi_type_double;
+extern ffi_type ffi_type_pointer;
+
+#if 1
+extern ffi_type ffi_type_longdouble;
+#else
+#define ffi_type_longdouble ffi_type_double
+#endif
+#endif /* LIBFFI_HIDE_BASIC_TYPES */
+
+typedef enum {
+  FFI_OK = 0,
+  FFI_BAD_TYPEDEF,
+  FFI_BAD_ABI
+} ffi_status;
+
+typedef unsigned FFI_TYPE;
+
+typedef struct {
+  ffi_abi abi;
+  unsigned nargs;
+  ffi_type **arg_types;
+  ffi_type *rtype;
+  unsigned bytes;
+  unsigned flags;
+#ifdef FFI_EXTRA_CIF_FIELDS
+  FFI_EXTRA_CIF_FIELDS;
+#endif
+} ffi_cif;
+
+/* ---- Definitions for the raw API -------------------------------------- */
+
+#ifndef FFI_SIZEOF_ARG
+# if LONG_MAX == 2147483647
+#  define FFI_SIZEOF_ARG        4
+# elif LONG_MAX == 9223372036854775807
+#  define FFI_SIZEOF_ARG        8
+# endif
+#endif
+
+#ifndef FFI_SIZEOF_JAVA_RAW
+#  define FFI_SIZEOF_JAVA_RAW FFI_SIZEOF_ARG
+#endif
+
+typedef union {
+  ffi_sarg  sint;
+  ffi_arg   uint;
+  float	    flt;
+  char      data[FFI_SIZEOF_ARG];
+  void*     ptr;
+} ffi_raw;
+
+#if FFI_SIZEOF_JAVA_RAW == 4 && FFI_SIZEOF_ARG == 8
+/* This is a special case for mips64/n32 ABI (and perhaps others) where
+   sizeof(void *) is 4 and FFI_SIZEOF_ARG is 8.  */
+typedef union {
+  signed int	sint;
+  unsigned int	uint;
+  float		flt;
+  char		data[FFI_SIZEOF_JAVA_RAW];
+  void*		ptr;
+} ffi_java_raw;
+#else
+typedef ffi_raw ffi_java_raw;
+#endif
+
+
+void ffi_raw_call (ffi_cif *cif,
+		   void (*fn)(void),
+		   void *rvalue,
+		   ffi_raw *avalue);
+
+void ffi_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_raw *raw);
+void ffi_raw_to_ptrarray (ffi_cif *cif, ffi_raw *raw, void **args);
+size_t ffi_raw_size (ffi_cif *cif);
+
+/* This is analogous to the raw API, except it uses Java parameter	*/
+/* packing, even on 64-bit machines.  I.e. on 64-bit machines		*/
+/* longs and doubles are followed by an empty 64-bit word.		*/
+
+void ffi_java_raw_call (ffi_cif *cif,
+			void (*fn)(void),
+			void *rvalue,
+			ffi_java_raw *avalue);
+
+void ffi_java_ptrarray_to_raw (ffi_cif *cif, void **args, ffi_java_raw *raw);
+void ffi_java_raw_to_ptrarray (ffi_cif *cif, ffi_java_raw *raw, void **args);
+size_t ffi_java_raw_size (ffi_cif *cif);
+
+/* ---- Definitions for closures ----------------------------------------- */
+
+#if FFI_CLOSURES
+
+typedef struct {
+  char tramp[FFI_TRAMPOLINE_SIZE];
+  ffi_cif   *cif;
+  void     (*fun)(ffi_cif*,void*,void**,void*);
+  void      *user_data;
+} ffi_closure __attribute__((aligned (8)));
+
+void *ffi_closure_alloc (size_t size, void **code);
+void ffi_closure_free (void *);
+
+ffi_status
+ffi_prep_closure (ffi_closure*,
+		  ffi_cif *,
+		  void (*fun)(ffi_cif*,void*,void**,void*),
+		  void *user_data);
+
+ffi_status
+ffi_prep_closure_loc (ffi_closure*,
+		      ffi_cif *,
+		      void (*fun)(ffi_cif*,void*,void**,void*),
+		      void *user_data,
+		      void*codeloc);
+
+typedef struct {
+  char tramp[FFI_TRAMPOLINE_SIZE];
+
+  ffi_cif   *cif;
+
+#if !FFI_NATIVE_RAW_API
+
+  /* if this is enabled, then a raw closure has the same layout 
+     as a regular closure.  We use this to install an intermediate 
+     handler to do the transaltion, void** -> ffi_raw*. */
+
+  void     (*translate_args)(ffi_cif*,void*,void**,void*);
+  void      *this_closure;
+
+#endif
+
+  void     (*fun)(ffi_cif*,void*,ffi_raw*,void*);
+  void      *user_data;
+
+} ffi_raw_closure;
+
+typedef struct {
+  char tramp[FFI_TRAMPOLINE_SIZE];
+
+  ffi_cif   *cif;
+
+#if !FFI_NATIVE_RAW_API
+
+  /* if this is enabled, then a raw closure has the same layout 
+     as a regular closure.  We use this to install an intermediate 
+     handler to do the transaltion, void** -> ffi_raw*. */
+
+  void     (*translate_args)(ffi_cif*,void*,void**,void*);
+  void      *this_closure;
+
+#endif
+
+  void     (*fun)(ffi_cif*,void*,ffi_java_raw*,void*);
+  void      *user_data;
+
+} ffi_java_raw_closure;
+
+ffi_status
+ffi_prep_raw_closure (ffi_raw_closure*,
+		      ffi_cif *cif,
+		      void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
+		      void *user_data);
+
+ffi_status
+ffi_prep_raw_closure_loc (ffi_raw_closure*,
+			  ffi_cif *cif,
+			  void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
+			  void *user_data,
+			  void *codeloc);
+
+ffi_status
+ffi_prep_java_raw_closure (ffi_java_raw_closure*,
+		           ffi_cif *cif,
+		           void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
+		           void *user_data);
+
+ffi_status
+ffi_prep_java_raw_closure_loc (ffi_java_raw_closure*,
+			       ffi_cif *cif,
+			       void (*fun)(ffi_cif*,void*,ffi_java_raw*,void*),
+			       void *user_data,
+			       void *codeloc);
+
+#endif /* FFI_CLOSURES */
+
+/* ---- Public interface definition -------------------------------------- */
+
+ffi_status ffi_prep_cif(ffi_cif *cif,
+			ffi_abi abi,
+			unsigned int nargs,
+			ffi_type *rtype,
+			ffi_type **atypes);
+
+void ffi_call(ffi_cif *cif,
+	      void (*fn)(void),
+	      void *rvalue,
+	      void **avalue);
+
+/* Useful for eliminating compiler warnings */
+#define FFI_FN(f) ((void (*)(void))f)
+
+/* ---- Definitions shared with assembly code ---------------------------- */
+
+#endif
+
+/* If these change, update src/mips/ffitarget.h. */
+#define FFI_TYPE_VOID       0    
+#define FFI_TYPE_INT        1
+#define FFI_TYPE_FLOAT      2    
+#define FFI_TYPE_DOUBLE     3
+#if 1
+#define FFI_TYPE_LONGDOUBLE 4
+#else
+#define FFI_TYPE_LONGDOUBLE FFI_TYPE_DOUBLE
+#endif
+#define FFI_TYPE_UINT8      5   
+#define FFI_TYPE_SINT8      6
+#define FFI_TYPE_UINT16     7 
+#define FFI_TYPE_SINT16     8
+#define FFI_TYPE_UINT32     9
+#define FFI_TYPE_SINT32     10
+#define FFI_TYPE_UINT64     11
+#define FFI_TYPE_SINT64     12
+#define FFI_TYPE_STRUCT     13
+#define FFI_TYPE_POINTER    14
+
+/* This should always refer to the last type code (for sanity checks) */
+#define FFI_TYPE_LAST       FFI_TYPE_POINTER
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/darwin-x86/fficonfig.h b/darwin-x86/fficonfig.h
new file mode 100644
index 0000000..12f95e3
--- /dev/null
+++ b/darwin-x86/fficonfig.h
@@ -0,0 +1,161 @@
+/* fficonfig.h.  Generated from fficonfig.h.in by configure.  */
+/* fficonfig.h.in.  Generated from configure.ac by autoheader.  */
+
+/* Define to one of `_getb67', `GETB67', `getb67' for Cray-2 and Cray-YMP
+   systems. This function is required for `alloca.c' support on those systems.
+   */
+/* #undef CRAY_STACKSEG_END */
+
+/* Define to 1 if using `alloca.c'. */
+/* #undef C_ALLOCA */
+
+/* Define to the flags needed for the .section .eh_frame directive. */
+#define EH_FRAME_FLAGS "aw"
+
+/* Define this if you want extra debugging. */
+/* #undef FFI_DEBUG */
+
+/* Define this is you do not want support for the raw API. */
+/* #undef FFI_NO_RAW_API */
+
+/* Define this is you do not want support for aggregate types. */
+/* #undef FFI_NO_STRUCTS */
+
+/* Define to 1 if you have `alloca', as a function or macro. */
+#define HAVE_ALLOCA 1
+
+/* Define to 1 if you have <alloca.h> and it should be used (not on Ultrix).
+   */
+#define HAVE_ALLOCA_H 1
+
+/* Define if your assembler supports .cfi_* directives. */
+/* #undef HAVE_AS_CFI_PSEUDO_OP */
+
+/* Define if your assembler supports .register. */
+/* #undef HAVE_AS_REGISTER_PSEUDO_OP */
+
+/* Define if your assembler and linker support unaligned PC relative relocs.
+   */
+/* #undef HAVE_AS_SPARC_UA_PCREL */
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#define HAVE_DLFCN_H 1
+
+/* Define if __attribute__((visibility("hidden"))) is supported. */
+/* #undef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE */
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* Define if you have the long double type and it is bigger than a double */
+#define HAVE_LONG_DOUBLE 1
+
+/* Define to 1 if you have the `memcpy' function. */
+#define HAVE_MEMCPY 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `mmap' function. */
+#define HAVE_MMAP 1
+
+/* Define if mmap with MAP_ANON(YMOUS) works. */
+#define HAVE_MMAP_ANON 1
+
+/* Define if mmap of /dev/zero works. */
+/* #undef HAVE_MMAP_DEV_ZERO */
+
+/* Define if read-only mmap of a plain file works. */
+#define HAVE_MMAP_FILE 1
+
+/* Define if .eh_frame sections should be read-only. */
+/* #undef HAVE_RO_EH_FRAME */
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/mman.h> header file. */
+#define HAVE_SYS_MMAN_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Define to 1 if your C compiler doesn't accept -c and -o together. */
+/* #undef NO_MINUS_C_MINUS_O */
+
+/* Name of package */
+#define PACKAGE "libffi"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "http://gcc.gnu.org/bugs.html"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "libffi"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "libffi 3.0.6"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "libffi"
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "3.0.6"
+
+/* The size of `double', as computed by sizeof. */
+#define SIZEOF_DOUBLE 8
+
+/* The size of `long double', as computed by sizeof. */
+#define SIZEOF_LONG_DOUBLE 16
+
+/* If using the C implementation of alloca, define if you know the
+   direction of stack growth for your system; otherwise it will be
+   automatically deduced at runtime.
+	STACK_DIRECTION > 0 => grows toward higher addresses
+	STACK_DIRECTION < 0 => grows toward lower addresses
+	STACK_DIRECTION = 0 => direction of growth unknown */
+/* #undef STACK_DIRECTION */
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Define this if you are using Purify and want to suppress spurious messages.
+   */
+/* #undef USING_PURIFY */
+
+/* Version number of package */
+#define VERSION "3.0.6"
+
+/* Define to 1 if your processor stores words with the most significant byte
+   first (like Motorola and SPARC, unlike Intel and VAX). */
+/* #undef WORDS_BIGENDIAN */
+
+
+#ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE
+#ifdef LIBFFI_ASM
+#define FFI_HIDDEN(name) .hidden name
+#else
+#define FFI_HIDDEN __attribute__ ((visibility ("hidden")))
+#endif
+#else
+#ifdef LIBFFI_ASM
+#define FFI_HIDDEN(name)
+#else
+#define FFI_HIDDEN
+#endif
+#endif
+
diff --git a/darwin-x86/ffitarget.h b/darwin-x86/ffitarget.h
new file mode 100644
index 0000000..8178d06
--- /dev/null
+++ b/darwin-x86/ffitarget.h
@@ -0,0 +1,90 @@
+/* -----------------------------------------------------------------*-C-*-
+   ffitarget.h - Copyright (c) 1996-2003  Red Hat, Inc.
+   Copyright (C) 2008  Free Software Foundation, Inc.
+
+   Target configuration macros for x86 and x86-64.
+
+   Permission is hereby granted, free of charge, to any person obtaining
+   a copy of this software and associated documentation files (the
+   ``Software''), to deal in the Software without restriction, including
+   without limitation the rights to use, copy, modify, merge, publish,
+   distribute, sublicense, and/or sell copies of the Software, and to
+   permit persons to whom the Software is furnished to do so, subject to
+   the following conditions:
+
+   The above copyright notice and this permission notice shall be included
+   in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+   DEALINGS IN THE SOFTWARE.
+
+   ----------------------------------------------------------------------- */
+
+#ifndef LIBFFI_TARGET_H
+#define LIBFFI_TARGET_H
+
+/* ---- System specific configurations ----------------------------------- */
+
+#if defined (X86_64) && defined (__i386__)
+#undef X86_64
+#define X86
+#endif
+
+/* ---- Generic type definitions ----------------------------------------- */
+
+#ifndef LIBFFI_ASM
+typedef unsigned long          ffi_arg;
+typedef signed long            ffi_sarg;
+
+typedef enum ffi_abi {
+  FFI_FIRST_ABI = 0,
+
+  /* ---- Intel x86 Win32 ---------- */
+#ifdef X86_WIN32
+  FFI_SYSV,
+  FFI_STDCALL,
+  /* TODO: Add fastcall support for the sake of completeness */
+  FFI_DEFAULT_ABI = FFI_SYSV,
+#endif
+
+  /* ---- Intel x86 and AMD x86-64 - */
+#if !defined(X86_WIN32) && (defined(__i386__) || defined(__x86_64__))
+  FFI_SYSV,
+  FFI_UNIX64,   /* Unix variants all use the same ABI for x86-64  */
+#ifdef __i386__
+  FFI_DEFAULT_ABI = FFI_SYSV,
+#else
+  FFI_DEFAULT_ABI = FFI_UNIX64,
+#endif
+#endif
+
+  FFI_LAST_ABI = FFI_DEFAULT_ABI + 1
+} ffi_abi;
+#endif
+
+/* ---- Definitions for closures ----------------------------------------- */
+
+#define FFI_CLOSURES 1
+#define FFI_TYPE_SMALL_STRUCT_1B (FFI_TYPE_LAST + 1)
+#define FFI_TYPE_SMALL_STRUCT_2B (FFI_TYPE_LAST + 2)
+
+#if defined (X86_64) || (defined (__x86_64__) && defined (X86_DARWIN))
+#define FFI_TRAMPOLINE_SIZE 24
+#define FFI_NATIVE_RAW_API 0
+#else
+#ifdef X86_WIN32
+#define FFI_TRAMPOLINE_SIZE 13
+#else
+#define FFI_TRAMPOLINE_SIZE 10
+#endif
+#define FFI_NATIVE_RAW_API 1	/* x86 has native raw api support */
+#endif
+
+#endif
+
diff --git a/testsuite/Android.mk b/testsuite/Android.mk
index 478e344..d7f53de 100644
--- a/testsuite/Android.mk
+++ b/testsuite/Android.mk
@@ -1,153 +1,38 @@
 # Copyright 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
 
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-# List of test cases.  Note that closures are not supported on ARM, so
-# none of "closure*" or "cls*" will work there.
-FFI_TEST_BASE := $(LOCAL_PATH)
-FFI_TEST_FILES := \
-		closure_fn0.c \
-		closure_fn1.c \
-		closure_fn2.c \
-		closure_fn3.c \
-		closure_fn4.c \
-		closure_fn5.c \
-		closure_fn6.c \
-		cls_12byte.c \
-		cls_16byte.c \
-		cls_18byte.c \
-		cls_19byte.c \
-		cls_1_1byte.c \
-		cls_20byte.c \
-		cls_20byte1.c \
-		cls_24byte.c \
-		cls_2byte.c \
-		cls_3_1byte.c \
-		cls_3byte1.c \
-		cls_3byte2.c \
-		cls_4_1byte.c \
-		cls_4byte.c \
-		cls_5_1_byte.c \
-		cls_5byte.c \
-		cls_64byte.c \
-		cls_6_1_byte.c \
-		cls_6byte.c \
-		cls_7_1_byte.c \
-		cls_7byte.c \
-		cls_8byte.c \
-		cls_9byte1.c \
-		cls_9byte2.c \
-		cls_align_double.c \
-		cls_align_float.c \
-		cls_align_longdouble.c \
-		cls_align_pointer.c \
-		cls_align_sint16.c \
-		cls_align_sint32.c \
-		cls_align_sint64.c \
-		cls_align_uint16.c \
-		cls_align_uint32.c \
-		cls_align_uint64.c \
-		cls_double.c \
-		cls_float.c \
-		cls_multi_schar.c \
-		cls_multi_sshort.c \
-		cls_multi_sshortchar.c \
-		cls_multi_uchar.c \
-		cls_multi_ushort.c \
-		cls_multi_ushortchar.c \
-		cls_schar.c \
-		cls_sint.c \
-		cls_sshort.c \
-		cls_uchar.c \
-		cls_uint.c \
-		cls_ulonglong.c \
-		cls_ushort.c \
-		float.c \
-		float1.c \
-		float2.c \
-		float3.c \
-		float4.c \
-		many.c \
-		negint.c \
-		nested_struct.c \
-		nested_struct1.c \
-		nested_struct10.c \
-		nested_struct2.c \
-		nested_struct3.c \
-		nested_struct4.c \
-		nested_struct5.c \
-		nested_struct6.c \
-		nested_struct7.c \
-		nested_struct8.c \
-		nested_struct9.c \
-		problem1.c \
-		promotion.c \
-		pyobjc-tc.c \
-		return_dbl.c \
-		return_dbl1.c \
-		return_dbl2.c \
-		return_fl.c \
-		return_fl1.c \
-		return_fl2.c \
-		return_fl3.c \
-		return_ll.c \
-		return_ll1.c \
-		return_sc.c \
-		return_uc.c \
-		return_ul.c \
-		strlen.c \
-		struct1.c \
-		struct2.c \
-		struct3.c \
-		struct4.c \
-		struct5.c \
-		struct6.c \
-		struct7.c \
-		struct8.c \
-		struct9.c
 
-#		many_win32.c \
-#		strlen_win32.c \
-#
+# Single test file to use when doing a default build.
+FFI_SINGLE_TEST_FILE := libffi.call/struct5.c
 
-# define this to make it stop on the first error
-#FFI_EXIT := exit 1
-FFI_EXIT := true
+# We only build ffi at all for non-arm, non-x86 targets.
+ifneq ($(TARGET_ARCH),arm)
+    ifneq ($(TARGET_ARCH),x86)
 
-#
-# Build and run each test individually.  This doesn't work for device builds.
-# The alternative is to build and install all of the little tests on the
-# device, but that seems silly.
-#
-ffitests:
-	@echo "Testing with LD_LIBRARY_PATH=$(TARGET_OUT_SHARED_LIBRARIES) /tmp/android-ffi-test"
-	@(for file in $(FFI_TEST_FILES); do \
-		echo -n $$file ;\
-		$(CC) -g -Iexternal/libffi/$(TARGET_OS)-$(TARGET_ARCH) \
-			-o /tmp/android-ffi-test \
-			$(FFI_TEST_BASE)/libffi.call/$$file \
-			-L$(TARGET_OUT_SHARED_LIBRARIES) -lffi ;\
-		LD_LIBRARY_PATH=$(TARGET_OUT_SHARED_LIBRARIES) /tmp/android-ffi-test > /tmp/android-ffi-out ;\
-		if [ "$$?" -eq 0 ]; then \
-		    echo " OK" ;\
-		else \
-		    echo " FAIL" ;\
-			cat /tmp/android-ffi-out ;\
-			$(FFI_EXIT) ;\
-		fi ;\
-		done ;\
-	)
-	rm -f /tmp/android-ffi-test
+        include $(CLEAR_VARS)
 
-#
-# Build and install one test, so we have something to try on the device.
-#
-LOCAL_MODULE := ffitest_struct5
-LOCAL_SRC_FILES := libffi.call/struct5.c
-LOCAL_C_INCLUDES := external/libffi/$(TARGET_OS)-$(TARGET_ARCH)
-LOCAL_SHARED_LIBRARIES := libffi
+        LOCAL_SRC_FILES := $(FFI_SINGLE_TEST_FILE)
+        LOCAL_C_INCLUDES := external/libffi/$(TARGET_OS)-$(TARGET_ARCH)
+        LOCAL_SHARED_LIBRARIES := libffi
 
-LOCAL_MODULE_TAGS := tests
+        LOCAL_MODULE := ffi-test
+        LOCAL_MODULE_TAGS := tests
 
-include $(BUILD_EXECUTABLE)
+        include $(BUILD_EXECUTABLE)
+
+    endif
+endif
diff --git a/testsuite/run-all-tests b/testsuite/run-all-tests
new file mode 100755
index 0000000..1d4b956
--- /dev/null
+++ b/testsuite/run-all-tests
@@ -0,0 +1,118 @@
+#!/bin/bash
+#
+# Copyright 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#
+# Script to run all the tests, which only works for host builds. This
+# uses some heuristics to navigate the source tree and built output,
+# and it won't be too surprising if this breaks with some change in
+# the build system.
+#
+
+libName="libffi-host"
+execFile="/tmp/run-test-$$"
+outFile="/tmp/out-test-$$.txt"
+
+
+# Set up prog to be the path of this script, including following symlinks,
+# and set up progdir to be the fully-qualified pathname of its directory.
+
+prog="$0"
+while [ -h "${prog}" ]; do
+    newProg=`/bin/ls -ld "${prog}"`
+    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
+    if expr "x${newProg}" : 'x/' >/dev/null; then
+        prog="${newProg}"
+    else
+        progdir=`dirname "${prog}"`
+        prog="${progdir}/${newProg}"
+    fi
+done
+origDir=`pwd`
+progDir=`dirname "${prog}"`
+cd "${progDir}"
+progDir=`pwd`
+prog="${progDir}"/`basename "${prog}"`
+
+
+# Find the base directory of the source tree (which is expected to be
+# the first directory found up the tree that contains both an out and
+# a build directory).
+
+while true; do
+    if [ -d out -a -d build ]; then
+        break;
+    fi
+    cd ..
+    if [ "x`pwd`" = "x/" ]; then
+        echo "could not find top of source tree" 1>&2
+        exit 1
+    fi
+done
+
+sourceDir=`pwd`
+
+
+# Find the library, collect the list of test files, and set other variables.
+
+if get_build_var x >/dev/null 2>&1; then
+    : # Already have build system defs.
+else
+    # Pull in envsetup.sh.
+    . build/envsetup.sh
+fi
+
+CC=`get_build_var CC`
+HOST_OS=`get_build_var HOST_OS`
+HOST_ARCH=`get_build_var HOST_ARCH`
+
+# All this is to make the libFile be an absolute path.
+libFile=`find out/host/${HOST_OS}-${HOST_ARCH} -name "${libName}.a" | head -1`
+libDir=`dirname ${libFile}`
+libDir=`cd "$libDir"; pwd`
+libFile="${libDir}/${libName}.a"
+
+if [ "x$libFile" = "x" ]; then
+    echo "could not find ${libName}" 1>&2
+    exit 1
+fi
+
+cd "${progDir}"
+testFiles=`/bin/ls libffi.call/*.c`
+
+echo "$libDir"
+ls "$libDir"
+
+# Iterate over all the files, compiling and running each.
+
+for file in $testFiles; do
+    echo "${file}..."
+    rm -f "$execFile" "$outFile"
+    "$CC" -g -I"../${HOST_OS}-${HOST_ARCH}" -o "$execFile" "$file" "$libFile"
+    #    -L"$libDir" -l"$libName"
+    if [ "$?" != "0" ]; then
+        echo "compilation failure" 1>&2
+    else
+        "$execFile" > "$outFile"
+        if [ "$?" = "0" ]; then
+            echo "${file}: OK"
+        else
+            echo "${file}: FAIL"
+            cat "$outFile"
+        fi
+    fi
+done
+
+rm -f "$execFile" "$outFile"