Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -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 cc |
| 16 | |
| 17 | import ( |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 18 | "path/filepath" |
| 19 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | type BinaryLinkerProperties struct { |
| 26 | // compile executable with -static |
| 27 | Static_executable *bool `android:"arch_variant"` |
| 28 | |
| 29 | // set the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 30 | Stem *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 31 | |
| 32 | // append to the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 33 | Suffix *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 34 | |
| 35 | // if set, add an extra objcopy --prefix-symbols= step |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 36 | Prefix_symbols *string |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 37 | |
| 38 | // if set, install a symlink to the preferred architecture |
Roland Levillain | d9bf9be | 2019-06-05 19:20:33 +0100 | [diff] [blame] | 39 | Symlink_preferred_arch *bool `android:"arch_variant"` |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 40 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 41 | // install symlinks to the binary. Symlink names will have the suffix and the binary |
| 42 | // extension (if any) appended |
| 43 | Symlinks []string `android:"arch_variant"` |
| 44 | |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 45 | DynamicLinker string `blueprint:"mutated"` |
Yifan Hong | 946e32e | 2018-04-03 13:22:50 -0700 | [diff] [blame] | 46 | |
| 47 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 48 | // (in Make or Soong). |
| 49 | // This does not completely prevent installation of the overridden binaries, but if both |
| 50 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 51 | // from PRODUCT_PACKAGES. |
| 52 | Overrides []string |
Colin Cross | d7227f9 | 2019-09-05 14:26:33 -0700 | [diff] [blame] | 53 | |
| 54 | // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl. |
| 55 | Inject_bssl_hash *bool `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func init() { |
Paul Duffin | 2ee6979 | 2020-01-16 12:14:42 +0000 | [diff] [blame] | 59 | RegisterBinaryBuildComponents(android.InitRegistrationContext) |
| 60 | } |
| 61 | |
| 62 | func RegisterBinaryBuildComponents(ctx android.RegistrationContext) { |
| 63 | ctx.RegisterModuleType("cc_binary", BinaryFactory) |
| 64 | ctx.RegisterModuleType("cc_binary_host", binaryHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 67 | // cc_binary produces a binary that is runnable on a device. |
Jiyong Park | 16e91a0 | 2018-12-20 18:18:08 +0900 | [diff] [blame] | 68 | func BinaryFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 69 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 70 | return module.Init() |
| 71 | } |
| 72 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 73 | // cc_binary_host produces a binary that is runnable on a host. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 74 | func binaryHostFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 75 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 76 | return module.Init() |
| 77 | } |
| 78 | |
| 79 | // |
| 80 | // Executables |
| 81 | // |
| 82 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 83 | type binaryDecorator struct { |
| 84 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 85 | *baseInstaller |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 86 | stripper Stripper |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 87 | |
| 88 | Properties BinaryLinkerProperties |
| 89 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 90 | toolPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 91 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 92 | // Location of the linked, unstripped binary |
| 93 | unstrippedOutputFile android.Path |
| 94 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 95 | // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS |
| 96 | symlinks []string |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 97 | |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame^] | 98 | // If the module has symlink_preferred_arch set, the name of the symlink to the |
| 99 | // binary for the preferred arch. |
| 100 | preferredArchSymlink string |
| 101 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 102 | // Output archive of gcno coverage information |
| 103 | coverageOutputFile android.OptionalPath |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 104 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 105 | // Location of the files that should be copied to dist dir when requested |
| 106 | distFiles android.TaggedDistFiles |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 107 | |
| 108 | post_install_cmds []string |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 111 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 112 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 113 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 114 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | &binary.Properties, |
| 116 | &binary.stripper.StripProperties) |
| 117 | |
| 118 | } |
| 119 | |
Roland Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 120 | func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 121 | stem := ctx.baseModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 122 | if String(binary.Properties.Stem) != "" { |
| 123 | stem = String(binary.Properties.Stem) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Roland Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 126 | return stem |
| 127 | } |
| 128 | |
| 129 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
| 130 | return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 133 | func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 134 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 135 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 136 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 137 | if binary.static() { |
| 138 | deps.CrtBegin = "crtbegin_static" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | } else { |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 140 | deps.CrtBegin = "crtbegin_dynamic" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | } |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 142 | deps.CrtEnd = "crtend_android" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 145 | if binary.static() { |
Dan Albert | dc2597d | 2017-01-26 17:44:26 -0800 | [diff] [blame] | 146 | if ctx.selectedStl() == "libc++_static" { |
Ryan Prichard | b49fe1b | 2019-10-11 15:03:34 -0700 | [diff] [blame] | 147 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 148 | } |
| 149 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with |
| 150 | // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, |
| 151 | // move them to the beginning of deps.LateStaticLibs |
| 152 | var groupLibs []string |
| 153 | deps.StaticLibs, groupLibs = filterList(deps.StaticLibs, |
| 154 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) |
| 155 | deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...) |
| 156 | } |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 157 | |
| 158 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 159 | deps.DynamicLinker = "linker" |
| 160 | deps.LinkerFlagsFile = "host_bionic_linker_flags" |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 161 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 164 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 165 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 166 | "from static libs or set static_executable: true") |
| 167 | } |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 168 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 169 | return deps |
| 170 | } |
| 171 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 172 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 173 | return true |
| 174 | } |
| 175 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 176 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 177 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 178 | binary := &binaryDecorator{ |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 179 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 180 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 181 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 182 | module.compiler = NewBaseCompiler() |
| 183 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 184 | module.installer = binary |
Paul Duffin | 25ce04b | 2020-01-16 11:47:25 +0000 | [diff] [blame] | 185 | |
| 186 | // Allow module to be added as member of an sdk/module_exports. |
| 187 | module.sdkMemberTypes = []android.SdkMemberType{ |
| 188 | ccBinarySdkMemberType, |
| 189 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 190 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 193 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 194 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 195 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 196 | if !ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 197 | if ctx.Os() == android.Linux { |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 198 | if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 199 | binary.Properties.Static_executable = BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | } |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 201 | } else if !ctx.Fuchsia() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 202 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 203 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 208 | func (binary *binaryDecorator) static() bool { |
| 209 | return Bool(binary.Properties.Static_executable) |
| 210 | } |
| 211 | |
| 212 | func (binary *binaryDecorator) staticBinary() bool { |
| 213 | return binary.static() |
| 214 | } |
| 215 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 216 | func (binary *binaryDecorator) binary() bool { |
| 217 | return true |
| 218 | } |
| 219 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 220 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 221 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 222 | |
Colin Cross | 446c666 | 2018-09-14 16:00:16 -0700 | [diff] [blame] | 223 | if ctx.Host() && !ctx.Windows() && !binary.static() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 224 | if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 225 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 230 | // all code is position independent, and then those warnings get promoted to |
| 231 | // errors. |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 232 | if !ctx.Windows() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 233 | flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 236 | if ctx.toolchain().Bionic() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 237 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 238 | // Clang driver needs -static to create static executable. |
| 239 | // However, bionic/linker uses -shared to overwrite. |
| 240 | // Linker for x86 targets does not allow coexistance of -static and -shared, |
| 241 | // so we add -static only if -shared is not used. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 242 | if !inList("-shared", flags.Local.LdFlags) { |
| 243 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-static") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 246 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 247 | "-nostdlib", |
| 248 | "-Bstatic", |
| 249 | "-Wl,--gc-sections", |
| 250 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 251 | } else { |
| 252 | if flags.DynamicLinker == "" { |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 253 | if binary.Properties.DynamicLinker != "" { |
| 254 | flags.DynamicLinker = binary.Properties.DynamicLinker |
| 255 | } else { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 256 | switch ctx.Os() { |
| 257 | case android.Android: |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 258 | if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() { |
Jiyong Park | a4b9dd0 | 2019-01-16 22:53:13 +0900 | [diff] [blame] | 259 | flags.DynamicLinker = "/system/bin/bootstrap/linker" |
| 260 | } else { |
| 261 | flags.DynamicLinker = "/system/bin/linker" |
| 262 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 263 | if flags.Toolchain.Is64Bit() { |
| 264 | flags.DynamicLinker += "64" |
| 265 | } |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 266 | case android.LinuxBionic: |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 267 | flags.DynamicLinker = "" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 268 | default: |
| 269 | ctx.ModuleErrorf("unknown dynamic linker") |
| 270 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if ctx.Os() == android.LinuxBionic { |
| 274 | // Use the dlwrap entry point, but keep _start around so |
| 275 | // that it can be used by host_bionic_inject |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 276 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 277 | "-Wl,--entry=__dlwrap__start", |
| 278 | "-Wl,--undefined=_start", |
| 279 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 283 | flags.Global.LdFlags = append(flags.Global.LdFlags, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 284 | "-pie", |
| 285 | "-nostdlib", |
| 286 | "-Bdynamic", |
| 287 | "-Wl,--gc-sections", |
| 288 | "-Wl,-z,nocopyreloc", |
| 289 | ) |
| 290 | } |
| 291 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 292 | if binary.static() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 293 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-static") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 294 | } |
| 295 | if ctx.Darwin() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 296 | flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | return flags |
| 301 | } |
| 302 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 303 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 304 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 305 | |
| 306 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 307 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 308 | ret := outputFile |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 309 | |
| 310 | var linkerDeps android.Paths |
| 311 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 312 | if deps.LinkerFlagsFile.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 313 | flags.Local.LdFlags = append(flags.Local.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")") |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 314 | linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path()) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 317 | if flags.DynamicLinker != "" { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 318 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker) |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 319 | } else if ctx.toolchain().Bionic() && !binary.static() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 320 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | builderFlags := flagsToBuilderFlags(flags) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 324 | stripFlags := flagsToStripFlags(flags) |
| 325 | if binary.stripper.NeedsStrip(ctx) { |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 326 | if ctx.Darwin() { |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 327 | stripFlags.StripUseGnuStrip = true |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 328 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 329 | strippedOutputFile := outputFile |
| 330 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 331 | binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 334 | binary.unstrippedOutputFile = outputFile |
| 335 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 336 | if String(binary.Properties.Prefix_symbols) != "" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 337 | afterPrefixSymbols := outputFile |
| 338 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 339 | TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile, |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 340 | builderFlags, afterPrefixSymbols) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Colin Cross | d7227f9 | 2019-09-05 14:26:33 -0700 | [diff] [blame] | 343 | outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName) |
| 344 | |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 345 | if Bool(binary.baseLinker.Properties.Use_version_lib) { |
| 346 | if ctx.Host() { |
| 347 | versionedOutputFile := outputFile |
| 348 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 349 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 350 | } else { |
| 351 | versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 352 | binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile) |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 353 | |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 354 | if binary.stripper.NeedsStrip(ctx) { |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 355 | out := android.PathForModuleOut(ctx, "versioned-stripped", fileName) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 356 | binary.distFiles = android.MakeDefaultDistFiles(out) |
ThiƩbaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 357 | binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags) |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 361 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 364 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
| 365 | injectedOutputFile := outputFile |
| 366 | outputFile = android.PathForModuleOut(ctx, "prelinker", fileName) |
| 367 | |
| 368 | if !deps.DynamicLinker.Valid() { |
| 369 | panic("Non-static host bionic modules must have a dynamic linker") |
| 370 | } |
| 371 | |
| 372 | binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile) |
| 373 | } |
| 374 | |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 375 | var sharedLibs android.Paths |
| 376 | // Ignore shared libs for static executables. |
| 377 | if !binary.static() { |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 378 | sharedLibs = deps.EarlySharedLibs |
| 379 | sharedLibs = append(sharedLibs, deps.SharedLibs...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 380 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 381 | linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 382 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 383 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
| 384 | } |
| 385 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 386 | linkerDeps = append(linkerDeps, objs.tidyFiles...) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 387 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 388 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 389 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 390 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
Josh Gao | 75a50a2 | 2019-06-07 17:58:59 -0700 | [diff] [blame] | 391 | builderFlags, outputFile, nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 392 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 393 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) |
| 394 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) |
Oliver Nguyen | c743414 | 2019-04-24 14:22:25 -0700 | [diff] [blame] | 395 | binary.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, binary.getStem(ctx)) |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 396 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 397 | // Need to determine symlinks early since some targets (ie APEX) need this |
| 398 | // information but will not call 'install' |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 399 | for _, symlink := range binary.Properties.Symlinks { |
| 400 | binary.symlinks = append(binary.symlinks, |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 401 | symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix()) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 404 | if Bool(binary.Properties.Symlink_preferred_arch) { |
Roland Levillain | 9efb8c7 | 2019-06-05 13:31:31 +0100 | [diff] [blame] | 405 | if String(binary.Properties.Suffix) == "" { |
| 406 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix") |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 407 | } |
| 408 | if ctx.TargetPrimary() { |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame^] | 409 | symlinkName := binary.getStemWithoutSuffix(ctx) |
| 410 | binary.symlinks = append(binary.symlinks, symlinkName) |
| 411 | binary.preferredArchSymlink = symlinkName |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 415 | return ret |
| 416 | } |
| 417 | |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 418 | func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path { |
| 419 | return binary.unstrippedOutputFile |
| 420 | } |
| 421 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 422 | func (binary *binaryDecorator) symlinkList() []string { |
| 423 | return binary.symlinks |
| 424 | } |
| 425 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 426 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 427 | return true |
| 428 | } |
| 429 | |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 430 | func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath { |
| 431 | return binary.coverageOutputFile |
| 432 | } |
| 433 | |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 434 | // /system/bin/linker -> /apex/com.android.runtime/bin/linker |
| 435 | func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) { |
| 436 | dir := binary.baseInstaller.installDir(ctx) |
| 437 | dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir) |
| 438 | target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base()) |
| 439 | |
| 440 | ctx.InstallAbsoluteSymlink(dir, file.Base(), target) |
| 441 | binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target)) |
| 442 | |
| 443 | for _, symlink := range binary.symlinks { |
| 444 | ctx.InstallAbsoluteSymlink(dir, symlink, target) |
| 445 | binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target)) |
| 446 | } |
| 447 | } |
| 448 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 449 | func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) { |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 450 | // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory. |
| 451 | // The original path becomes a symlink to the corresponding file in the |
| 452 | // runtime APEX. |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 453 | translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 454 | if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() && |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 455 | !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() && |
| 456 | !ctx.inVendorRamdisk() { |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 457 | |
Peter Collingbourne | 3478bb2 | 2019-04-24 14:41:12 -0700 | [diff] [blame] | 458 | if ctx.Device() && isBionic(ctx.baseModuleName()) { |
Jiyong Park | c3e2c86 | 2019-03-16 01:10:08 +0900 | [diff] [blame] | 459 | binary.installSymlinkToRuntimeApex(ctx, file) |
| 460 | } |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 461 | binary.baseInstaller.subDir = "bootstrap" |
| 462 | } |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 463 | binary.baseInstaller.install(ctx, file) |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame^] | 464 | |
| 465 | var preferredArchSymlinkPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 466 | for _, symlink := range binary.symlinks { |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame^] | 467 | installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, |
| 468 | binary.baseInstaller.path) |
| 469 | if symlink == binary.preferredArchSymlink { |
| 470 | // If this is the preferred arch symlink, save the installed path for use as the |
| 471 | // tool path. |
| 472 | preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink) |
| 473 | } |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 476 | if ctx.Os().Class == android.Host { |
Colin Cross | a615901 | 2020-11-12 12:14:36 -0800 | [diff] [blame^] | 477 | // If the binary is multilib with a symlink to the preferred architecture, use the |
| 478 | // symlink instead of the binary because that's the more "canonical" name. |
| 479 | if preferredArchSymlinkPath.Valid() { |
| 480 | binary.toolPath = preferredArchSymlinkPath |
| 481 | } else { |
| 482 | binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path) |
| 483 | } |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 488 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 489 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 490 | |
| 491 | func init() { |
| 492 | pctx.HostBinToolVariable("hostBionicSymbolsInjectCmd", "host_bionic_inject") |
| 493 | } |
| 494 | |
| 495 | var injectHostBionicSymbols = pctx.AndroidStaticRule("injectHostBionicSymbols", |
| 496 | blueprint.RuleParams{ |
| 497 | Command: "$hostBionicSymbolsInjectCmd -i $in -l $linker -o $out", |
| 498 | CommandDeps: []string{"$hostBionicSymbolsInjectCmd"}, |
| 499 | }, "linker") |
| 500 | |
| 501 | func (binary *binaryDecorator) injectHostBionicLinkerSymbols(ctx ModuleContext, in, linker android.Path, out android.WritablePath) { |
| 502 | ctx.Build(pctx, android.BuildParams{ |
| 503 | Rule: injectHostBionicSymbols, |
| 504 | Description: "inject host bionic symbols", |
| 505 | Input: in, |
| 506 | Implicit: linker, |
| 507 | Output: out, |
| 508 | Args: map[string]string{ |
| 509 | "linker": linker.String(), |
| 510 | }, |
| 511 | }) |
| 512 | } |