blob: be790325a46e85cf8a6272367937947322894e07 [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
34 // if set, install a symlink to the preferred architecture
Nan Zhang0007d812017-11-07 10:57:05 -080035 Symlink_preferred_arch *bool
Colin Cross522e3732016-09-07 13:14:06 -070036
Colin Cross9b09f242016-12-07 13:37:42 -080037 // install symlinks to the binary. Symlink names will have the suffix and the binary
38 // extension (if any) appended
39 Symlinks []string `android:"arch_variant"`
40
Colin Cross522e3732016-09-07 13:14:06 -070041 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070042
43 // Names of modules to be overridden. Listed modules can only be other binaries
44 // (in Make or Soong).
45 // This does not completely prevent installation of the overridden binaries, but if both
46 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
47 // from PRODUCT_PACKAGES.
48 Overrides []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070049}
50
51func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterModuleType("cc_binary", binaryFactory)
53 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070054}
55
56// Module factory for binaries
Colin Cross36242852017-06-23 15:06:31 -070057func binaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070058 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070059 return module.Init()
60}
61
62// Module factory for host binaries
Colin Cross36242852017-06-23 15:06:31 -070063func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070064 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070065 return module.Init()
66}
67
68//
69// Executables
70//
71
Colin Crossb916a382016-07-29 17:28:03 -070072type binaryDecorator struct {
73 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070074 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 stripper
76
77 Properties BinaryLinkerProperties
78
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070079 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080080
81 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
82 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080083
84 // Output archive of gcno coverage information
85 coverageOutputFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070086}
87
Colin Crossb916a382016-07-29 17:28:03 -070088var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070089
Colin Crossb916a382016-07-29 17:28:03 -070090func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070091 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070092 &binary.Properties,
93 &binary.stripper.StripProperties)
94
95}
96
Colin Crossb916a382016-07-29 17:28:03 -070097func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070098 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -080099 if String(binary.Properties.Stem) != "" {
100 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101 }
102
Nan Zhang0007d812017-11-07 10:57:05 -0800103 return stem + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104}
105
Colin Cross37047f12016-12-13 17:06:13 -0800106func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700107 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800108 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 if !Bool(binary.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700110 if !ctx.useSdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700111 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 deps.CrtBegin = "crtbegin_static"
113 } else {
114 deps.CrtBegin = "crtbegin_dynamic"
115 }
116 deps.CrtEnd = "crtend_android"
117 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800118 // TODO(danalbert): Add generation of crt objects.
119 // For `sdk_version: "current"`, we don't actually have a
120 // freshly generated set of CRT objects. Use the last stable
121 // version.
122 version := ctx.sdkVersion()
123 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700124 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800125 }
126
Colin Crossb916a382016-07-29 17:28:03 -0700127 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800128 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700130 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800131 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800133 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 }
Dan Albertebedf672016-11-08 15:06:22 -0800135 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 }
137 }
138 }
139
Colin Crossb916a382016-07-29 17:28:03 -0700140 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800141 if ctx.selectedStl() == "libc++_static" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
143 }
144 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
145 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
146 // move them to the beginning of deps.LateStaticLibs
147 var groupLibs []string
148 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
149 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
150 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
151 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700152
153 if ctx.Os() == android.LinuxBionic && !binary.static() {
154 deps.LinkerScript = "host_bionic_linker_script"
155 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 }
157
Colin Crossb916a382016-07-29 17:28:03 -0700158 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
160 "from static libs or set static_executable: true")
161 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800162
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 return deps
164}
165
Colin Crossb916a382016-07-29 17:28:03 -0700166func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 return true
168}
169
Colin Crossb916a382016-07-29 17:28:03 -0700170func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700172 binary := &binaryDecorator{
Dan Albert61f32122018-07-26 14:00:24 -0700173 baseLinker: NewBaseLinker(module.sanitize),
Colin Cross1e7d3702016-08-24 15:25:47 -0700174 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 }
Colin Crossb916a382016-07-29 17:28:03 -0700176 module.compiler = NewBaseCompiler()
177 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700178 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700179 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180}
181
Colin Crossb916a382016-07-29 17:28:03 -0700182func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700183 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184
Dan Willemsen2e47b342016-11-17 01:02:25 -0800185 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 if ctx.Os() == android.Linux {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700187 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800188 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189 }
190 } else {
191 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700192 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 }
194 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}
196
Colin Crossb916a382016-07-29 17:28:03 -0700197func (binary *binaryDecorator) static() bool {
198 return Bool(binary.Properties.Static_executable)
199}
200
201func (binary *binaryDecorator) staticBinary() bool {
202 return binary.static()
203}
204
205func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700206 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207
Colin Crossb916a382016-07-29 17:28:03 -0700208 if ctx.Host() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800209 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross7a108bc2017-01-30 22:44:19 -0800210 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 }
212 }
213
214 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
215 // all code is position independent, and then those warnings get promoted to
216 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700217 if !ctx.Windows() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700218 flags.CFlags = append(flags.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 }
220
Dan Willemsen2e47b342016-11-17 01:02:25 -0800221 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700222 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 // Clang driver needs -static to create static executable.
224 // However, bionic/linker uses -shared to overwrite.
225 // Linker for x86 targets does not allow coexistance of -static and -shared,
226 // so we add -static only if -shared is not used.
227 if !inList("-shared", flags.LdFlags) {
228 flags.LdFlags = append(flags.LdFlags, "-static")
229 }
230
231 flags.LdFlags = append(flags.LdFlags,
232 "-nostdlib",
233 "-Bstatic",
234 "-Wl,--gc-sections",
235 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 } else {
237 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700238 if binary.Properties.DynamicLinker != "" {
239 flags.DynamicLinker = binary.Properties.DynamicLinker
240 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700241 switch ctx.Os() {
242 case android.Android:
243 flags.DynamicLinker = "/system/bin/linker"
244 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700245 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700246 default:
247 ctx.ModuleErrorf("unknown dynamic linker")
248 }
Colin Cross522e3732016-09-07 13:14:06 -0700249 if flags.Toolchain.Is64Bit() {
250 flags.DynamicLinker += "64"
251 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 }
253 }
254
255 flags.LdFlags = append(flags.LdFlags,
256 "-pie",
257 "-nostdlib",
258 "-Bdynamic",
259 "-Wl,--gc-sections",
260 "-Wl,-z,nocopyreloc",
261 )
dimitryfeda20b2017-08-29 15:00:01 +0200262
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 }
264 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700265 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 flags.LdFlags = append(flags.LdFlags, "-static")
267 }
268 if ctx.Darwin() {
269 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
270 }
271 }
272
273 return flags
274}
275
Colin Crossb916a382016-07-29 17:28:03 -0700276func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700277 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278
279 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
280 outputFile := android.PathForModuleOut(ctx, fileName)
281 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282
283 var linkerDeps android.Paths
284
285 sharedLibs := deps.SharedLibs
286 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
287
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700288 if deps.LinkerScript.Valid() {
289 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
290 linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
291 }
292
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 if flags.DynamicLinker != "" {
294 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
295 }
296
297 builderFlags := flagsToBuilderFlags(flags)
298
299 if binary.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700300 // b/80093681, GNU strip/objcopy bug.
301 // Use llvm-{strip,objcopy} when clang lld is used.
302 builderFlags.stripUseLlvmStrip =
303 flags.Clang && binary.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304 strippedOutputFile := outputFile
305 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
306 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
307 }
308
Nan Zhang0007d812017-11-07 10:57:05 -0800309 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 afterPrefixSymbols := outputFile
311 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800312 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 flagsToBuilderFlags(flags), afterPrefixSymbols)
314 }
315
Colin Cross86803cf2018-02-15 14:12:26 -0800316 if Bool(binary.baseLinker.Properties.Use_version_lib) && ctx.Host() {
317 versionedOutputFile := outputFile
318 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
319 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
320 }
321
Colin Cross26c34ed2016-09-30 17:10:16 -0700322 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
323 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700324 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700325 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700326
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700327 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
329 builderFlags, outputFile)
330
Dan Willemsen581341d2017-02-09 16:16:31 -0800331 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
332 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
333 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
334
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 return ret
336}
337
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700338func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
339 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800340 for _, symlink := range binary.Properties.Symlinks {
341 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800342 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800343 }
344
Nan Zhang0007d812017-11-07 10:57:05 -0800345 if Bool(binary.Properties.Symlink_preferred_arch) {
346 if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" {
Colin Cross9b09f242016-12-07 13:37:42 -0800347 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
348 }
349 if ctx.TargetPrimary() {
350 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
351 }
352 }
353
354 for _, symlink := range binary.symlinks {
355 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
356 }
357
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700358 if ctx.Os().Class == android.Host {
359 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
360 }
361}
362
363func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
364 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365}