| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 18 | "android/soong/android" |
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 19 | "strings" |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 23 | linuxBionicCflags = []string{ |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 24 | "-Wa,--noexecstack", |
| 25 | |
| 26 | "-fPIC", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 27 | |
| Elliott Hughes | 0b5dce6 | 2024-08-20 15:06:00 +0000 | [diff] [blame^] | 28 | "-fno-omit-frame-pointer", |
| 29 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 30 | "-U_FORTIFY_SOURCE", |
| 31 | "-D_FORTIFY_SOURCE=2", |
| 32 | "-fstack-protector-strong", |
| 33 | |
| 34 | // From x86_64_device |
| 35 | "-ffunction-sections", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 36 | "-fno-short-enums", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 37 | "-funwind-tables", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 38 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 39 | // Tell clang where the gcc toolchain is |
| 40 | "--gcc-toolchain=${LinuxBionicGccRoot}", |
| 41 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 42 | // This is normally in ClangExtraTargetCflags, but this is considered host |
| 43 | "-nostdlibinc", |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 44 | } |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 45 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 46 | linuxBionicLdflags = []string{ |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 47 | "-Wl,-z,noexecstack", |
| 48 | "-Wl,-z,relro", |
| 49 | "-Wl,-z,now", |
| 50 | "-Wl,--build-id=md5", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 51 | "-Wl,--fatal-warnings", |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 52 | "-Wl,--no-undefined-version", |
| 53 | |
| 54 | // Use the device gcc toolchain |
| 55 | "--gcc-toolchain=${LinuxBionicGccRoot}", |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 56 | } |
| Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 57 | |
| Eric Rahm | 1952471 | 2023-10-20 15:56:27 +0000 | [diff] [blame] | 58 | linuxBionicLldflags = append(linuxBionicLdflags, |
| 59 | "-Wl,--compress-debug-sections=zstd", |
| 60 | ) |
| 61 | |
| Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 62 | // 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 Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 71 | ) |
| 72 | |
| Colin Cross | 4fa894d | 2022-09-30 15:44:45 -0700 | [diff] [blame] | 73 | const ( |
| 74 | x86_64GccVersion = "4.9" |
| 75 | ) |
| 76 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 77 | func init() { |
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 78 | pctx.StaticVariable("LinuxBionicCflags", strings.Join(linuxBionicCflags, " ")) |
| 79 | pctx.StaticVariable("LinuxBionicLdflags", strings.Join(linuxBionicLdflags, " ")) |
| 80 | pctx.StaticVariable("LinuxBionicLldflags", strings.Join(linuxBionicLldflags, " ")) |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 81 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 82 | // Use the device gcc toolchain for now |
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 83 | pctx.StaticVariable("LinuxBionicGccVersion", x86_64GccVersion) |
| 84 | pctx.SourcePathVariable("LinuxBionicGccRoot", |
| Colin Cross | 4fa894d | 2022-09-30 15:44:45 -0700 | [diff] [blame] | 85 | "prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${LinuxBionicGccVersion}") |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | type toolchainLinuxBionic struct { |
| 89 | toolchain64Bit |
| Colin Cross | e3fee34 | 2021-06-21 17:28:25 -0700 | [diff] [blame] | 90 | toolchainBionic |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (t *toolchainLinuxBionic) Name() string { |
| 94 | return "x86_64" |
| 95 | } |
| 96 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 97 | func (t *toolchainLinuxBionic) IncludeFlags() string { |
| Martin Stjernholm | 41ab251 | 2020-04-08 01:06:07 +0100 | [diff] [blame] | 98 | return "" |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (t *toolchainLinuxBionic) ClangTriple() string { |
| 102 | // TODO: we don't have a triple yet b/31393676 |
| 103 | return "x86_64-linux-android" |
| 104 | } |
| 105 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 106 | func (t *toolchainLinuxBionic) Cflags() string { |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 107 | return "${config.LinuxBionicCflags}" |
| 108 | } |
| 109 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 110 | func (t *toolchainLinuxBionic) Cppflags() string { |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 111 | return "" |
| 112 | } |
| 113 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 114 | func (t *toolchainLinuxBionic) Ldflags() string { |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 115 | return "${config.LinuxBionicLdflags}" |
| 116 | } |
| 117 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 118 | func (t *toolchainLinuxBionic) Lldflags() string { |
| Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 119 | return "${config.LinuxBionicLldflags}" |
| Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 122 | func (t *toolchainLinuxBionic) ToolchainCflags() string { |
| Dan Willemsen | 38394b9 | 2017-09-18 22:50:22 -0700 | [diff] [blame] | 123 | return "-m64 -march=x86-64" + |
| 124 | // TODO: We're not really android, but we don't have a triple yet b/31393676 |
| Alex Light | a08ae84 | 2018-10-31 11:18:22 -0700 | [diff] [blame] | 125 | " -U__ANDROID__" |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 128 | func (t *toolchainLinuxBionic) ToolchainLdflags() string { |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 129 | return "-m64" |
| 130 | } |
| 131 | |
| 132 | func (t *toolchainLinuxBionic) AvailableLibraries() []string { |
| 133 | return nil |
| 134 | } |
| 135 | |
| Dan Willemsen | 9ff34c0 | 2018-10-10 17:58:19 -0700 | [diff] [blame] | 136 | func (toolchainLinuxBionic) LibclangRuntimeLibraryArch() string { |
| 137 | return "x86_64" |
| 138 | } |
| 139 | |
| Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 140 | func (toolchainLinuxBionic) CrtBeginSharedBinary() []string { |
| 141 | return linuxBionicCrtBeginSharedBinary |
| 142 | } |
| 143 | |
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 144 | var toolchainLinuxBionicSingleton Toolchain = &toolchainLinuxBionic{} |
| 145 | |
| 146 | func linuxBionicToolchainFactory(arch android.Arch) Toolchain { |
| 147 | return toolchainLinuxBionicSingleton |
| 148 | } |
| 149 | |
| 150 | func init() { |
| 151 | registerToolchainFactory(android.LinuxBionic, android.X86_64, linuxBionicToolchainFactory) |
| 152 | } |