blob: 82e1941f2ed131e6d9169ad050694f65d4d11807 [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 Cross522e3732016-09-07 13:14:06 -070044 DynamicLinker string `blueprint:"mutated"`
Yifan Hong946e32e2018-04-03 13:22:50 -070045
46 // Names of modules to be overridden. Listed modules can only be other binaries
47 // (in Make or Soong).
48 // This does not completely prevent installation of the overridden binaries, but if both
49 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
50 // from PRODUCT_PACKAGES.
51 Overrides []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070052}
53
54func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070055 android.RegisterModuleType("cc_binary", binaryFactory)
56 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070057}
58
59// Module factory for binaries
Colin Cross36242852017-06-23 15:06:31 -070060func binaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070061 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070062 return module.Init()
63}
64
65// Module factory for host binaries
Colin Cross36242852017-06-23 15:06:31 -070066func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070067 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 return module.Init()
69}
70
71//
72// Executables
73//
74
Colin Crossb916a382016-07-29 17:28:03 -070075type binaryDecorator struct {
76 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070077 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 stripper
79
80 Properties BinaryLinkerProperties
81
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070082 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080083
84 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
85 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080086
87 // Output archive of gcno coverage information
88 coverageOutputFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070089}
90
Colin Crossb916a382016-07-29 17:28:03 -070091var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070092
Colin Crossb916a382016-07-29 17:28:03 -070093func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070094 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 &binary.Properties,
96 &binary.stripper.StripProperties)
97
98}
99
Colin Crossb916a382016-07-29 17:28:03 -0700100func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700101 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800102 if String(binary.Properties.Stem) != "" {
103 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 }
105
Nan Zhang0007d812017-11-07 10:57:05 -0800106 return stem + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107}
108
Colin Cross37047f12016-12-13 17:06:13 -0800109func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700110 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800111 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 if !Bool(binary.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700113 if !ctx.useSdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700114 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 deps.CrtBegin = "crtbegin_static"
116 } else {
117 deps.CrtBegin = "crtbegin_dynamic"
118 }
119 deps.CrtEnd = "crtend_android"
120 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800121 // TODO(danalbert): Add generation of crt objects.
122 // For `sdk_version: "current"`, we don't actually have a
123 // freshly generated set of CRT objects. Use the last stable
124 // version.
125 version := ctx.sdkVersion()
126 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700127 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800128 }
129
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 {
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 {
Dan Albertebedf672016-11-08 15:06:22 -0800136 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137 }
Dan Albertebedf672016-11-08 15:06:22 -0800138 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 }
140 }
141 }
142
Colin Crossb916a382016-07-29 17:28:03 -0700143 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800144 if ctx.selectedStl() == "libc++_static" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
146 }
147 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
148 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
149 // move them to the beginning of deps.LateStaticLibs
150 var groupLibs []string
151 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
152 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
153 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
154 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700155
156 if ctx.Os() == android.LinuxBionic && !binary.static() {
157 deps.LinkerScript = "host_bionic_linker_script"
158 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 }
160
Colin Crossb916a382016-07-29 17:28:03 -0700161 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
163 "from static libs or set static_executable: true")
164 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800165
166 android.ExtractSourceDeps(ctx, binary.Properties.Version_script)
167
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 return deps
169}
170
Colin Crossb916a382016-07-29 17:28:03 -0700171func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 return true
173}
174
Colin Crossb916a382016-07-29 17:28:03 -0700175func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700177 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700178 baseLinker: NewBaseLinker(),
179 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 }
Colin Crossb916a382016-07-29 17:28:03 -0700181 module.compiler = NewBaseCompiler()
182 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700183 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700184 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185}
186
Colin Crossb916a382016-07-29 17:28:03 -0700187func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700188 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189
Dan Willemsen2e47b342016-11-17 01:02:25 -0800190 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 if ctx.Os() == android.Linux {
Dan Willemsen3fb1fae2018-03-12 15:30:26 -0700192 if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
Nan Zhang0007d812017-11-07 10:57:05 -0800193 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 }
195 } else {
196 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700197 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 }
199 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200}
201
Colin Crossb916a382016-07-29 17:28:03 -0700202func (binary *binaryDecorator) static() bool {
203 return Bool(binary.Properties.Static_executable)
204}
205
206func (binary *binaryDecorator) staticBinary() bool {
207 return binary.static()
208}
209
210func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700211 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212
Colin Crossb916a382016-07-29 17:28:03 -0700213 if ctx.Host() && !binary.static() {
Colin Cross6510f912017-11-29 00:27:14 -0800214 if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
Colin Cross7a108bc2017-01-30 22:44:19 -0800215 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 }
217 }
218
219 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
220 // all code is position independent, and then those warnings get promoted to
221 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700222 if !ctx.Windows() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700223 flags.CFlags = append(flags.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224 }
225
Dan Willemsen2e47b342016-11-17 01:02:25 -0800226 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700227 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 // Clang driver needs -static to create static executable.
229 // However, bionic/linker uses -shared to overwrite.
230 // Linker for x86 targets does not allow coexistance of -static and -shared,
231 // so we add -static only if -shared is not used.
232 if !inList("-shared", flags.LdFlags) {
233 flags.LdFlags = append(flags.LdFlags, "-static")
234 }
235
236 flags.LdFlags = append(flags.LdFlags,
237 "-nostdlib",
238 "-Bstatic",
239 "-Wl,--gc-sections",
240 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 } else {
242 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700243 if binary.Properties.DynamicLinker != "" {
244 flags.DynamicLinker = binary.Properties.DynamicLinker
245 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700246 switch ctx.Os() {
247 case android.Android:
248 flags.DynamicLinker = "/system/bin/linker"
249 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700250 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700251 default:
252 ctx.ModuleErrorf("unknown dynamic linker")
253 }
Colin Cross522e3732016-09-07 13:14:06 -0700254 if flags.Toolchain.Is64Bit() {
255 flags.DynamicLinker += "64"
256 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 }
258 }
259
260 flags.LdFlags = append(flags.LdFlags,
261 "-pie",
262 "-nostdlib",
263 "-Bdynamic",
264 "-Wl,--gc-sections",
265 "-Wl,-z,nocopyreloc",
266 )
dimitryfeda20b2017-08-29 15:00:01 +0200267
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 }
269 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700270 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 flags.LdFlags = append(flags.LdFlags, "-static")
272 }
273 if ctx.Darwin() {
274 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
275 }
276 }
277
278 return flags
279}
280
Colin Crossb916a382016-07-29 17:28:03 -0700281func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700282 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283
Colin Cross2383f3b2018-02-06 14:40:13 -0800284 versionScript := ctx.ExpandOptionalSource(binary.Properties.Version_script, "version_script")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
286 outputFile := android.PathForModuleOut(ctx, fileName)
287 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288
289 var linkerDeps android.Paths
290
291 sharedLibs := deps.SharedLibs
292 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
293
dimitryfeda20b2017-08-29 15:00:01 +0200294 if versionScript.Valid() {
295 if ctx.Darwin() {
296 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
297 } else {
298 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
299 linkerDeps = append(linkerDeps, versionScript.Path())
300 }
301 }
302
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700303 if deps.LinkerScript.Valid() {
304 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
305 linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
306 }
307
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 if flags.DynamicLinker != "" {
309 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
310 }
311
312 builderFlags := flagsToBuilderFlags(flags)
313
314 if binary.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700315 // b/80093681, GNU strip/objcopy bug.
316 // Use llvm-{strip,objcopy} when clang lld is used.
317 builderFlags.stripUseLlvmStrip =
318 flags.Clang && binary.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700319 strippedOutputFile := outputFile
320 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
321 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
322 }
323
Nan Zhang0007d812017-11-07 10:57:05 -0800324 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 afterPrefixSymbols := outputFile
326 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800327 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 flagsToBuilderFlags(flags), afterPrefixSymbols)
329 }
330
Colin Cross86803cf2018-02-15 14:12:26 -0800331 if Bool(binary.baseLinker.Properties.Use_version_lib) && ctx.Host() {
332 versionedOutputFile := outputFile
333 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
334 binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
335 }
336
Colin Cross26c34ed2016-09-30 17:10:16 -0700337 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
338 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700339 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700340 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700341
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700342 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
344 builderFlags, outputFile)
345
Dan Willemsen581341d2017-02-09 16:16:31 -0800346 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
347 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
348 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
349
Colin Cross4d9c2d12016-07-29 12:48:20 -0700350 return ret
351}
352
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700353func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
354 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800355 for _, symlink := range binary.Properties.Symlinks {
356 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800357 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800358 }
359
Nan Zhang0007d812017-11-07 10:57:05 -0800360 if Bool(binary.Properties.Symlink_preferred_arch) {
361 if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" {
Colin Cross9b09f242016-12-07 13:37:42 -0800362 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
363 }
364 if ctx.TargetPrimary() {
365 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
366 }
367 }
368
369 for _, symlink := range binary.symlinks {
370 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
371 }
372
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700373 if ctx.Os().Class == android.Host {
374 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
375 }
376}
377
378func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
379 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700380}