From f6dc24bd84625469770a955609b467d3d7c4c829 Mon Sep 17 00:00:00 2001
From: David 'Digit' Turner <digit@google.com>
Date: Fri, 13 Apr 2012 16:14:11 +0200
Subject: Add MIPS Android support and default to Little Endian

Change-Id: I6e8e5ab1d773a8eef02287725246f57c861e0dd6
---
 gcc-4.4.3/gcc/config.gcc                  |    6 ++++
 gcc-4.4.3/gcc/config/mips/linux.h         |   40 ++++++++++++++++++++++++----
 gcc-4.4.3/gcc/config/mips/t-linux-android |    3 ++
 gcc-4.4.3/gcc/unwind-dw2-fde-glibc.c      |    6 +++-
 4 files changed, 47 insertions(+), 8 deletions(-)
 create mode 100644 gcc-4.4.3/gcc/config/mips/t-linux-android

diff --git a/gcc-4.4.3/gcc/config.gcc b/gcc-4.4.3/gcc/config.gcc
index e758dc4..755b90d 100644
--- a/gcc-4.4.3/gcc/config.gcc
+++ b/gcc-4.4.3/gcc/config.gcc
@@ -1628,6 +1628,12 @@ mips*-*-linux*)				# Linux MIPS, either endian.
         mipsisa32*)
 		tm_defines="${tm_defines} MIPS_ISA_DEFAULT=32"
         esac
+	case ${target} in
+	*android*)
+		# Default to little-endian for MIPS Android
+		# tm_defines="${tm_defines} TARGET_ENDIAN_DEFAULT=0"
+		tmake_file="$tmake_file mips/t-linux-android"
+        esac
 	test x$with_llsc != x || with_llsc=yes
 	;;
 mips*-*-openbsd*)
diff --git a/gcc-4.4.3/gcc/config/mips/linux.h b/gcc-4.4.3/gcc/config/mips/linux.h
index 0512ef7..9e3ec05 100644
--- a/gcc-4.4.3/gcc/config/mips/linux.h
+++ b/gcc-4.4.3/gcc/config/mips/linux.h
@@ -47,6 +47,7 @@ along with GCC; see the file COPYING3.  If not see
     /* The GNU C++ standard library requires this.  */		\
     if (c_dialect_cxx ())					\
       builtin_define ("_GNU_SOURCE");				\
+    ANDROID_TARGET_OS_CPP_BUILTINS();				\
   } while (0)
 
 #undef SUBTARGET_CPP_SPEC
@@ -55,8 +56,8 @@ along with GCC; see the file COPYING3.  If not see
 /* A standard GNU/Linux mapping.  On most targets, it is included in
    CC1_SPEC itself by config/linux.h, but mips.h overrides CC1_SPEC
    and provides this hook instead.  */
-#undef SUBTARGET_CC1_SPEC
-#define SUBTARGET_CC1_SPEC "%{profile:-p}"
+#undef LINUX_SUBTARGET_CC1_SPEC
+#define LINUX_SUBTARGET_CC1_SPEC "%{profile:-p}"
 
 /* From iris5.h */
 /* -G is incompatible with -KPIC which is the default, so only allow objects
@@ -67,8 +68,8 @@ along with GCC; see the file COPYING3.  If not see
 #define GLIBC_DYNAMIC_LINKER "/lib/ld.so.1"
 
 /* Borrowed from sparc/linux.h */
-#undef LINK_SPEC
-#define LINK_SPEC \
+#undef LINUX_SUBTARGET_LINK_SPEC
+#define LINUX_SUBTARGET_LINK_SPEC \
  "%(endian_spec) \
   %{shared:-shared} \
   %{!shared: \
@@ -103,8 +104,8 @@ along with GCC; see the file COPYING3.  If not see
 #undef ASM_OUTPUT_REG_PUSH
 #undef ASM_OUTPUT_REG_POP
 
-#undef LIB_SPEC
-#define LIB_SPEC "\
+#undef LINUX_SUBTARGET_LIB_SPEC
+#define LINUX_SUBTARGET_LIB_SPEC "\
 %{pthread:-lpthread} \
 %{shared:-lc} \
 %{!shared: \
@@ -135,7 +135,7 @@ extern const char *host_detect_local_cpu (int argc, const char **argv);
 #endif

 #define LINUX_DRIVER_SELF_SPECS \
-  NO_SHARED_SPECS							\
+  LINUX_OR_ANDROID_CC(NO_SHARED_SPECS, "")                              \
   MARCH_MTUNE_NATIVE_SPECS,						\
   /* -mplt has no effect without -mno-shared.  Simplify later		\
      specs handling by removing a redundant option.  */			\
@@ -147,3 +148,30 @@ extern const char *host_detect_local_cpu (int argc, const char **argv);
 #define DRIVER_SELF_SPECS \
   BASE_DRIVER_SELF_SPECS, \
   LINUX_DRIVER_SELF_SPECS
+
+#undef  LINK_SPEC
+#define LINK_SPEC							\
+  LINUX_OR_ANDROID_LD (LINUX_SUBTARGET_LINK_SPEC,			\
+		       LINUX_SUBTARGET_LINK_SPEC " " ANDROID_LINK_SPEC)
+
+#undef  SUBTARGET_CC1_SPEC
+#define SUBTARGET_CC1_SPEC						\
+  LINUX_OR_ANDROID_CC (LINUX_SUBTARGET_CC1_SPEC,			\
+		       LINUX_SUBTARGET_CC1_SPEC " " ANDROID_CC1_SPEC("-fpic"))
+
+#undef  CC1PLUS_SPEC
+#define CC1PLUS_SPEC							\
+  LINUX_OR_ANDROID_CC ("", ANDROID_CC1PLUS_SPEC)
+
+#undef  LIB_SPEC
+#define LIB_SPEC							\
+  LINUX_OR_ANDROID_LD (LINUX_SUBTARGET_LIB_SPEC,			\
+		       LINUX_SUBTARGET_LIB_SPEC " " ANDROID_LIB_SPEC)
+
+#undef STARTFILE_SPEC
+#define STARTFILE_SPEC							\
+  LINUX_OR_ANDROID_LD (LINUX_TARGET_STARTFILE_SPEC, ANDROID_STARTFILE_SPEC)
+
+#undef ENDFILE_SPEC
+#define ENDFILE_SPEC							\
+  LINUX_OR_ANDROID_LD (LINUX_TARGET_ENDFILE_SPEC, ANDROID_ENDFILE_SPEC)
diff --git a/gcc-4.4.3/gcc/config/mips/t-linux-android b/gcc-4.4.3/gcc/config/mips/t-linux-android
new file mode 100644
index 0000000..298cad9
--- /dev/null
+++ b/gcc-4.4.3/gcc/config/mips/t-linux-android
@@ -0,0 +1,3 @@
+MULTILIB_OPTIONS = mips32r2
+MULTILIB_DIRNAMES = mips-r2
+MULTILIB_EXCLUSIONS :=
diff --git a/gcc-4.4.3/gcc/unwind-dw2-fde-glibc.c b/gcc-4.4.3/gcc/unwind-dw2-fde-glibc.c
index 11f53dd..bad6082 100644
--- a/gcc-4.4.3/gcc/unwind-dw2-fde-glibc.c
+++ b/gcc-4.4.3/gcc/unwind-dw2-fde-glibc.c
@@ -46,8 +46,10 @@
 #include "gthr.h"
 
 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
-    && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
-	|| (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
+    && ((defined(__BIONIC__) && (defined(mips) || defined(__mips__))) \
+	|| (__GLIBC__ > 2 \
+	    || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
+	    || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))))
 
 #include <link.h>
 
-- 
1.7.6.rc0