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 ( |
| 18 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var ( |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 24 | linuxBionicCflags = []string{ |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 25 | "-Wa,--noexecstack", |
| 26 | |
| 27 | "-fPIC", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 28 | |
| 29 | "-U_FORTIFY_SOURCE", |
| 30 | "-D_FORTIFY_SOURCE=2", |
| 31 | "-fstack-protector-strong", |
| 32 | |
| 33 | // From x86_64_device |
| 34 | "-ffunction-sections", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 35 | "-fno-short-enums", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 36 | "-funwind-tables", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 37 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 38 | // Tell clang where the gcc toolchain is |
| 39 | "--gcc-toolchain=${LinuxBionicGccRoot}", |
| 40 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 41 | // This is normally in ClangExtraTargetCflags, but this is considered host |
| 42 | "-nostdlibinc", |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 43 | } |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 44 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 45 | linuxBionicLdflags = []string{ |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 46 | "-Wl,-z,noexecstack", |
| 47 | "-Wl,-z,relro", |
| 48 | "-Wl,-z,now", |
| 49 | "-Wl,--build-id=md5", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 50 | "-Wl,--fatal-warnings", |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 51 | "-Wl,--hash-style=gnu", |
| 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 | |
| 58 | // Embed the linker into host bionic binaries. This is needed to support host bionic, |
| 59 | // as the linux kernel requires that the ELF interpreter referenced by PT_INTERP be |
| 60 | // either an absolute path, or relative from CWD. To work around this, we extract |
| 61 | // the load sections from the runtime linker ELF binary and embed them into each host |
| 62 | // bionic binary, omitting the PT_INTERP declaration. The kernel will treat it as a static |
| 63 | // binary, and then we use a special entry point to fix up the arguments passed by |
| 64 | // the kernel before jumping to the embedded linker. |
| 65 | linuxBionicCrtBeginSharedBinary = append(android.CopyOf(bionicCrtBeginSharedBinary), |
| 66 | "host_bionic_linker_script") |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 67 | ) |
| 68 | |
Colin Cross | 4fa894d | 2022-09-30 15:44:45 -0700 | [diff] [blame^] | 69 | const ( |
| 70 | x86_64GccVersion = "4.9" |
| 71 | ) |
| 72 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 73 | func init() { |
Colin Cross | 4fa894d | 2022-09-30 15:44:45 -0700 | [diff] [blame^] | 74 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 75 | pctx.StaticVariable("LinuxBionicCflags", strings.Join(linuxBionicCflags, " ")) |
| 76 | pctx.StaticVariable("LinuxBionicLdflags", strings.Join(linuxBionicLdflags, " ")) |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 77 | pctx.StaticVariable("LinuxBionicLldflags", strings.Join(linuxBionicLdflags, " ")) |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 78 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 79 | // Use the device gcc toolchain for now |
Colin Cross | 4fa894d | 2022-09-30 15:44:45 -0700 | [diff] [blame^] | 80 | pctx.StaticVariable("LinuxBionicGccVersion", x86_64GccVersion) |
| 81 | pctx.SourcePathVariable("LinuxBionicGccRoot", |
| 82 | "prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${LinuxBionicGccVersion}") |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | type toolchainLinuxBionic struct { |
| 86 | toolchain64Bit |
Colin Cross | e3fee34 | 2021-06-21 17:28:25 -0700 | [diff] [blame] | 87 | toolchainBionic |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func (t *toolchainLinuxBionic) Name() string { |
| 91 | return "x86_64" |
| 92 | } |
| 93 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 94 | func (t *toolchainLinuxBionic) IncludeFlags() string { |
Martin Stjernholm | 41ab251 | 2020-04-08 01:06:07 +0100 | [diff] [blame] | 95 | return "" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func (t *toolchainLinuxBionic) ClangTriple() string { |
| 99 | // TODO: we don't have a triple yet b/31393676 |
| 100 | return "x86_64-linux-android" |
| 101 | } |
| 102 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 103 | func (t *toolchainLinuxBionic) Cflags() string { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 104 | return "${config.LinuxBionicCflags}" |
| 105 | } |
| 106 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 107 | func (t *toolchainLinuxBionic) Cppflags() string { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 108 | return "" |
| 109 | } |
| 110 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 111 | func (t *toolchainLinuxBionic) Ldflags() string { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 112 | return "${config.LinuxBionicLdflags}" |
| 113 | } |
| 114 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 115 | func (t *toolchainLinuxBionic) Lldflags() string { |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame] | 116 | return "${config.LinuxBionicLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 119 | func (t *toolchainLinuxBionic) ToolchainCflags() string { |
Dan Willemsen | 38394b9 | 2017-09-18 22:50:22 -0700 | [diff] [blame] | 120 | return "-m64 -march=x86-64" + |
| 121 | // 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] | 122 | " -U__ANDROID__" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 125 | func (t *toolchainLinuxBionic) ToolchainLdflags() string { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 126 | return "-m64" |
| 127 | } |
| 128 | |
| 129 | func (t *toolchainLinuxBionic) AvailableLibraries() []string { |
| 130 | return nil |
| 131 | } |
| 132 | |
Dan Willemsen | 9ff34c0 | 2018-10-10 17:58:19 -0700 | [diff] [blame] | 133 | func (toolchainLinuxBionic) LibclangRuntimeLibraryArch() string { |
| 134 | return "x86_64" |
| 135 | } |
| 136 | |
Colin Cross | d1a2813 | 2021-06-21 17:34:47 -0700 | [diff] [blame] | 137 | func (toolchainLinuxBionic) CrtBeginSharedBinary() []string { |
| 138 | return linuxBionicCrtBeginSharedBinary |
| 139 | } |
| 140 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 141 | var toolchainLinuxBionicSingleton Toolchain = &toolchainLinuxBionic{} |
| 142 | |
| 143 | func linuxBionicToolchainFactory(arch android.Arch) Toolchain { |
| 144 | return toolchainLinuxBionicSingleton |
| 145 | } |
| 146 | |
| 147 | func init() { |
| 148 | registerToolchainFactory(android.LinuxBionic, android.X86_64, linuxBionicToolchainFactory) |
| 149 | } |