blob: fa0ee6465245970092f43b09cf03c8ac444f00f7 [file] [log] [blame]
Jiyong Park4afa2e22020-07-13 15:43:45 +09001// 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
15package config
16
17import (
Jiyong Park4afa2e22020-07-13 15:43:45 +090018 "strings"
Colin Cross33bac242021-07-14 17:03:16 -070019
20 "android/soong/android"
Jiyong Park4afa2e22020-07-13 15:43:45 +090021)
22
23var (
24 // This is a host toolchain but flags for device toolchain are required
25 // as the flags are actually for Bionic-based builds.
Colin Cross33bac242021-07-14 17:03:16 -070026 linuxCrossCflags = append(deviceGlobalCflags,
Jiyong Park4afa2e22020-07-13 15:43:45 +090027 // 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 Parkb304e802020-11-10 11:55:42 +090033
34 // This is normally in ClangExtraTargetCflags, but that's for device and we need
35 // the same for host
36 "-nostdlibinc",
Colin Cross33bac242021-07-14 17:03:16 -070037 )
Jiyong Park4afa2e22020-07-13 15:43:45 +090038
Colin Cross33bac242021-07-14 17:03:16 -070039 linuxCrossLdflags = []string{
Jiyong Park4afa2e22020-07-13 15:43:45 +090040 "-Wl,-z,noexecstack",
41 "-Wl,-z,relro",
42 "-Wl,-z,now",
43 "-Wl,--build-id=md5",
44 "-Wl,--warn-shared-textrel",
45 "-Wl,--fatal-warnings",
46 "-Wl,--hash-style=gnu",
47 "-Wl,--no-undefined-version",
Colin Cross33bac242021-07-14 17:03:16 -070048 }
Colin Crossd1a28132021-06-21 17:34:47 -070049
50 // Embed the linker into host bionic binaries. This is needed to support host bionic,
51 // as the linux kernel requires that the ELF interpreter referenced by PT_INTERP be
52 // either an absolute path, or relative from CWD. To work around this, we extract
53 // the load sections from the runtime linker ELF binary and embed them into each host
54 // bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static
55 // binary, and then we use a special entry point to fix up the arguments passed by
56 // the kernel before jumping to the embedded linker.
57 linuxArm64CrtBeginSharedBinary = append(android.CopyOf(bionicCrtBeginSharedBinary),
58 "host_bionic_linker_script")
Jiyong Park4afa2e22020-07-13 15:43:45 +090059)
60
61func init() {
62 pctx.StaticVariable("LinuxBionicArm64Cflags", strings.Join(linuxCrossCflags, " "))
63 pctx.StaticVariable("LinuxBionicArm64Ldflags", strings.Join(linuxCrossLdflags, " "))
64}
65
66// toolchain config for ARM64 Linux CrossHost. Almost everything is the same as the ARM64 Android
67// target. The overridden methods below show the differences.
68type toolchainLinuxArm64 struct {
69 toolchainArm64
70}
71
72func (toolchainLinuxArm64) ClangTriple() string {
73 // Note the absence of "-android" suffix. The compiler won't define __ANDROID__
74 return "aarch64-linux"
75}
76
Colin Cross33bac242021-07-14 17:03:16 -070077func (toolchainLinuxArm64) Cflags() string {
Jiyong Park4afa2e22020-07-13 15:43:45 +090078 // The inherited flags + extra flags
79 return "${config.Arm64ClangCflags} ${config.LinuxBionicArm64Cflags}"
80}
81
Colin Crossd1a28132021-06-21 17:34:47 -070082func (toolchainLinuxArm64) CrtBeginSharedBinary() []string {
83 return linuxArm64CrtBeginSharedBinary
84}
85
Jiyong Park4afa2e22020-07-13 15:43:45 +090086func linuxArm64ToolchainFactory(arch android.Arch) Toolchain {
87 archVariant := "armv8-a" // for host, default to armv8-a
Colin Cross33bac242021-07-14 17:03:16 -070088 toolchainCflags := []string{arm64ArchVariantCflagsVar[archVariant]}
Jiyong Park4afa2e22020-07-13 15:43:45 +090089
90 // We don't specify CPU architecture for host. Conservatively assume
91 // the host CPU needs the fix
92 extraLdflags := "-Wl,--fix-cortex-a53-843419"
93
94 ret := toolchainLinuxArm64{}
95
96 // add the extra ld and lld flags
97 ret.toolchainArm64.ldflags = strings.Join([]string{
98 "${config.Arm64Ldflags}",
99 "${config.LinuxBionicArm64Ldflags}",
100 extraLdflags,
101 }, " ")
102 ret.toolchainArm64.lldflags = strings.Join([]string{
103 "${config.Arm64Lldflags}",
104 "${config.LinuxBionicArm64Ldflags}",
105 extraLdflags,
106 }, " ")
Colin Cross33bac242021-07-14 17:03:16 -0700107 ret.toolchainArm64.toolchainCflags = strings.Join(toolchainCflags, " ")
Jiyong Park4afa2e22020-07-13 15:43:45 +0900108 return &ret
109}
110
111func init() {
112 registerToolchainFactory(android.LinuxBionic, android.Arm64, linuxArm64ToolchainFactory)
113}