blob: c3e899a043962909e965f486878a00896c3a02d9 [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 (
Colin Cross4d9c2d12016-07-29 12:48:20 -070018 "android/soong/android"
19)
20
21type BinaryLinkerProperties struct {
22 // compile executable with -static
23 Static_executable *bool `android:"arch_variant"`
24
25 // set the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080026 Stem *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070027
28 // append to the name of the output
Nan Zhang0007d812017-11-07 10:57:05 -080029 Suffix *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
31 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -080032 Prefix_symbols *string
Colin Cross1e7d3702016-08-24 15:25:47 -070033
dimitryfeda20b2017-08-29 15:00:01 +020034 // local file name to pass to the linker as --version_script
35 Version_script *string `android:"arch_variant"`
36
Colin Cross1e7d3702016-08-24 15:25:47 -070037 // if set, install a symlink to the preferred architecture
Nan Zhang0007d812017-11-07 10:57:05 -080038 Symlink_preferred_arch *bool
Colin Cross522e3732016-09-07 13:14:06 -070039
Colin Cross9b09f242016-12-07 13:37:42 -080040 // install symlinks to the binary. Symlink names will have the suffix and the binary
41 // extension (if any) appended
42 Symlinks []string `android:"arch_variant"`
43
Colin Cross7a108bc2017-01-30 22:44:19 -080044 // do not pass -pie
45 No_pie *bool `android:"arch_variant"`
46
Colin Cross522e3732016-09-07 13:14:06 -070047 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070048
49 // Names of modules to be overridden. Listed modules can only be other binaries
50 // (in Make or Soong).
51 // This does not completely prevent installation of the overridden binaries, but if both
52 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
53 // from PRODUCT_PACKAGES.
54 Overrides []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070055}
56
57func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070058 android.RegisterModuleType("cc_binary", binaryFactory)
59 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060}
61
62// Module factory for binaries
Colin Cross36242852017-06-23 15:06:31 -070063func binaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070064 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070065 return module.Init()
66}
67
68// Module factory for host binaries
Colin Cross36242852017-06-23 15:06:31 -070069func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070070 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 return module.Init()
72}
73
74//
75// Executables
76//
77
Colin Crossb916a382016-07-29 17:28:03 -070078type binaryDecorator struct {
79 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070080 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 stripper
82
83 Properties BinaryLinkerProperties
84
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070085 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080086
87 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
88 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080089
90 // Output archive of gcno coverage information
91 coverageOutputFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070092}
93
Colin Crossb916a382016-07-29 17:28:03 -070094var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070095
Colin Crossb916a382016-07-29 17:28:03 -070096func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070097 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 &binary.Properties,
99 &binary.stripper.StripProperties)
100
101}
102
Colin Crossb916a382016-07-29 17:28:03 -0700103func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700104 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800105 if String(binary.Properties.Stem) != "" {
106 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 }
108
Nan Zhang0007d812017-11-07 10:57:05 -0800109 return stem + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110}
111
Colin Cross37047f12016-12-13 17:06:13 -0800112func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700113 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800114 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 if !Bool(binary.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700116 if !ctx.useSdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700117 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 deps.CrtBegin = "crtbegin_static"
119 } else {
120 deps.CrtBegin = "crtbegin_dynamic"
121 }
122 deps.CrtEnd = "crtend_android"
123 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800124 // TODO(danalbert): Add generation of crt objects.
125 // For `sdk_version: "current"`, we don't actually have a
126 // freshly generated set of CRT objects. Use the last stable
127 // version.
128 version := ctx.sdkVersion()
129 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700130 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800131 }
132
Colin Crossb916a382016-07-29 17:28:03 -0700133 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800134 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700136 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800137 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800139 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 }
Dan Albertebedf672016-11-08 15:06:22 -0800141 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 }
143 }
144 }
145
Colin Crossb916a382016-07-29 17:28:03 -0700146 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800147 if ctx.selectedStl() == "libc++_static" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
149 }
150 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
151 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
152 // move them to the beginning of deps.LateStaticLibs
153 var groupLibs []string
154 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
155 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
156 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
157 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700158
159 if ctx.Os() == android.LinuxBionic && !binary.static() {
160 deps.LinkerScript = "host_bionic_linker_script"
161 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163
Colin Crossb916a382016-07-29 17:28:03 -0700164 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
166 "from static libs or set static_executable: true")
167 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800168
169 android.ExtractSourceDeps(ctx, binary.Properties.Version_script)
170
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 return deps
172}
173
Colin Crossb916a382016-07-29 17:28:03 -0700174func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 return true
176}
177
Colin Crossb916a382016-07-29 17:28:03 -0700178func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700180 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700181 baseLinker: NewBaseLinker(),
182 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183 }
Colin Crossb916a382016-07-29 17:28:03 -0700184 module.compiler = NewBaseCompiler()
185 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700186 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700187 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188}
189
Colin Crossb916a382016-07-29 17:28:03 -0700190func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700191 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192
Dan Willemsen2e47b342016-11-17 01:02:25 -0800193 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 if ctx.Os() == android.Linux {
Colin Cross6510f912017-11-29 00:27:14 -0800195 if binary.Properties.Static_executable == nil && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
Nan Zhang0007d812017-11-07 10:57:05 -0800196 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 }
198 } else {
199 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700200 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 }
202 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203}
204
Colin Crossb916a382016-07-29 17:28:03 -0700205func (binary *binaryDecorator) static() bool {
206 return Bool(binary.Properties.Static_executable)
207}
208
209func (binary *binaryDecorator) staticBinary() bool {
210 return binary.static()
211}
212
213func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700214 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215
Colin Crossb916a382016-07-29 17:28:03 -0700216 if ctx.Host() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800217 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross7a108bc2017-01-30 22:44:19 -0800218 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross3edeee12017-04-04 12:59:48 -0700219 if ctx.Windows() {
Colin Cross7a108bc2017-01-30 22:44:19 -0800220 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
221 }
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() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700229 flags.CFlags = append(flags.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.
238 if !inList("-shared", flags.LdFlags) {
239 flags.LdFlags = append(flags.LdFlags, "-static")
240 }
241
242 flags.LdFlags = append(flags.LdFlags,
243 "-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:
254 flags.DynamicLinker = "/system/bin/linker"
255 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700256 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700257 default:
258 ctx.ModuleErrorf("unknown dynamic linker")
259 }
Colin Cross522e3732016-09-07 13:14:06 -0700260 if flags.Toolchain.Is64Bit() {
261 flags.DynamicLinker += "64"
262 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 }
264 }
265
266 flags.LdFlags = append(flags.LdFlags,
267 "-pie",
268 "-nostdlib",
269 "-Bdynamic",
270 "-Wl,--gc-sections",
271 "-Wl,-z,nocopyreloc",
272 )
dimitryfeda20b2017-08-29 15:00:01 +0200273
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 }
275 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700276 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 flags.LdFlags = append(flags.LdFlags, "-static")
278 }
279 if ctx.Darwin() {
280 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
281 }
282 }
283
284 return flags
285}
286
Colin Crossb916a382016-07-29 17:28:03 -0700287func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700288 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289
Colin Cross2383f3b2018-02-06 14:40:13 -0800290 versionScript := ctx.ExpandOptionalSource(binary.Properties.Version_script, "version_script")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
292 outputFile := android.PathForModuleOut(ctx, fileName)
293 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294
295 var linkerDeps android.Paths
296
297 sharedLibs := deps.SharedLibs
298 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
299
dimitryfeda20b2017-08-29 15:00:01 +0200300 if versionScript.Valid() {
301 if ctx.Darwin() {
302 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
303 } else {
304 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
305 linkerDeps = append(linkerDeps, versionScript.Path())
306 }
307 }
308
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700309 if deps.LinkerScript.Valid() {
310 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
311 linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
312 }
313
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 if flags.DynamicLinker != "" {
315 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
316 }
317
318 builderFlags := flagsToBuilderFlags(flags)
319
320 if binary.stripper.needsStrip(ctx) {
321 strippedOutputFile := outputFile
322 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
323 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
324 }
325
Nan Zhang0007d812017-11-07 10:57:05 -0800326 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327 afterPrefixSymbols := outputFile
328 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800329 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 flagsToBuilderFlags(flags), afterPrefixSymbols)
331 }
332
Colin Cross86803cf2018-02-15 14:12:26 -0800333 if Bool(binary.baseLinker.Properties.Use_version_lib) && ctx.Host() {
334 versionedOutputFile := outputFile
335 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
336 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
337 }
338
Colin Cross26c34ed2016-09-30 17:10:16 -0700339 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
340 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700341 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700342 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700343
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700344 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
346 builderFlags, outputFile)
347
Dan Willemsen581341d2017-02-09 16:16:31 -0800348 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
349 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
350 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
351
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 return ret
353}
354
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700355func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
356 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800357 for _, symlink := range binary.Properties.Symlinks {
358 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800359 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800360 }
361
Nan Zhang0007d812017-11-07 10:57:05 -0800362 if Bool(binary.Properties.Symlink_preferred_arch) {
363 if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" {
Colin Cross9b09f242016-12-07 13:37:42 -0800364 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
365 }
366 if ctx.TargetPrimary() {
367 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
368 }
369 }
370
371 for _, symlink := range binary.symlinks {
372 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
373 }
374
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700375 if ctx.Os().Class == android.Host {
376 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
377 }
378}
379
380func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
381 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382}