blob: 70cf5ffaddf4327eadbc60707f5bf5bb88c207f8 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -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 cc
16
17import (
Jiyong Parkf1194352019-02-25 11:05:47 +090018 "path/filepath"
19
Dan Willemsena0790e32018-10-12 00:24:23 -070020 "github.com/google/blueprint"
21
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "android/soong/android"
23)
24
25type BinaryLinkerProperties struct {
26 // compile executable with -static
27 Static_executable *bool `android:"arch_variant"`
28
29 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080030 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070031
32 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080033 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
35 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080036 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070037
38 // if set, install a symlink to the preferred architecture
Roland Levillaind9bf9be2019-06-05 19:20:33 +010039 Symlink_preferred_arch *bool `android:"arch_variant"`
Colin Cross522e3732016-09-07 13:14:06 -070040
Colin Cross9b09f242016-12-07 13:37:42 -080041 // 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 Cross522e3732016-09-07 13:14:06 -070045 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070046
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 Crossd7227f92019-09-05 14:26:33 -070053
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 Cross4d9c2d12016-07-29 12:48:20 -070056}
57
58func init() {
Paul Duffin2ee69792020-01-16 12:14:42 +000059 RegisterBinaryBuildComponents(android.InitRegistrationContext)
60}
61
62func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
63 ctx.RegisterModuleType("cc_binary", BinaryFactory)
64 ctx.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070065}
66
Patrice Arrudac249c712019-03-19 17:00:29 -070067// cc_binary produces a binary that is runnable on a device.
Jiyong Park16e91a02018-12-20 18:18:08 +090068func BinaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070069 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070070 return module.Init()
71}
72
Patrice Arrudac249c712019-03-19 17:00:29 -070073// cc_binary_host produces a binary that is runnable on a host.
Colin Cross36242852017-06-23 15:06:31 -070074func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070075 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 return module.Init()
77}
78
79//
80// Executables
81//
82
Colin Crossb916a382016-07-29 17:28:03 -070083type binaryDecorator struct {
84 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070085 *baseInstaller
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +020086 stripper Stripper
Colin Cross4d9c2d12016-07-29 12:48:20 -070087
88 Properties BinaryLinkerProperties
89
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070090 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080091
Colin Crossb60190a2018-09-04 16:28:17 -070092 // Location of the linked, unstripped binary
93 unstrippedOutputFile android.Path
94
Colin Cross9b09f242016-12-07 13:37:42 -080095 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
96 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080097
98 // Output archive of gcno coverage information
99 coverageOutputFile android.OptionalPath
Dan Willemsen569edc52018-11-19 09:33:29 -0800100
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000101 // Location of the files that should be copied to dist dir when requested
102 distFiles android.TaggedDistFiles
Jiyong Parkf1194352019-02-25 11:05:47 +0900103
104 post_install_cmds []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105}
106
Colin Crossb916a382016-07-29 17:28:03 -0700107var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108
Colin Crossb916a382016-07-29 17:28:03 -0700109func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -0700110 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 &binary.Properties,
112 &binary.stripper.StripProperties)
113
114}
115
Roland Levillain9efb8c72019-06-05 13:31:31 +0100116func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700117 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800118 if String(binary.Properties.Stem) != "" {
119 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 }
121
Roland Levillain9efb8c72019-06-05 13:31:31 +0100122 return stem
123}
124
125func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
126 return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127}
128
Colin Cross37047f12016-12-13 17:06:13 -0800129func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700130 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800131 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132 if !Bool(binary.baseLinker.Properties.Nocrt) {
Dan Albert92fe7402020-07-15 13:33:30 -0700133 if binary.static() {
134 deps.CrtBegin = "crtbegin_static"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 } else {
Dan Albert92fe7402020-07-15 13:33:30 -0700136 deps.CrtBegin = "crtbegin_dynamic"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137 }
Dan Albert92fe7402020-07-15 13:33:30 -0700138 deps.CrtEnd = "crtend_android"
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 }
140
Colin Crossb916a382016-07-29 17:28:03 -0700141 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800142 if ctx.selectedStl() == "libc++_static" {
Ryan Prichardb49fe1b2019-10-11 15:03:34 -0700143 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 }
145 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
146 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
147 // move them to the beginning of deps.LateStaticLibs
148 var groupLibs []string
149 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
150 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
151 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
152 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700153
154 if ctx.Os() == android.LinuxBionic && !binary.static() {
Dan Willemsena0790e32018-10-12 00:24:23 -0700155 deps.DynamicLinker = "linker"
156 deps.LinkerFlagsFile = "host_bionic_linker_flags"
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700157 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 }
159
Colin Crossb916a382016-07-29 17:28:03 -0700160 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
162 "from static libs or set static_executable: true")
163 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800164
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 return deps
166}
167
Colin Crossb916a382016-07-29 17:28:03 -0700168func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 return true
170}
171
Colin Crossb916a382016-07-29 17:28:03 -0700172func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700174 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700175 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700176 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 }
Colin Crossb916a382016-07-29 17:28:03 -0700178 module.compiler = NewBaseCompiler()
179 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700180 module.installer = binary
Paul Duffin25ce04b2020-01-16 11:47:25 +0000181
182 // Allow module to be added as member of an sdk/module_exports.
183 module.sdkMemberTypes = []android.SdkMemberType{
184 ccBinarySdkMemberType,
185 }
Colin Crossb916a382016-07-29 17:28:03 -0700186 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187}
188
Colin Crossb916a382016-07-29 17:28:03 -0700189func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700190 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191
Dan Willemsen2e47b342016-11-17 01:02:25 -0800192 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 if ctx.Os() == android.Linux {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700194 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800195 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 }
Doug Hornc32c6b02019-01-17 14:44:05 -0800197 } else if !ctx.Fuchsia() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700199 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 }
201 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202}
203
Colin Crossb916a382016-07-29 17:28:03 -0700204func (binary *binaryDecorator) static() bool {
205 return Bool(binary.Properties.Static_executable)
206}
207
208func (binary *binaryDecorator) staticBinary() bool {
209 return binary.static()
210}
211
Inseob Kim7f283f42020-06-01 21:53:49 +0900212func (binary *binaryDecorator) binary() bool {
213 return true
214}
215
Colin Crossb916a382016-07-29 17:28:03 -0700216func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700217 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218
Colin Cross446c6662018-09-14 16:00:16 -0700219 if ctx.Host() && !ctx.Windows() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800220 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross4af21ed2019-11-04 09:37:55 -0800221 flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223 }
224
225 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
226 // all code is position independent, and then those warnings get promoted to
227 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700228 if !ctx.Windows() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800229 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 }
231
Dan Willemsen2e47b342016-11-17 01:02:25 -0800232 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700233 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 // Clang driver needs -static to create static executable.
235 // However, bionic/linker uses -shared to overwrite.
236 // Linker for x86 targets does not allow coexistance of -static and -shared,
237 // so we add -static only if -shared is not used.
Colin Cross4af21ed2019-11-04 09:37:55 -0800238 if !inList("-shared", flags.Local.LdFlags) {
239 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 }
241
Colin Cross4af21ed2019-11-04 09:37:55 -0800242 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 "-nostdlib",
244 "-Bstatic",
245 "-Wl,--gc-sections",
246 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 } else {
248 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700249 if binary.Properties.DynamicLinker != "" {
250 flags.DynamicLinker = binary.Properties.DynamicLinker
251 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700252 switch ctx.Os() {
253 case android.Android:
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700254 if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
Jiyong Parka4b9dd02019-01-16 22:53:13 +0900255 flags.DynamicLinker = "/system/bin/bootstrap/linker"
256 } else {
257 flags.DynamicLinker = "/system/bin/linker"
258 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700259 if flags.Toolchain.Is64Bit() {
260 flags.DynamicLinker += "64"
261 }
Dan Willemsen01a405a2016-06-13 17:19:03 -0700262 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700263 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700264 default:
265 ctx.ModuleErrorf("unknown dynamic linker")
266 }
Dan Willemsena0790e32018-10-12 00:24:23 -0700267 }
268
269 if ctx.Os() == android.LinuxBionic {
270 // Use the dlwrap entry point, but keep _start around so
271 // that it can be used by host_bionic_inject
Colin Cross4af21ed2019-11-04 09:37:55 -0800272 flags.Global.LdFlags = append(flags.Global.LdFlags,
Dan Willemsena0790e32018-10-12 00:24:23 -0700273 "-Wl,--entry=__dlwrap__start",
274 "-Wl,--undefined=_start",
275 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 }
277 }
278
Colin Cross4af21ed2019-11-04 09:37:55 -0800279 flags.Global.LdFlags = append(flags.Global.LdFlags,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 "-pie",
281 "-nostdlib",
282 "-Bdynamic",
283 "-Wl,--gc-sections",
284 "-Wl,-z,nocopyreloc",
285 )
286 }
287 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700288 if binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800289 flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 }
291 if ctx.Darwin() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800292 flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 }
294 }
295
296 return flags
297}
298
Colin Crossb916a382016-07-29 17:28:03 -0700299func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700300 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301
302 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
303 outputFile := android.PathForModuleOut(ctx, fileName)
304 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305
306 var linkerDeps android.Paths
307
Dan Willemsena0790e32018-10-12 00:24:23 -0700308 if deps.LinkerFlagsFile.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800309 flags.Local.LdFlags = append(flags.Local.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")")
Dan Willemsena0790e32018-10-12 00:24:23 -0700310 linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path())
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700311 }
312
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 if flags.DynamicLinker != "" {
Colin Cross4af21ed2019-11-04 09:37:55 -0800314 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
Dan Willemsena0790e32018-10-12 00:24:23 -0700315 } else if ctx.toolchain().Bionic() && !binary.static() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800316 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317 }
318
319 builderFlags := flagsToBuilderFlags(flags)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200320 stripFlags := flagsToStripFlags(flags)
321 if binary.stripper.NeedsStrip(ctx) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800322 if ctx.Darwin() {
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200323 stripFlags.StripUseGnuStrip = true
Yi Kongb5c34d72018-11-07 16:28:49 -0800324 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 strippedOutputFile := outputFile
326 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200327 binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 }
329
Colin Crossb60190a2018-09-04 16:28:17 -0700330 binary.unstrippedOutputFile = outputFile
331
Nan Zhang0007d812017-11-07 10:57:05 -0800332 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700333 afterPrefixSymbols := outputFile
334 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800335 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200336 builderFlags, afterPrefixSymbols)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 }
338
Colin Crossd7227f92019-09-05 14:26:33 -0700339 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
340
Dan Willemsen569edc52018-11-19 09:33:29 -0800341 if Bool(binary.baseLinker.Properties.Use_version_lib) {
342 if ctx.Host() {
343 versionedOutputFile := outputFile
344 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
345 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
346 } else {
347 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000348 binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
Dan Willemsen569edc52018-11-19 09:33:29 -0800349
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200350 if binary.stripper.NeedsStrip(ctx) {
Dan Willemsen569edc52018-11-19 09:33:29 -0800351 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000352 binary.distFiles = android.MakeDefaultDistFiles(out)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200353 binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
Dan Willemsen569edc52018-11-19 09:33:29 -0800354 }
355
356 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
357 }
Colin Cross86803cf2018-02-15 14:12:26 -0800358 }
359
Dan Willemsena0790e32018-10-12 00:24:23 -0700360 if ctx.Os() == android.LinuxBionic && !binary.static() {
361 injectedOutputFile := outputFile
362 outputFile = android.PathForModuleOut(ctx, "prelinker", fileName)
363
364 if !deps.DynamicLinker.Valid() {
365 panic("Non-static host bionic modules must have a dynamic linker")
366 }
367
368 binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile)
369 }
370
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800371 var sharedLibs android.Paths
372 // Ignore shared libs for static executables.
373 if !binary.static() {
Jiyong Park64a44f22019-01-18 14:37:08 +0900374 sharedLibs = deps.EarlySharedLibs
375 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800376 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Jiyong Park64a44f22019-01-18 14:37:08 +0900377 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Jaewoong Jung232c07c2018-12-18 11:08:25 -0800378 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
379 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
380 }
381
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700382 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700383 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700384
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700385 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Josh Gao75a50a22019-06-07 17:58:59 -0700387 builderFlags, outputFile, nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700388
Dan Willemsen581341d2017-02-09 16:16:31 -0800389 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
390 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Oliver Nguyenc7434142019-04-24 14:22:25 -0700391 binary.coverageOutputFile = TransformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
Dan Willemsen581341d2017-02-09 16:16:31 -0800392
Alex Light3d673592019-01-18 14:37:31 -0800393 // Need to determine symlinks early since some targets (ie APEX) need this
394 // information but will not call 'install'
Colin Cross9b09f242016-12-07 13:37:42 -0800395 for _, symlink := range binary.Properties.Symlinks {
396 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800397 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800398 }
399
Nan Zhang0007d812017-11-07 10:57:05 -0800400 if Bool(binary.Properties.Symlink_preferred_arch) {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100401 if String(binary.Properties.Suffix) == "" {
402 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
Colin Cross9b09f242016-12-07 13:37:42 -0800403 }
404 if ctx.TargetPrimary() {
Roland Levillain9efb8c72019-06-05 13:31:31 +0100405 binary.symlinks = append(binary.symlinks, binary.getStemWithoutSuffix(ctx))
Colin Cross9b09f242016-12-07 13:37:42 -0800406 }
407 }
408
Alex Light3d673592019-01-18 14:37:31 -0800409 return ret
410}
411
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900412func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
413 return binary.unstrippedOutputFile
414}
415
Alex Light3d673592019-01-18 14:37:31 -0800416func (binary *binaryDecorator) symlinkList() []string {
417 return binary.symlinks
418}
419
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700420func (binary *binaryDecorator) nativeCoverage() bool {
421 return true
422}
423
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900424func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
425 return binary.coverageOutputFile
426}
427
Jiyong Parkf1194352019-02-25 11:05:47 +0900428// /system/bin/linker -> /apex/com.android.runtime/bin/linker
429func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
430 dir := binary.baseInstaller.installDir(ctx)
431 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
432 target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
433
434 ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
435 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
436
437 for _, symlink := range binary.symlinks {
438 ctx.InstallAbsoluteSymlink(dir, symlink, target)
439 binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target))
440 }
441}
442
Alex Light3d673592019-01-18 14:37:31 -0800443func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
Jiyong Parkf1194352019-02-25 11:05:47 +0900444 // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
445 // The original path becomes a symlink to the corresponding file in the
446 // runtime APEX.
Colin Cross3b19f5d2019-09-17 14:45:31 -0700447 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
Colin Cross56a83212020-09-15 18:30:11 -0700448 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700449 !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
450 !ctx.inVendorRamdisk() {
Colin Cross56a83212020-09-15 18:30:11 -0700451
Peter Collingbourne3478bb22019-04-24 14:41:12 -0700452 if ctx.Device() && isBionic(ctx.baseModuleName()) {
Jiyong Parkc3e2c862019-03-16 01:10:08 +0900453 binary.installSymlinkToRuntimeApex(ctx, file)
454 }
Jiyong Parkf1194352019-02-25 11:05:47 +0900455 binary.baseInstaller.subDir = "bootstrap"
456 }
Alex Light3d673592019-01-18 14:37:31 -0800457 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800458 for _, symlink := range binary.symlinks {
459 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
460 }
461
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700462 if ctx.Os().Class == android.Host {
463 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
464 }
465}
466
467func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
468 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700469}
Dan Willemsena0790e32018-10-12 00:24:23 -0700470
471func init() {
472 pctx.HostBinToolVariable("hostBionicSymbolsInjectCmd", "host_bionic_inject")
473}
474
475var injectHostBionicSymbols = pctx.AndroidStaticRule("injectHostBionicSymbols",
476 blueprint.RuleParams{
477 Command: "$hostBionicSymbolsInjectCmd -i $in -l $linker -o $out",
478 CommandDeps: []string{"$hostBionicSymbolsInjectCmd"},
479 }, "linker")
480
481func (binary *binaryDecorator) injectHostBionicLinkerSymbols(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
482 ctx.Build(pctx, android.BuildParams{
483 Rule: injectHostBionicSymbols,
484 Description: "inject host bionic symbols",
485 Input: in,
486 Implicit: linker,
487 Output: out,
488 Args: map[string]string{
489 "linker": linker.String(),
490 },
491 })
492}