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