blob: 5f818662d9d8d262b5c1dfcfa5c195d58c0d0f23 [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"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070048}
49
50func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070051 android.RegisterModuleType("cc_binary", binaryFactory)
52 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070053}
54
55// Module factory for binaries
Colin Cross36242852017-06-23 15:06:31 -070056func binaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070057 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 return module.Init()
59}
60
61// Module factory for host binaries
Colin Cross36242852017-06-23 15:06:31 -070062func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070063 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070064 return module.Init()
65}
66
67//
68// Executables
69//
70
Colin Crossb916a382016-07-29 17:28:03 -070071type binaryDecorator struct {
72 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070073 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 stripper
75
76 Properties BinaryLinkerProperties
77
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070078 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080079
80 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
81 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080082
83 // Output archive of gcno coverage information
84 coverageOutputFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070085}
86
Colin Crossb916a382016-07-29 17:28:03 -070087var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070088
Colin Crossb916a382016-07-29 17:28:03 -070089func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070090 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070091 &binary.Properties,
92 &binary.stripper.StripProperties)
93
94}
95
Colin Crossb916a382016-07-29 17:28:03 -070096func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070097 stem := ctx.baseModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -080098 if String(binary.Properties.Stem) != "" {
99 stem = String(binary.Properties.Stem)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 }
101
Nan Zhang0007d812017-11-07 10:57:05 -0800102 return stem + String(binary.Properties.Suffix)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103}
104
Colin Cross37047f12016-12-13 17:06:13 -0800105func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700106 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800107 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 if !Bool(binary.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700109 if !ctx.useSdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700110 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 deps.CrtBegin = "crtbegin_static"
112 } else {
113 deps.CrtBegin = "crtbegin_dynamic"
114 }
115 deps.CrtEnd = "crtend_android"
116 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800117 // TODO(danalbert): Add generation of crt objects.
118 // For `sdk_version: "current"`, we don't actually have a
119 // freshly generated set of CRT objects. Use the last stable
120 // version.
121 version := ctx.sdkVersion()
122 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700123 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800124 }
125
Colin Crossb916a382016-07-29 17:28:03 -0700126 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800127 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700129 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800130 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800132 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 }
Dan Albertebedf672016-11-08 15:06:22 -0800134 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 }
136 }
137 }
138
Colin Crossb916a382016-07-29 17:28:03 -0700139 if binary.static() {
Dan Albertdc2597d2017-01-26 17:44:26 -0800140 if ctx.selectedStl() == "libc++_static" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
142 }
143 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
144 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
145 // move them to the beginning of deps.LateStaticLibs
146 var groupLibs []string
147 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
148 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
149 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
150 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700151
152 if ctx.Os() == android.LinuxBionic && !binary.static() {
153 deps.LinkerScript = "host_bionic_linker_script"
154 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
156
Colin Crossb916a382016-07-29 17:28:03 -0700157 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
159 "from static libs or set static_executable: true")
160 }
161 return deps
162}
163
Colin Crossb916a382016-07-29 17:28:03 -0700164func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 return true
166}
167
Colin Crossb916a382016-07-29 17:28:03 -0700168func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700170 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700171 baseLinker: NewBaseLinker(),
172 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 }
Colin Crossb916a382016-07-29 17:28:03 -0700174 module.compiler = NewBaseCompiler()
175 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700176 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700177 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178}
179
Colin Crossb916a382016-07-29 17:28:03 -0700180func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700181 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182
Dan Willemsen2e47b342016-11-17 01:02:25 -0800183 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 if ctx.Os() == android.Linux {
185 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Nan Zhang0007d812017-11-07 10:57:05 -0800186 binary.Properties.Static_executable = BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 }
188 } else {
189 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700190 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 }
192 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193}
194
Colin Crossb916a382016-07-29 17:28:03 -0700195func (binary *binaryDecorator) static() bool {
196 return Bool(binary.Properties.Static_executable)
197}
198
199func (binary *binaryDecorator) staticBinary() bool {
200 return binary.static()
201}
202
203func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700204 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205
Colin Crossb916a382016-07-29 17:28:03 -0700206 if ctx.Host() && !binary.static() {
Colin Cross7a108bc2017-01-30 22:44:19 -0800207 if !ctx.AConfig().IsEnvTrue("DISABLE_HOST_PIE") {
208 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross3edeee12017-04-04 12:59:48 -0700209 if ctx.Windows() {
Colin Cross7a108bc2017-01-30 22:44:19 -0800210 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
211 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 }
213 }
214
215 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
216 // all code is position independent, and then those warnings get promoted to
217 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700218 if !ctx.Windows() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700219 flags.CFlags = append(flags.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 }
221
Dan Willemsen2e47b342016-11-17 01:02:25 -0800222 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700223 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224 // Clang driver needs -static to create static executable.
225 // However, bionic/linker uses -shared to overwrite.
226 // Linker for x86 targets does not allow coexistance of -static and -shared,
227 // so we add -static only if -shared is not used.
228 if !inList("-shared", flags.LdFlags) {
229 flags.LdFlags = append(flags.LdFlags, "-static")
230 }
231
232 flags.LdFlags = append(flags.LdFlags,
233 "-nostdlib",
234 "-Bstatic",
235 "-Wl,--gc-sections",
236 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 } else {
238 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700239 if binary.Properties.DynamicLinker != "" {
240 flags.DynamicLinker = binary.Properties.DynamicLinker
241 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700242 switch ctx.Os() {
243 case android.Android:
244 flags.DynamicLinker = "/system/bin/linker"
245 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700246 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700247 default:
248 ctx.ModuleErrorf("unknown dynamic linker")
249 }
Colin Cross522e3732016-09-07 13:14:06 -0700250 if flags.Toolchain.Is64Bit() {
251 flags.DynamicLinker += "64"
252 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 }
254 }
255
256 flags.LdFlags = append(flags.LdFlags,
257 "-pie",
258 "-nostdlib",
259 "-Bdynamic",
260 "-Wl,--gc-sections",
261 "-Wl,-z,nocopyreloc",
262 )
dimitryfeda20b2017-08-29 15:00:01 +0200263
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264 }
265 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700266 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 flags.LdFlags = append(flags.LdFlags, "-static")
268 }
269 if ctx.Darwin() {
270 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
271 }
272 }
273
274 return flags
275}
276
Colin Crossb916a382016-07-29 17:28:03 -0700277func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700278 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279
dimitryfeda20b2017-08-29 15:00:01 +0200280 versionScript := android.OptionalPathForModuleSrc(ctx, binary.Properties.Version_script)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
282 outputFile := android.PathForModuleOut(ctx, fileName)
283 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284
285 var linkerDeps android.Paths
286
287 sharedLibs := deps.SharedLibs
288 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
289
dimitryfeda20b2017-08-29 15:00:01 +0200290 if versionScript.Valid() {
291 if ctx.Darwin() {
292 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
293 } else {
294 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
295 linkerDeps = append(linkerDeps, versionScript.Path())
296 }
297 }
298
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700299 if deps.LinkerScript.Valid() {
300 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
301 linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
302 }
303
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304 if flags.DynamicLinker != "" {
305 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
306 }
307
308 builderFlags := flagsToBuilderFlags(flags)
309
310 if binary.stripper.needsStrip(ctx) {
311 strippedOutputFile := outputFile
312 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
313 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
314 }
315
Nan Zhang0007d812017-11-07 10:57:05 -0800316 if String(binary.Properties.Prefix_symbols) != "" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317 afterPrefixSymbols := outputFile
318 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Nan Zhang0007d812017-11-07 10:57:05 -0800319 TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 flagsToBuilderFlags(flags), afterPrefixSymbols)
321 }
322
Colin Cross26c34ed2016-09-30 17:10:16 -0700323 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
324 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700325 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700326 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700327
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700328 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
330 builderFlags, outputFile)
331
Dan Willemsen581341d2017-02-09 16:16:31 -0800332 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
333 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
334 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
335
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336 return ret
337}
338
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700339func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
340 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800341 for _, symlink := range binary.Properties.Symlinks {
342 binary.symlinks = append(binary.symlinks,
Nan Zhang0007d812017-11-07 10:57:05 -0800343 symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800344 }
345
Nan Zhang0007d812017-11-07 10:57:05 -0800346 if Bool(binary.Properties.Symlink_preferred_arch) {
347 if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" {
Colin Cross9b09f242016-12-07 13:37:42 -0800348 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
349 }
350 if ctx.TargetPrimary() {
351 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
352 }
353 }
354
355 for _, symlink := range binary.symlinks {
356 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
357 }
358
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700359 if ctx.Os().Class == android.Host {
360 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
361 }
362}
363
364func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
365 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366}