blob: b2405b6fcbd90eeed73c749765c03d20f2342e9b [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 Crossb916a382016-07-29 17:28:03 -070018 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "android/soong/android"
21)
22
23type BinaryLinkerProperties struct {
24 // compile executable with -static
25 Static_executable *bool `android:"arch_variant"`
26
27 // set the name of the output
28 Stem string `android:"arch_variant"`
29
30 // append to the name of the output
31 Suffix string `android:"arch_variant"`
32
33 // if set, add an extra objcopy --prefix-symbols= step
34 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070035
dimitryfeda20b2017-08-29 15:00:01 +020036 // local file name to pass to the linker as --version_script
37 Version_script *string `android:"arch_variant"`
38
Colin Cross1e7d3702016-08-24 15:25:47 -070039 // if set, install a symlink to the preferred architecture
40 Symlink_preferred_arch bool
Colin Cross522e3732016-09-07 13:14:06 -070041
Colin Cross9b09f242016-12-07 13:37:42 -080042 // install symlinks to the binary. Symlink names will have the suffix and the binary
43 // extension (if any) appended
44 Symlinks []string `android:"arch_variant"`
45
Colin Cross7a108bc2017-01-30 22:44:19 -080046 // do not pass -pie
47 No_pie *bool `android:"arch_variant"`
48
Colin Cross522e3732016-09-07 13:14:06 -070049 DynamicLinker string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070050}
51
52func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070053 android.RegisterModuleType("cc_binary", binaryFactory)
54 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070055}
56
57// Module factory for binaries
Colin Cross36242852017-06-23 15:06:31 -070058func binaryFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070059 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060 return module.Init()
61}
62
63// Module factory for host binaries
Colin Cross36242852017-06-23 15:06:31 -070064func binaryHostFactory() android.Module {
Colin Crossb916a382016-07-29 17:28:03 -070065 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 return module.Init()
67}
68
69//
70// Executables
71//
72
Colin Crossb916a382016-07-29 17:28:03 -070073type binaryDecorator struct {
74 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070075 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 stripper
77
78 Properties BinaryLinkerProperties
79
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070080 toolPath android.OptionalPath
Colin Cross9b09f242016-12-07 13:37:42 -080081
82 // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
83 symlinks []string
Dan Willemsen581341d2017-02-09 16:16:31 -080084
85 // Output archive of gcno coverage information
86 coverageOutputFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070087}
88
Colin Crossb916a382016-07-29 17:28:03 -070089var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070090
Colin Crossb916a382016-07-29 17:28:03 -070091func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070092 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 &binary.Properties,
94 &binary.stripper.StripProperties)
95
96}
97
Colin Crossb916a382016-07-29 17:28:03 -070098func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070099 stem := ctx.baseModuleName()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 if binary.Properties.Stem != "" {
101 stem = binary.Properties.Stem
102 }
103
104 return stem + binary.Properties.Suffix
105}
106
Colin Cross37047f12016-12-13 17:06:13 -0800107func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -0700108 deps = binary.baseLinker.linkerDeps(ctx, deps)
Dan Willemsen2e47b342016-11-17 01:02:25 -0800109 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 if !Bool(binary.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700111 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -0700112 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 deps.CrtBegin = "crtbegin_static"
114 } else {
115 deps.CrtBegin = "crtbegin_dynamic"
116 }
117 deps.CrtEnd = "crtend_android"
118 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800119 // TODO(danalbert): Add generation of crt objects.
120 // For `sdk_version: "current"`, we don't actually have a
121 // freshly generated set of CRT objects. Use the last stable
122 // version.
123 version := ctx.sdkVersion()
124 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700125 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800126 }
127
Colin Crossb916a382016-07-29 17:28:03 -0700128 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800129 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700131 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800132 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800134 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 }
Dan Albertebedf672016-11-08 15:06:22 -0800136 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137 }
138 }
139 }
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" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
144 }
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() {
155 deps.LinkerScript = "host_bionic_linker_script"
156 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 }
158
Colin Crossb916a382016-07-29 17:28:03 -0700159 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
161 "from static libs or set static_executable: true")
162 }
163 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{
Colin Cross1e7d3702016-08-24 15:25:47 -0700173 baseLinker: NewBaseLinker(),
174 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 {
187 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700188 binary.Properties.Static_executable = proptools.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 Cross7a108bc2017-01-30 22:44:19 -0800209 if !ctx.AConfig().IsEnvTrue("DISABLE_HOST_PIE") {
210 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross3edeee12017-04-04 12:59:48 -0700211 if ctx.Windows() {
Colin Cross7a108bc2017-01-30 22:44:19 -0800212 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
213 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 }
215 }
216
217 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
218 // all code is position independent, and then those warnings get promoted to
219 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700220 if !ctx.Windows() {
Vishwath Mohane87b7682017-04-17 16:21:41 -0700221 flags.CFlags = append(flags.CFlags, "-fPIE")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223
Dan Willemsen2e47b342016-11-17 01:02:25 -0800224 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700225 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 // Clang driver needs -static to create static executable.
227 // However, bionic/linker uses -shared to overwrite.
228 // Linker for x86 targets does not allow coexistance of -static and -shared,
229 // so we add -static only if -shared is not used.
230 if !inList("-shared", flags.LdFlags) {
231 flags.LdFlags = append(flags.LdFlags, "-static")
232 }
233
234 flags.LdFlags = append(flags.LdFlags,
235 "-nostdlib",
236 "-Bstatic",
237 "-Wl,--gc-sections",
238 )
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239 } else {
240 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700241 if binary.Properties.DynamicLinker != "" {
242 flags.DynamicLinker = binary.Properties.DynamicLinker
243 } else {
Dan Willemsen01a405a2016-06-13 17:19:03 -0700244 switch ctx.Os() {
245 case android.Android:
246 flags.DynamicLinker = "/system/bin/linker"
247 case android.LinuxBionic:
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700248 flags.DynamicLinker = ""
Dan Willemsen01a405a2016-06-13 17:19:03 -0700249 default:
250 ctx.ModuleErrorf("unknown dynamic linker")
251 }
Colin Cross522e3732016-09-07 13:14:06 -0700252 if flags.Toolchain.Is64Bit() {
253 flags.DynamicLinker += "64"
254 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
256 }
257
258 flags.LdFlags = append(flags.LdFlags,
259 "-pie",
260 "-nostdlib",
261 "-Bdynamic",
262 "-Wl,--gc-sections",
263 "-Wl,-z,nocopyreloc",
264 )
dimitryfeda20b2017-08-29 15:00:01 +0200265
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 }
267 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700268 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 flags.LdFlags = append(flags.LdFlags, "-static")
270 }
271 if ctx.Darwin() {
272 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
273 }
274 }
275
276 return flags
277}
278
Colin Crossb916a382016-07-29 17:28:03 -0700279func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700280 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281
dimitryfeda20b2017-08-29 15:00:01 +0200282 versionScript := android.OptionalPathForModuleSrc(ctx, binary.Properties.Version_script)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
284 outputFile := android.PathForModuleOut(ctx, fileName)
285 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286
287 var linkerDeps android.Paths
288
289 sharedLibs := deps.SharedLibs
290 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
291
dimitryfeda20b2017-08-29 15:00:01 +0200292 if versionScript.Valid() {
293 if ctx.Darwin() {
294 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
295 } else {
296 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
297 linkerDeps = append(linkerDeps, versionScript.Path())
298 }
299 }
300
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700301 if deps.LinkerScript.Valid() {
302 flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String())
303 linkerDeps = append(linkerDeps, deps.LinkerScript.Path())
304 }
305
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 if flags.DynamicLinker != "" {
307 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
308 }
309
310 builderFlags := flagsToBuilderFlags(flags)
311
312 if binary.stripper.needsStrip(ctx) {
313 strippedOutputFile := outputFile
314 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
315 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
316 }
317
318 if binary.Properties.Prefix_symbols != "" {
319 afterPrefixSymbols := outputFile
320 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
321 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
322 flagsToBuilderFlags(flags), afterPrefixSymbols)
323 }
324
Colin Cross26c34ed2016-09-30 17:10:16 -0700325 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
326 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700327 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700328 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700329
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700330 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
332 builderFlags, outputFile)
333
Dan Willemsen581341d2017-02-09 16:16:31 -0800334 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
335 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
336 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
337
Colin Cross4d9c2d12016-07-29 12:48:20 -0700338 return ret
339}
340
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700341func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
342 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800343 for _, symlink := range binary.Properties.Symlinks {
344 binary.symlinks = append(binary.symlinks,
Colin Cross989c66e2016-12-08 09:43:41 -0800345 symlink+binary.Properties.Suffix+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800346 }
347
348 if binary.Properties.Symlink_preferred_arch {
349 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
350 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
351 }
352 if ctx.TargetPrimary() {
353 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
354 }
355 }
356
357 for _, symlink := range binary.symlinks {
358 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
359 }
360
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700361 if ctx.Os().Class == android.Host {
362 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
363 }
364}
365
366func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
367 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368}