blob: ddc86c299e424366eb66378cbf24bcce694d4d2d [file] [log] [blame]
Dan Willemsen01a405a2016-06-13 17:19:03 -07001// Copyright 2016 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 (
Dan Willemsen01a405a2016-06-13 17:19:03 -070018 "android/soong/android"
Cole Faust8982b1c2024-04-08 16:54:45 -070019 "strings"
Dan Willemsen01a405a2016-06-13 17:19:03 -070020)
21
22var (
Colin Cross33bac242021-07-14 17:03:16 -070023 linuxBionicCflags = []string{
Dan Willemsen01a405a2016-06-13 17:19:03 -070024 "-Wa,--noexecstack",
25
26 "-fPIC",
Dan Willemsen01a405a2016-06-13 17:19:03 -070027
Elliott Hughes0b5dce62024-08-20 15:06:00 +000028 "-fno-omit-frame-pointer",
29
Dan Willemsen01a405a2016-06-13 17:19:03 -070030 "-U_FORTIFY_SOURCE",
31 "-D_FORTIFY_SOURCE=2",
32 "-fstack-protector-strong",
33
34 // From x86_64_device
35 "-ffunction-sections",
Dan Willemsen01a405a2016-06-13 17:19:03 -070036 "-fno-short-enums",
Dan Willemsen01a405a2016-06-13 17:19:03 -070037 "-funwind-tables",
Dan Willemsen01a405a2016-06-13 17:19:03 -070038
Dan Willemsen01a405a2016-06-13 17:19:03 -070039 // Tell clang where the gcc toolchain is
40 "--gcc-toolchain=${LinuxBionicGccRoot}",
41
Dan Willemsen01a405a2016-06-13 17:19:03 -070042 // This is normally in ClangExtraTargetCflags, but this is considered host
43 "-nostdlibinc",
Colin Cross33bac242021-07-14 17:03:16 -070044 }
Dan Willemsen01a405a2016-06-13 17:19:03 -070045
Colin Cross33bac242021-07-14 17:03:16 -070046 linuxBionicLdflags = []string{
Dan Willemsen01a405a2016-06-13 17:19:03 -070047 "-Wl,-z,noexecstack",
48 "-Wl,-z,relro",
49 "-Wl,-z,now",
50 "-Wl,--build-id=md5",
Dan Willemsen01a405a2016-06-13 17:19:03 -070051 "-Wl,--fatal-warnings",
Dan Willemsen01a405a2016-06-13 17:19:03 -070052 "-Wl,--no-undefined-version",
53
54 // Use the device gcc toolchain
55 "--gcc-toolchain=${LinuxBionicGccRoot}",
Colin Cross33bac242021-07-14 17:03:16 -070056 }
Colin Crossd1a28132021-06-21 17:34:47 -070057
Eric Rahm19524712023-10-20 15:56:27 +000058 linuxBionicLldflags = append(linuxBionicLdflags,
59 "-Wl,--compress-debug-sections=zstd",
60 )
61
Colin Crossd1a28132021-06-21 17:34:47 -070062 // Embed the linker into host bionic binaries. This is needed to support host bionic,
63 // as the linux kernel requires that the ELF interpreter referenced by PT_INTERP be
64 // either an absolute path, or relative from CWD. To work around this, we extract
65 // the load sections from the runtime linker ELF binary and embed them into each host
66 // bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static
67 // binary, and then we use a special entry point to fix up the arguments passed by
68 // the kernel before jumping to the embedded linker.
69 linuxBionicCrtBeginSharedBinary = append(android.CopyOf(bionicCrtBeginSharedBinary),
70 "host_bionic_linker_script")
Dan Willemsen01a405a2016-06-13 17:19:03 -070071)
72
Colin Cross4fa894d2022-09-30 15:44:45 -070073const (
74 x86_64GccVersion = "4.9"
75)
76
Dan Willemsen01a405a2016-06-13 17:19:03 -070077func init() {
Cole Faust8982b1c2024-04-08 16:54:45 -070078 pctx.StaticVariable("LinuxBionicCflags", strings.Join(linuxBionicCflags, " "))
79 pctx.StaticVariable("LinuxBionicLdflags", strings.Join(linuxBionicLdflags, " "))
80 pctx.StaticVariable("LinuxBionicLldflags", strings.Join(linuxBionicLldflags, " "))
Dan Willemsen01a405a2016-06-13 17:19:03 -070081
Dan Willemsen01a405a2016-06-13 17:19:03 -070082 // Use the device gcc toolchain for now
Cole Faust8982b1c2024-04-08 16:54:45 -070083 pctx.StaticVariable("LinuxBionicGccVersion", x86_64GccVersion)
84 pctx.SourcePathVariable("LinuxBionicGccRoot",
Colin Cross4fa894d2022-09-30 15:44:45 -070085 "prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${LinuxBionicGccVersion}")
Dan Willemsen01a405a2016-06-13 17:19:03 -070086}
87
88type toolchainLinuxBionic struct {
89 toolchain64Bit
Colin Crosse3fee342021-06-21 17:28:25 -070090 toolchainBionic
Dan Willemsen01a405a2016-06-13 17:19:03 -070091}
92
93func (t *toolchainLinuxBionic) Name() string {
94 return "x86_64"
95}
96
Dan Willemsen01a405a2016-06-13 17:19:03 -070097func (t *toolchainLinuxBionic) IncludeFlags() string {
Martin Stjernholm41ab2512020-04-08 01:06:07 +010098 return ""
Dan Willemsen01a405a2016-06-13 17:19:03 -070099}
100
101func (t *toolchainLinuxBionic) ClangTriple() string {
102 // TODO: we don't have a triple yet b/31393676
103 return "x86_64-linux-android"
104}
105
Colin Cross33bac242021-07-14 17:03:16 -0700106func (t *toolchainLinuxBionic) Cflags() string {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700107 return "${config.LinuxBionicCflags}"
108}
109
Colin Cross33bac242021-07-14 17:03:16 -0700110func (t *toolchainLinuxBionic) Cppflags() string {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700111 return ""
112}
113
Colin Cross33bac242021-07-14 17:03:16 -0700114func (t *toolchainLinuxBionic) Ldflags() string {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700115 return "${config.LinuxBionicLdflags}"
116}
117
Colin Cross33bac242021-07-14 17:03:16 -0700118func (t *toolchainLinuxBionic) Lldflags() string {
Chih-Hung Hsieh3101a962018-04-17 14:16:05 -0700119 return "${config.LinuxBionicLldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700120}
121
Colin Cross33bac242021-07-14 17:03:16 -0700122func (t *toolchainLinuxBionic) ToolchainCflags() string {
Dan Willemsen38394b92017-09-18 22:50:22 -0700123 return "-m64 -march=x86-64" +
124 // TODO: We're not really android, but we don't have a triple yet b/31393676
Alex Lighta08ae842018-10-31 11:18:22 -0700125 " -U__ANDROID__"
Dan Willemsen01a405a2016-06-13 17:19:03 -0700126}
127
Colin Cross33bac242021-07-14 17:03:16 -0700128func (t *toolchainLinuxBionic) ToolchainLdflags() string {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700129 return "-m64"
130}
131
132func (t *toolchainLinuxBionic) AvailableLibraries() []string {
133 return nil
134}
135
Dan Willemsen9ff34c02018-10-10 17:58:19 -0700136func (toolchainLinuxBionic) LibclangRuntimeLibraryArch() string {
137 return "x86_64"
138}
139
Colin Crossd1a28132021-06-21 17:34:47 -0700140func (toolchainLinuxBionic) CrtBeginSharedBinary() []string {
141 return linuxBionicCrtBeginSharedBinary
142}
143
Dan Willemsen01a405a2016-06-13 17:19:03 -0700144var toolchainLinuxBionicSingleton Toolchain = &toolchainLinuxBionic{}
145
146func linuxBionicToolchainFactory(arch android.Arch) Toolchain {
147 return toolchainLinuxBionicSingleton
148}
149
150func init() {
151 registerToolchainFactory(android.LinuxBionic, android.X86_64, linuxBionicToolchainFactory)
152}