cc/linker.go: check min_sdk_version.

Checking sdk_version broke mainline code that compiles against a current
API level but needs to be able to run on an older API level.

Bug: http://b/187907243
Test: treehugger
Change-Id: If1192ef2809e39b043f0a384775b6c9e3a8bd619
This commit is contained in:
Elliott Hughes 2021-05-14 13:07:32 -07:00
parent c2a739c9df
commit 0e9cdb04df
2 changed files with 25 additions and 11 deletions

View File

@ -158,6 +158,21 @@ var NoneApiLevel = ApiLevel{
// The first version that introduced 64-bit ABIs. // The first version that introduced 64-bit ABIs.
var FirstLp64Version = uncheckedFinalApiLevel(21) var FirstLp64Version = uncheckedFinalApiLevel(21)
// Android has had various kinds of packed relocations over the years
// (http://b/187907243).
//
// API level 30 is where the now-standard SHT_RELR is available.
var FirstShtRelrVersion = uncheckedFinalApiLevel(30)
// API level 28 introduced SHT_RELR when it was still Android-only, and used an
// Android-specific relocation.
var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28)
// API level 23 was when we first had the Chrome relocation packer, which is
// obsolete and has been removed, but lld can now generate compatible packed
// relocations itself.
var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23)
// The first API level that does not require NDK code to link // The first API level that does not require NDK code to link
// libandroid_support. // libandroid_support.
var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21) var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21)

View File

@ -18,7 +18,6 @@ import (
"android/soong/android" "android/soong/android"
"android/soong/cc/config" "android/soong/cc/config"
"fmt" "fmt"
"strconv"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
@ -390,17 +389,17 @@ func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
} }
// Check whether the SDK version is not older than the specific one // Check whether the SDK version is not older than the specific one
func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion int) bool { func CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion android.ApiLevel) bool {
if ctx.sdkVersion() == "current" { if ctx.minSdkVersion() == "current" {
return true return true
} }
parsedSdkVersion, err := strconv.Atoi(ctx.sdkVersion()) parsedSdkVersion, err := android.ApiLevelFromUser(ctx, ctx.minSdkVersion())
if err != nil { if err != nil {
ctx.PropertyErrorf("sdk_version", ctx.PropertyErrorf("min_sdk_version",
"Invalid sdk_version value (must be int or current): %q", "Invalid min_sdk_version value (must be int or current): %q",
ctx.sdkVersion()) ctx.minSdkVersion())
} }
if parsedSdkVersion < SdkVersion { if parsedSdkVersion.LessThan(SdkVersion) {
return false return false
} }
return true return true
@ -425,13 +424,13 @@ func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
// ANDROID_RELR relocations were supported at API level >= 28. // ANDROID_RELR relocations were supported at API level >= 28.
// Relocation packer was supported at API level >= 23. // Relocation packer was supported at API level >= 23.
// Do the best we can... // Do the best we can...
if !ctx.useSdk() || CheckSdkVersionAtLeast(ctx, 30) { if !ctx.useSdk() || CheckSdkVersionAtLeast(ctx, android.FirstShtRelrVersion) {
flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr") flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
} else if CheckSdkVersionAtLeast(ctx, 28) { } else if CheckSdkVersionAtLeast(ctx, android.FirstAndroidRelrVersion) {
flags.Global.LdFlags = append(flags.Global.LdFlags, flags.Global.LdFlags = append(flags.Global.LdFlags,
"-Wl,--pack-dyn-relocs=android+relr", "-Wl,--pack-dyn-relocs=android+relr",
"-Wl,--use-android-relr-tags") "-Wl,--use-android-relr-tags")
} else if CheckSdkVersionAtLeast(ctx, 23) { } else if CheckSdkVersionAtLeast(ctx, android.FirstPackedRelocationsVersion) {
flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android") flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
} }
} }