toolchain: use pie/runpath by default

Bionic will reject non-PIC/PIE code now for all programs.  We need to
manually add the -fpie/-pie flags to the command line in our compiler
wrapper so we get PIE programs by default.  Ideally this would be in
the compiler itself, but the Android toolchain doesn't do that, and
upstream gcc support isn't available until gcc-6.

Bionic also only accepts DT_RUNPATH tags and ignores DT_RPATH tags,
so switch over to the newer tag format so ELFs can find their libs.

BUG=b:26861037
TEST=`python2.7 -c 'print("HI")'` works

Change-Id: I3d114119cd5c0920a152e9a7c136b1cf8c47a3d6
diff --git a/toolchain/3rd-party-g++.in b/toolchain/3rd-party-g++.in
index 7c0fa78..f308016 100644
--- a/toolchain/3rd-party-g++.in
+++ b/toolchain/3rd-party-g++.in
@@ -4,12 +4,20 @@
   echo "$0: poisoned host path detected: $*" >&2
   exit 1
 fi
+
+# Android only supports PIEs, but they don't set the default PIE behavior in
+# the compiler, so we need to manually add the flags.
+case " $* " in
+*" -fPIC "*|*" -fpic "*|*" -fPIE "*|*" -fpie "*|*" -shared "*|*" -static "*|*" -pie "*) ;;
+*) set -- -fpie -pie "$@"
+esac
+
 exec \
 	"${ANDROID_TOOLCHAIN}/@CXX@" \
 	@CXXFLAGS@ \
 	@LDFLAGS@ \
 	--sysroot "${ANDROID_PRODUCT_OUT}/@ROOT_SUBDIR@" \
-	-Wl,-rpath,/system/usr/@LIBDIR@ \
+	-Wl,--enable-new-dtags,-rpath,/system/usr/@LIBDIR@ \
 	-Wl,-lc++ \
 	-frtti \
 	"$@"
diff --git a/toolchain/3rd-party-gcc.in b/toolchain/3rd-party-gcc.in
index 3a03f13..e5e3af4 100644
--- a/toolchain/3rd-party-gcc.in
+++ b/toolchain/3rd-party-gcc.in
@@ -4,10 +4,18 @@
   echo "$0: poisoned host path detected: $*" >&2
   exit 1
 fi
+
+# Android only supports PIEs, but they don't set the default PIE behavior in
+# the compiler, so we need to manually add the flags.
+case " $* " in
+*" -fPIC "*|*" -fpic "*|*" -fPIE "*|*" -fpie "*|*" -shared "*|*" -static "*|*" -pie "*) ;;
+*) set -- -fpie -pie "$@"
+esac
+
 exec \
 	"${ANDROID_TOOLCHAIN}/@CC@" \
 	@CFLAGS@ \
 	@LDFLAGS@ \
 	--sysroot "${ANDROID_PRODUCT_OUT}/@ROOT_SUBDIR@" \
-	-Wl,-rpath,/system/usr/@LIBDIR@ \
+	-Wl,--enable-new-dtags,-rpath,/system/usr/@LIBDIR@ \
 	"$@"