Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 18 | "strings" |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | // This is a host toolchain but flags for device toolchain are required |
| 25 | // as the flags are actually for Bionic-based builds. |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 26 | linuxCrossCflags = append(deviceGlobalCflags, |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 27 | // clang by default enables PIC when the clang triple is set to *-android. |
| 28 | // See toolchain/llvm-project/clang/lib/Driver/ToolChains/CommonArgs.cpp#920. |
| 29 | // However, for this host target, we don't set "-android" to avoid __ANDROID__ macro |
| 30 | // which stands for "Android device target". Keeping PIC on is required because |
| 31 | // many modules we have (e.g. Bionic) assume PIC. |
| 32 | "-fpic", |
Jiyong Park | b304e80 | 2020-11-10 11:55:42 +0900 | [diff] [blame] | 33 | |
| 34 | // This is normally in ClangExtraTargetCflags, but that's for device and we need |
| 35 | // the same for host |
| 36 | "-nostdlibinc", |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 37 | ) |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 38 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 39 | linuxCrossLdflags = []string{ |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 40 | "-Wl,-z,noexecstack", |
| 41 | "-Wl,-z,relro", |
| 42 | "-Wl,-z,now", |
| 43 | "-Wl,--build-id=md5", |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 44 | "-Wl,--fatal-warnings", |
| 45 | "-Wl,--hash-style=gnu", |
| 46 | "-Wl,--no-undefined-version", |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 47 | } |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 48 | |
| 49 | // Embed the linker into host bionic binaries. This is needed to support host bionic, |
| 50 | // as the linux kernel requires that the ELF interpreter referenced by PT_INTERP be |
| 51 | // either an absolute path, or relative from CWD. To work around this, we extract |
| 52 | // the load sections from the runtime linker ELF binary and embed them into each host |
| 53 | // bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static |
| 54 | // binary, and then we use a special entry point to fix up the arguments passed by |
| 55 | // the kernel before jumping to the embedded linker. |
| 56 | linuxArm64CrtBeginSharedBinary = append(android.CopyOf(bionicCrtBeginSharedBinary), |
| 57 | "host_bionic_linker_script") |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | func init() { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c4d11bc | 2022-10-14 21:20:27 +0000 | [diff] [blame] | 61 | exportedVars.ExportStringListStaticVariable("LinuxBionicArm64Cflags", linuxCrossCflags) |
| 62 | exportedVars.ExportStringListStaticVariable("LinuxBionicArm64Ldflags", linuxCrossLdflags) |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // toolchain config for ARM64 Linux CrossHost. Almost everything is the same as the ARM64 Android |
| 66 | // target. The overridden methods below show the differences. |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 67 | type toolchainLinuxBionicArm64 struct { |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 68 | toolchainArm64 |
| 69 | } |
| 70 | |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 71 | func (toolchainLinuxBionicArm64) ClangTriple() string { |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 72 | // Note the absence of "-android" suffix. The compiler won't define __ANDROID__ |
| 73 | return "aarch64-linux" |
| 74 | } |
| 75 | |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 76 | func (toolchainLinuxBionicArm64) Cflags() string { |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 77 | // The inherited flags + extra flags |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 78 | return "${config.Arm64Cflags} ${config.LinuxBionicArm64Cflags}" |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 81 | func (toolchainLinuxBionicArm64) CrtBeginSharedBinary() []string { |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 82 | return linuxArm64CrtBeginSharedBinary |
| 83 | } |
| 84 | |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 85 | func linuxBionicArm64ToolchainFactory(arch android.Arch) Toolchain { |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 86 | archVariant := "armv8-a" // for host, default to armv8-a |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 87 | toolchainCflags := []string{arm64ArchVariantCflagsVar[archVariant]} |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 88 | |
| 89 | // We don't specify CPU architecture for host. Conservatively assume |
| 90 | // the host CPU needs the fix |
| 91 | extraLdflags := "-Wl,--fix-cortex-a53-843419" |
| 92 | |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 93 | ret := toolchainLinuxBionicArm64{} |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 94 | |
| 95 | // add the extra ld and lld flags |
| 96 | ret.toolchainArm64.ldflags = strings.Join([]string{ |
| 97 | "${config.Arm64Ldflags}", |
| 98 | "${config.LinuxBionicArm64Ldflags}", |
| 99 | extraLdflags, |
| 100 | }, " ") |
| 101 | ret.toolchainArm64.lldflags = strings.Join([]string{ |
| 102 | "${config.Arm64Lldflags}", |
| 103 | "${config.LinuxBionicArm64Ldflags}", |
| 104 | extraLdflags, |
| 105 | }, " ") |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 106 | ret.toolchainArm64.toolchainCflags = strings.Join(toolchainCflags, " ") |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 107 | return &ret |
| 108 | } |
| 109 | |
| 110 | func init() { |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 111 | registerToolchainFactory(android.LinuxBionic, android.Arm64, linuxBionicArm64ToolchainFactory) |
Jiyong Park | 4afa2e2 | 2020-07-13 15:43:45 +0900 | [diff] [blame] | 112 | } |