Merge "Revert "Revert "Android Q is API 29""" into qt-dev
diff --git a/Android.bp b/Android.bp
index 910fe4b..56fa894 100644
--- a/Android.bp
+++ b/Android.bp
@@ -480,6 +480,117 @@
 }
 
 toolchain_library {
+    name: "libgcc_stripped",
+    defaults: ["linux_bionic_supported"],
+    vendor_available: true,
+    recovery_available: true,
+
+    arch: {
+        arm: {
+            src: "prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a",
+            strip: {
+                keep_symbols_list: [
+                    // unwind-arm.o
+                    "_Unwind_Complete",
+                    "_Unwind_DeleteException",
+                    "_Unwind_GetCFA",
+                    "_Unwind_VRS_Get",
+                    "_Unwind_VRS_Pop",
+                    "_Unwind_VRS_Set",
+                    "__aeabi_unwind_cpp_pr0",
+                    "__aeabi_unwind_cpp_pr1",
+                    "__aeabi_unwind_cpp_pr2",
+                    "__gnu_Unwind_Backtrace",
+                    "__gnu_Unwind_ForcedUnwind",
+                    "__gnu_Unwind_RaiseException",
+                    "__gnu_Unwind_Resume",
+                    "__gnu_Unwind_Resume_or_Rethrow",
+
+                    // libunwind.o
+                    "_Unwind_Backtrace",
+                    "_Unwind_ForcedUnwind",
+                    "_Unwind_RaiseException",
+                    "_Unwind_Resume",
+                    "_Unwind_Resume_or_Rethrow",
+                    "___Unwind_Backtrace",
+                    "___Unwind_ForcedUnwind",
+                    "___Unwind_RaiseException",
+                    "___Unwind_Resume",
+                    "___Unwind_Resume_or_Rethrow",
+                    "__gnu_Unwind_Restore_VFP",
+                    "__gnu_Unwind_Restore_VFP_D",
+                    "__gnu_Unwind_Restore_VFP_D_16_to_31",
+                    "__gnu_Unwind_Restore_WMMXC",
+                    "__gnu_Unwind_Restore_WMMXD",
+                    "__gnu_Unwind_Save_VFP",
+                    "__gnu_Unwind_Save_VFP_D",
+                    "__gnu_Unwind_Save_VFP_D_16_to_31",
+                    "__gnu_Unwind_Save_WMMXC",
+                    "__gnu_Unwind_Save_WMMXD",
+                    "__restore_core_regs",
+                    "restore_core_regs",
+
+                    // pr-support.o
+                    "_Unwind_GetDataRelBase",
+                    "_Unwind_GetLanguageSpecificData",
+                    "_Unwind_GetRegionStart",
+                    "_Unwind_GetTextRelBase",
+                    "__gnu_unwind_execute",
+                    "__gnu_unwind_frame",
+                ],
+                use_gnu_strip: true,
+            },
+        },
+        arm64: {
+            src: "prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/lib/gcc/aarch64-linux-android/4.9.x/libgcc.a",
+        },
+        x86: {
+            src: "prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/lib/gcc/x86_64-linux-android/4.9.x/32/libgcc.a",
+
+        },
+        x86_64: {
+            src: "prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/lib/gcc/x86_64-linux-android/4.9.x/libgcc.a",
+        },
+    },
+    strip: {
+        keep_symbols_list: [
+            // unwind-dw2.o
+            "_Unwind_Backtrace",
+            "_Unwind_DeleteException",
+            "_Unwind_FindEnclosingFunction",
+            "_Unwind_ForcedUnwind",
+            "_Unwind_GetCFA",
+            "_Unwind_GetDataRelBase",
+            "_Unwind_GetGR",
+            "_Unwind_GetIP",
+            "_Unwind_GetIPInfo",
+            "_Unwind_GetLanguageSpecificData",
+            "_Unwind_GetRegionStart",
+            "_Unwind_GetTextRelBase",
+            "_Unwind_RaiseException",
+            "_Unwind_Resume",
+            "_Unwind_Resume_or_Rethrow",
+            "_Unwind_SetGR",
+            "_Unwind_SetIP",
+            "__frame_state_for",
+
+            // unwind-dw2-fde-dip.o
+            "_Unwind_Find_FDE",
+            "__deregister_frame",
+            "__deregister_frame_info",
+            "__deregister_frame_info_bases",
+            "__register_frame",
+            "__register_frame_info",
+            "__register_frame_info_bases",
+            "__register_frame_info_table",
+            "__register_frame_info_table_bases",
+            "__register_frame_table",
+        ],
+        use_gnu_strip: true,
+    },
+}
+
+toolchain_library {
     name: "libwinpthread",
     host_supported: true,
     enabled: false,
diff --git a/android/prebuilt_etc.go b/android/prebuilt_etc.go
index 2a3e07e..8f23d78 100644
--- a/android/prebuilt_etc.go
+++ b/android/prebuilt_etc.go
@@ -158,6 +158,7 @@
 			fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
 			fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
 			fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
+			fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", p.Arch().ArchType.String())
 			if p.additionalDependencies != nil {
 				fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
 				for _, path := range *p.additionalDependencies {
diff --git a/apex/apex_test.go b/apex/apex_test.go
index c771ae4..a07a89b 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -93,6 +93,13 @@
 		}
 
 		toolchain_library {
+			name: "libgcc_stripped",
+			src: "",
+			vendor_available: true,
+			recovery_available: true,
+		}
+
+		toolchain_library {
 			name: "libclang_rt.builtins-aarch64-android",
 			src: "",
 			vendor_available: true,
diff --git a/cc/builder.go b/cc/builder.go
index c64243f..c42f56c 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -255,6 +255,7 @@
 	groupStaticLibs bool
 
 	stripKeepSymbols       bool
+	stripKeepSymbolsList   string
 	stripKeepMiniDebugInfo bool
 	stripAddGnuDebuglink   bool
 	stripUseGnuStrip       bool
@@ -823,6 +824,9 @@
 	if flags.stripKeepSymbols {
 		args += " --keep-symbols"
 	}
+	if flags.stripKeepSymbolsList != "" {
+		args += " -k" + flags.stripKeepSymbolsList
+	}
 	if flags.stripUseGnuStrip {
 		args += " --use-gnu-strip"
 	}
diff --git a/cc/cc_test.go b/cc/cc_test.go
index 05d74b9..f3d5e60 100644
--- a/cc/cc_test.go
+++ b/cc/cc_test.go
@@ -1833,13 +1833,13 @@
 	// Check the shared version of lib2.
 	variant := "android_arm64_armv8-a_core_shared"
 	module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
-	checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc"}, module)
+	checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
 
 	// Check the static version of lib2.
 	variant = "android_arm64_armv8-a_core_static"
 	module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
 	// libc++_static is linked additionally.
-	checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc"}, module)
+	checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)
 }
 
 var compilerFlagsTestCases = []struct {
diff --git a/cc/config/global.go b/cc/config/global.go
index 7775e9e..815c31d 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -84,6 +84,7 @@
 		"-Wl,--fatal-warnings",
 		"-Wl,--no-undefined-version",
 		"-Wl,--exclude-libs,libgcc.a",
+		"-Wl,--exclude-libs,libgcc_stripped.a",
 	}
 
 	deviceGlobalLldflags = append(ClangFilterUnknownLldflags(deviceGlobalLdflags),
diff --git a/cc/linker.go b/cc/linker.go
index b279c06..e063e44 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -228,10 +228,10 @@
 		// libclang_rt.builtins, libgcc and libatomic have to be last on the command line
 		if !Bool(linker.Properties.No_libcrt) {
 			deps.LateStaticLibs = append(deps.LateStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
-		}
-
-		deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
-		if !Bool(linker.Properties.No_libgcc) {
+			deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
+			deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc_stripped")
+		} else if !Bool(linker.Properties.No_libgcc) {
+			deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
 			deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
 		}
 
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 2d80c22..ef60b8e 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -57,7 +57,7 @@
 
 	minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
 		"-fno-sanitize-recover=integer,undefined"}
-	hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512"}
+	hwasanGlobalOptions = []string{"heap_history_size=1023,stack_history_size=512,export_memory_stats=0"}
 )
 
 type sanitizerType int
diff --git a/cc/strip.go b/cc/strip.go
index 02397f4..7122585 100644
--- a/cc/strip.go
+++ b/cc/strip.go
@@ -15,15 +15,19 @@
 package cc
 
 import (
+	"strings"
+
 	"android/soong/android"
 )
 
 type StripProperties struct {
 	Strip struct {
-		None         *bool
-		All          *bool
-		Keep_symbols *bool
-	}
+		None              *bool    `android:"arch_variant"`
+		All               *bool    `android:"arch_variant"`
+		Keep_symbols      *bool    `android:"arch_variant"`
+		Keep_symbols_list []string `android:"arch_variant"`
+		Use_gnu_strip     *bool    `android:"arch_variant"`
+	} `android:"arch_variant"`
 }
 
 type stripper struct {
@@ -42,9 +46,14 @@
 	} else {
 		if Bool(stripper.StripProperties.Strip.Keep_symbols) {
 			flags.stripKeepSymbols = true
+		} else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
+			flags.stripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
 		} else if !Bool(stripper.StripProperties.Strip.All) {
 			flags.stripKeepMiniDebugInfo = true
 		}
+		if Bool(stripper.StripProperties.Strip.Use_gnu_strip) {
+			flags.stripUseGnuStrip = true
+		}
 		if ctx.Config().Debuggable() && !flags.stripKeepMiniDebugInfo {
 			flags.stripAddGnuDebuglink = true
 		}
diff --git a/cc/testing.go b/cc/testing.go
index 2f41de1..8d76c2f 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -69,6 +69,13 @@
 			src: "",
 		}
 
+		toolchain_library {
+			name: "libgcc_stripped",
+			vendor_available: true,
+			recovery_available: true,
+			src: "",
+		}
+
 		cc_library {
 			name: "libc",
 			no_libgcc: true,
diff --git a/cc/toolchain_library.go b/cc/toolchain_library.go
index ae08b1c..8ab8bc9 100644
--- a/cc/toolchain_library.go
+++ b/cc/toolchain_library.go
@@ -34,6 +34,8 @@
 type toolchainLibraryDecorator struct {
 	*libraryDecorator
 
+	stripper
+
 	Properties toolchainLibraryProperties
 }
 
@@ -45,7 +47,7 @@
 func (library *toolchainLibraryDecorator) linkerProps() []interface{} {
 	var props []interface{}
 	props = append(props, library.libraryDecorator.linkerProps()...)
-	return append(props, &library.Properties)
+	return append(props, &library.Properties, &library.stripper.StripProperties)
 }
 
 func ToolchainLibraryFactory() android.Module {
@@ -75,7 +77,17 @@
 		return android.PathForSource(ctx, "")
 	}
 
-	return android.PathForSource(ctx, *library.Properties.Src)
+	srcPath := android.PathForSource(ctx, *library.Properties.Src)
+
+	if library.stripper.StripProperties.Strip.Keep_symbols_list != nil {
+		fileName := ctx.ModuleName() + staticLibraryExtension
+		outputFile := android.PathForModuleOut(ctx, fileName)
+		buildFlags := flagsToBuilderFlags(flags)
+		library.stripper.strip(ctx, srcPath, outputFile, buildFlags)
+		return outputFile
+	}
+
+	return srcPath
 }
 
 func (library *toolchainLibraryDecorator) nativeCoverage() bool {
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index f1fa0ff..3b77042 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -59,6 +59,7 @@
 	NeverAllowStripping bool // whether stripping should not be done - used as build time check to make sure dex files are always available
 
 	NoDebugInfo                 bool // don't generate debug info by default
+	DontResolveStartupStrings   bool // don't resolve string literals loaded during application startup.
 	AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
 	NeverSystemServerDebugInfo  bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
 	AlwaysOtherDebugInfo        bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
@@ -301,6 +302,7 @@
 		GenerateDMFiles:                    false,
 		NeverAllowStripping:                false,
 		NoDebugInfo:                        false,
+		DontResolveStartupStrings:          false,
 		AlwaysSystemServerDebugInfo:        false,
 		NeverSystemServerDebugInfo:         false,
 		AlwaysOtherDebugInfo:               false,
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
index 01ee15e..5b658d9 100644
--- a/dexpreopt/dexpreopt.go
+++ b/dexpreopt/dexpreopt.go
@@ -461,6 +461,9 @@
 		appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
 		cmd.FlagWithOutput("--app-image-file=", appImagePath).
 			FlagWithArg("--image-format=", "lz4")
+		if !global.DontResolveStartupStrings {
+			cmd.FlagWithArg("--resolve-startup-const-strings=", "true")
+		}
 		rule.Install(appImagePath, appImageInstallPath)
 	}
 
diff --git a/java/dexpreopt_config.go b/java/dexpreopt_config.go
index 409b4b1..abc5fa1 100644
--- a/java/dexpreopt_config.go
+++ b/java/dexpreopt_config.go
@@ -136,21 +136,29 @@
 		global := dexpreoptGlobalConfig(ctx)
 
 		runtimeModules := global.RuntimeApexJars
+		nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules)
+		frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
+		imageModules := concat(runtimeModules, frameworkModules)
 
-		var runtimeBootLocations []string
+		var bootLocations []string
 
 		for _, m := range runtimeModules {
-			runtimeBootLocations = append(runtimeBootLocations,
+			bootLocations = append(bootLocations,
 				filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
 		}
 
+		for _, m := range frameworkModules {
+			bootLocations = append(bootLocations,
+				filepath.Join("/system/framework", m+".jar"))
+		}
+
 		// The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
 		// the bootclasspath modules have been compiled.  Set up known paths for them, the singleton rules will copy
 		// them there.
 		// TODO: use module dependencies instead
-		var runtimeBootDexPaths android.WritablePaths
-		for _, m := range runtimeModules {
-			runtimeBootDexPaths = append(runtimeBootDexPaths,
+		var bootDexPaths android.WritablePaths
+		for _, m := range imageModules {
+			bootDexPaths = append(bootDexPaths,
 				android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar"))
 		}
 
@@ -165,9 +173,9 @@
 
 		return bootImageConfig{
 			name:         "apex",
-			modules:      runtimeModules,
-			dexLocations: runtimeBootLocations,
-			dexPaths:     runtimeBootDexPaths,
+			modules:      imageModules,
+			dexLocations: bootLocations,
+			dexPaths:     bootDexPaths,
 			dir:          dir,
 			symbolsDir:   symbolsDir,
 			images:       images,
diff --git a/scripts/strip.sh b/scripts/strip.sh
index d536907..0f77da8 100755
--- a/scripts/strip.sh
+++ b/scripts/strip.sh
@@ -24,6 +24,7 @@
 #   -i ${file}: input file (required)
 #   -o ${file}: output file (required)
 #   -d ${file}: deps file (required)
+#   -k symbols: Symbols to keep (optional)
 #   --add-gnu-debuglink
 #   --keep-mini-debug-info
 #   --keep-symbols
@@ -32,11 +33,11 @@
 
 set -o pipefail
 
-OPTSTRING=d:i:o:-:
+OPTSTRING=d:i:o:k:-:
 
 usage() {
     cat <<EOF
-Usage: strip.sh [options] -i in-file -o out-file -d deps-file
+Usage: strip.sh [options] -k symbols -i in-file -o out-file -d deps-file
 Options:
         --add-gnu-debuglink     Add a gnu-debuglink section to out-file
         --keep-mini-debug-info  Keep compressed debug info in out-file
@@ -71,6 +72,20 @@
     fi
 }
 
+do_strip_keep_symbol_list() {
+    if [ -z "${use_gnu_strip}" ]; then
+        echo "do_strip_keep_symbol_list does not work with llvm-objcopy"
+        echo "http://b/131631155"
+        usage
+    fi
+
+    echo "${symbols_to_keep}" | tr ',' '\n' > "${outfile}.symbolList"
+    KEEP_SYMBOLS="-w --strip-unneeded-symbol=* --keep-symbols="
+    KEEP_SYMBOLS+="${outfile}.symbolList"
+
+    "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${KEEP_SYMBOLS}
+}
+
 do_strip_keep_mini_debug_info() {
     rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz"
     local fail=
@@ -124,19 +139,21 @@
 
 while getopts $OPTSTRING opt; do
     case "$opt" in
-	d) depsfile="${OPTARG}" ;;
-	i) infile="${OPTARG}" ;;
-	o) outfile="${OPTARG}" ;;
-	-)
-	    case "${OPTARG}" in
-		add-gnu-debuglink) add_gnu_debuglink=true ;;
-		keep-mini-debug-info) keep_mini_debug_info=true ;;
-		keep-symbols) keep_symbols=true ;;
-		remove-build-id) remove_build_id=true ;;
-		*) echo "Unknown option --${OPTARG}"; usage ;;
-	    esac;;
-	?) usage ;;
-	*) echo "'${opt}' '${OPTARG}'"
+        d) depsfile="${OPTARG}" ;;
+        i) infile="${OPTARG}" ;;
+        o) outfile="${OPTARG}" ;;
+        k) symbols_to_keep="${OPTARG}" ;;
+        -)
+            case "${OPTARG}" in
+                add-gnu-debuglink) add_gnu_debuglink=true ;;
+                keep-mini-debug-info) keep_mini_debug_info=true ;;
+                keep-symbols) keep_symbols=true ;;
+                remove-build-id) remove_build_id=true ;;
+                use-gnu-strip) use_gnu_strip=true ;;
+                *) echo "Unknown option --${OPTARG}"; usage ;;
+            esac;;
+        ?) usage ;;
+        *) echo "'${opt}' '${OPTARG}'"
     esac
 done
 
@@ -160,6 +177,11 @@
     usage
 fi
 
+if [ ! -z "${symbols_to_keep}" -a ! -z "${keep_symbols}" ]; then
+    echo "--keep-symbols and -k cannot be used together"
+    usage
+fi
+
 if [ ! -z "${add_gnu_debuglink}" -a ! -z "${keep_mini_debug_info}" ]; then
     echo "--add-gnu-debuglink cannot be used with --keep-mini-debug-info"
     usage
@@ -169,6 +191,8 @@
 
 if [ ! -z "${keep_symbols}" ]; then
     do_strip_keep_symbols
+elif [ ! -z "${symbols_to_keep}" ]; then
+    do_strip_keep_symbol_list
 elif [ ! -z "${keep_mini_debug_info}" ]; then
     do_strip_keep_mini_debug_info
 else
diff --git a/ui/build/paths/config.go b/ui/build/paths/config.go
index ed2d9ca..f4bb89f 100644
--- a/ui/build/paths/config.go
+++ b/ui/build/paths/config.go
@@ -81,6 +81,7 @@
 	"dd":       Allowed,
 	"diff":     Allowed,
 	"egrep":    Allowed,
+	"expr":     Allowed,
 	"find":     Allowed,
 	"fuser":    Allowed,
 	"getopt":   Allowed,
@@ -99,6 +100,7 @@
 	"python3":  Allowed,
 	"realpath": Allowed,
 	"rsync":    Allowed,
+	"sed":      Allowed,
 	"sh":       Allowed,
 	"tar":      Allowed,
 	"timeout":  Allowed,
@@ -134,7 +136,6 @@
 	"du":        LinuxOnlyPrebuilt,
 	"echo":      LinuxOnlyPrebuilt,
 	"env":       LinuxOnlyPrebuilt,
-	"expr":      LinuxOnlyPrebuilt,
 	"head":      LinuxOnlyPrebuilt,
 	"getconf":   LinuxOnlyPrebuilt,
 	"hostname":  LinuxOnlyPrebuilt,
@@ -154,7 +155,6 @@
 	"readlink":  LinuxOnlyPrebuilt,
 	"rm":        LinuxOnlyPrebuilt,
 	"rmdir":     LinuxOnlyPrebuilt,
-	"sed":       LinuxOnlyPrebuilt,
 	"seq":       LinuxOnlyPrebuilt,
 	"setsid":    LinuxOnlyPrebuilt,
 	"sha1sum":   LinuxOnlyPrebuilt,