blob: 521ccb703e273e62e72c59a0a37d08ddddd2a975 [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 (
18 "github.com/google/blueprint"
Colin Crossb916a382016-07-29 17:28:03 -070019 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "android/soong/android"
22)
23
24type BinaryLinkerProperties struct {
25 // compile executable with -static
26 Static_executable *bool `android:"arch_variant"`
27
28 // set the name of the output
29 Stem string `android:"arch_variant"`
30
31 // append to the name of the output
32 Suffix string `android:"arch_variant"`
33
34 // if set, add an extra objcopy --prefix-symbols= step
35 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070036
37 // if set, install a symlink to the preferred architecture
38 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
56func binaryFactory() (blueprint.Module, []interface{}) {
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
62func binaryHostFactory() (blueprint.Module, []interface{}) {
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()
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 if binary.Properties.Stem != "" {
99 stem = binary.Properties.Stem
100 }
101
102 return stem + binary.Properties.Suffix
103}
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) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800109 if !ctx.sdk() && !ctx.vndk() {
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" {
123 version = ctx.AConfig().PlatformSdkVersion()
124 }
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 }
151 }
152
Colin Crossb916a382016-07-29 17:28:03 -0700153 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
155 "from static libs or set static_executable: true")
156 }
157 return deps
158}
159
Colin Crossb916a382016-07-29 17:28:03 -0700160func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 return true
162}
163
Colin Crossb916a382016-07-29 17:28:03 -0700164func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700166 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700167 baseLinker: NewBaseLinker(),
168 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 }
Colin Crossb916a382016-07-29 17:28:03 -0700170 module.compiler = NewBaseCompiler()
171 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700172 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700173 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700177 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178
Dan Willemsen2e47b342016-11-17 01:02:25 -0800179 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 if ctx.Os() == android.Linux {
181 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700182 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183 }
184 } else {
185 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700186 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 }
188 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189}
190
Colin Crossb916a382016-07-29 17:28:03 -0700191func (binary *binaryDecorator) static() bool {
192 return Bool(binary.Properties.Static_executable)
193}
194
195func (binary *binaryDecorator) staticBinary() bool {
196 return binary.static()
197}
198
199func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700200 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201
Colin Crossb916a382016-07-29 17:28:03 -0700202 if ctx.Host() && !binary.static() {
Colin Cross7a108bc2017-01-30 22:44:19 -0800203 if !ctx.AConfig().IsEnvTrue("DISABLE_HOST_PIE") {
204 flags.LdFlags = append(flags.LdFlags, "-pie")
205 if ctx.Os() == android.Windows {
206 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
207 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208 }
209 }
210
211 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
212 // all code is position independent, and then those warnings get promoted to
213 // errors.
214 if ctx.Os() != android.Windows {
215 flags.CFlags = append(flags.CFlags, "-fpie")
216 }
217
Dan Willemsen2e47b342016-11-17 01:02:25 -0800218 if ctx.toolchain().Bionic() {
Colin Crossb916a382016-07-29 17:28:03 -0700219 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 // Clang driver needs -static to create static executable.
221 // However, bionic/linker uses -shared to overwrite.
222 // Linker for x86 targets does not allow coexistance of -static and -shared,
223 // so we add -static only if -shared is not used.
224 if !inList("-shared", flags.LdFlags) {
225 flags.LdFlags = append(flags.LdFlags, "-static")
226 }
227
228 flags.LdFlags = append(flags.LdFlags,
229 "-nostdlib",
230 "-Bstatic",
231 "-Wl,--gc-sections",
232 )
233
234 } else {
235 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700236 if binary.Properties.DynamicLinker != "" {
237 flags.DynamicLinker = binary.Properties.DynamicLinker
238 } else {
239 flags.DynamicLinker = "/system/bin/linker"
240 if flags.Toolchain.Is64Bit() {
241 flags.DynamicLinker += "64"
242 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 }
244 }
245
246 flags.LdFlags = append(flags.LdFlags,
247 "-pie",
248 "-nostdlib",
249 "-Bdynamic",
250 "-Wl,--gc-sections",
251 "-Wl,-z,nocopyreloc",
252 )
253 }
254 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700255 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 flags.LdFlags = append(flags.LdFlags, "-static")
257 }
258 if ctx.Darwin() {
259 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
260 }
261 }
262
263 return flags
264}
265
Colin Crossb916a382016-07-29 17:28:03 -0700266func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700267 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268
269 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
270 outputFile := android.PathForModuleOut(ctx, fileName)
271 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272
273 var linkerDeps android.Paths
274
275 sharedLibs := deps.SharedLibs
276 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
277
278 if flags.DynamicLinker != "" {
279 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
280 }
281
282 builderFlags := flagsToBuilderFlags(flags)
283
284 if binary.stripper.needsStrip(ctx) {
285 strippedOutputFile := outputFile
286 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
287 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
288 }
289
290 if binary.Properties.Prefix_symbols != "" {
291 afterPrefixSymbols := outputFile
292 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
293 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
294 flagsToBuilderFlags(flags), afterPrefixSymbols)
295 }
296
Colin Cross26c34ed2016-09-30 17:10:16 -0700297 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
298 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700299 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700300
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700301 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
303 builderFlags, outputFile)
304
Dan Willemsen581341d2017-02-09 16:16:31 -0800305 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
306 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
307 binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx))
308
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 return ret
310}
311
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700312func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
313 binary.baseInstaller.install(ctx, file)
Colin Cross9b09f242016-12-07 13:37:42 -0800314 for _, symlink := range binary.Properties.Symlinks {
315 binary.symlinks = append(binary.symlinks,
Colin Cross989c66e2016-12-08 09:43:41 -0800316 symlink+binary.Properties.Suffix+ctx.toolchain().ExecutableSuffix())
Colin Cross9b09f242016-12-07 13:37:42 -0800317 }
318
319 if binary.Properties.Symlink_preferred_arch {
320 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
321 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
322 }
323 if ctx.TargetPrimary() {
324 binary.symlinks = append(binary.symlinks, ctx.baseModuleName())
325 }
326 }
327
328 for _, symlink := range binary.symlinks {
329 ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path)
330 }
331
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700332 if ctx.Os().Class == android.Host {
333 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
334 }
335}
336
337func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
338 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339}