blob: c16dae60119e334ee28a2bb815c2ade389c95538 [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
40 DynamicLinker string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070041}
42
43func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterModuleType("cc_binary", binaryFactory)
45 android.RegisterModuleType("cc_binary_host", binaryHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070046}
47
48// Module factory for binaries
49func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070050 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070051 return module.Init()
52}
53
54// Module factory for host binaries
55func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070056 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070057 return module.Init()
58}
59
60//
61// Executables
62//
63
Colin Crossb916a382016-07-29 17:28:03 -070064type binaryDecorator struct {
65 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070066 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070067 stripper
68
69 Properties BinaryLinkerProperties
70
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070071 toolPath android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070072}
73
Colin Crossb916a382016-07-29 17:28:03 -070074var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070075
Colin Crossb916a382016-07-29 17:28:03 -070076func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070077 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 &binary.Properties,
79 &binary.stripper.StripProperties)
80
81}
82
Colin Crossb916a382016-07-29 17:28:03 -070083func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070084 stem := ctx.baseModuleName()
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 if binary.Properties.Stem != "" {
86 stem = binary.Properties.Stem
87 }
88
89 return stem + binary.Properties.Suffix
90}
91
Colin Crossb916a382016-07-29 17:28:03 -070092func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070093 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070094 if ctx.Device() {
95 if !Bool(binary.baseLinker.Properties.Nocrt) {
96 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070097 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 deps.CrtBegin = "crtbegin_static"
99 } else {
100 deps.CrtBegin = "crtbegin_dynamic"
101 }
102 deps.CrtEnd = "crtend_android"
103 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800104 // TODO(danalbert): Add generation of crt objects.
105 // For `sdk_version: "current"`, we don't actually have a
106 // freshly generated set of CRT objects. Use the last stable
107 // version.
108 version := ctx.sdkVersion()
109 if version == "current" {
110 version = ctx.AConfig().PlatformSdkVersion()
111 }
112
Colin Crossb916a382016-07-29 17:28:03 -0700113 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800114 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700116 if binary.static() {
Dan Albertebedf672016-11-08 15:06:22 -0800117 deps.CrtBegin = "ndk_crtbegin_static." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800119 deps.CrtBegin = "ndk_crtbegin_dynamic." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 }
Dan Albertebedf672016-11-08 15:06:22 -0800121 deps.CrtEnd = "ndk_crtend_android." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122 }
123 }
124 }
125
Colin Crossb916a382016-07-29 17:28:03 -0700126 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 if inList("libc++_static", deps.StaticLibs) {
128 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
129 }
130 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
131 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
132 // move them to the beginning of deps.LateStaticLibs
133 var groupLibs []string
134 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
135 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
136 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
137 }
138 }
139
Colin Crossb916a382016-07-29 17:28:03 -0700140 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
142 "from static libs or set static_executable: true")
143 }
144 return deps
145}
146
Colin Crossb916a382016-07-29 17:28:03 -0700147func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 return true
149}
150
Colin Crossb916a382016-07-29 17:28:03 -0700151func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700153 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700154 baseLinker: NewBaseLinker(),
155 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 }
Colin Crossb916a382016-07-29 17:28:03 -0700157 module.compiler = NewBaseCompiler()
158 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700159 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700160 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161}
162
Colin Crossb916a382016-07-29 17:28:03 -0700163func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700164 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 if ctx.Host() {
167 if ctx.Os() == android.Linux {
168 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700169 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 }
171 } else {
172 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700173 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 }
175 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700176
177 if binary.Properties.Symlink_preferred_arch {
178 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
179 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
180 }
Colin Cross8b74d172016-09-13 09:59:14 -0700181 if ctx.TargetPrimary() {
Colin Cross1e7d3702016-08-24 15:25:47 -0700182 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
Colin Crossce75d2c2016-10-06 16:12:58 -0700183 ctx.baseModuleName())
Colin Cross1e7d3702016-08-24 15:25:47 -0700184 }
185 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186}
187
Colin Crossb916a382016-07-29 17:28:03 -0700188func (binary *binaryDecorator) static() bool {
189 return Bool(binary.Properties.Static_executable)
190}
191
192func (binary *binaryDecorator) staticBinary() bool {
193 return binary.static()
194}
195
196func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700197 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198
Colin Crossb916a382016-07-29 17:28:03 -0700199 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 flags.LdFlags = append(flags.LdFlags, "-pie")
201 if ctx.Os() == android.Windows {
202 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
203 }
204 }
205
206 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
207 // all code is position independent, and then those warnings get promoted to
208 // errors.
209 if ctx.Os() != android.Windows {
210 flags.CFlags = append(flags.CFlags, "-fpie")
211 }
212
213 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700214 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 // Clang driver needs -static to create static executable.
216 // However, bionic/linker uses -shared to overwrite.
217 // Linker for x86 targets does not allow coexistance of -static and -shared,
218 // so we add -static only if -shared is not used.
219 if !inList("-shared", flags.LdFlags) {
220 flags.LdFlags = append(flags.LdFlags, "-static")
221 }
222
223 flags.LdFlags = append(flags.LdFlags,
224 "-nostdlib",
225 "-Bstatic",
226 "-Wl,--gc-sections",
227 )
228
229 } else {
230 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700231 if binary.Properties.DynamicLinker != "" {
232 flags.DynamicLinker = binary.Properties.DynamicLinker
233 } else {
234 flags.DynamicLinker = "/system/bin/linker"
235 if flags.Toolchain.Is64Bit() {
236 flags.DynamicLinker += "64"
237 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
239 }
240
241 flags.LdFlags = append(flags.LdFlags,
242 "-pie",
243 "-nostdlib",
244 "-Bdynamic",
245 "-Wl,--gc-sections",
246 "-Wl,-z,nocopyreloc",
247 )
248 }
249 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700250 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 flags.LdFlags = append(flags.LdFlags, "-static")
252 }
253 if ctx.Darwin() {
254 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
255 }
256 }
257
258 return flags
259}
260
Colin Crossb916a382016-07-29 17:28:03 -0700261func (binary *binaryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700262 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263
264 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
265 outputFile := android.PathForModuleOut(ctx, fileName)
266 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267
268 var linkerDeps android.Paths
269
270 sharedLibs := deps.SharedLibs
271 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
272
273 if flags.DynamicLinker != "" {
274 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
275 }
276
277 builderFlags := flagsToBuilderFlags(flags)
278
279 if binary.stripper.needsStrip(ctx) {
280 strippedOutputFile := outputFile
281 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
282 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
283 }
284
285 if binary.Properties.Prefix_symbols != "" {
286 afterPrefixSymbols := outputFile
287 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
288 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
289 flagsToBuilderFlags(flags), afterPrefixSymbols)
290 }
291
Colin Cross26c34ed2016-09-30 17:10:16 -0700292 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
293 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700294 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700295
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700296 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
298 builderFlags, outputFile)
299
300 return ret
301}
302
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700303func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
304 binary.baseInstaller.install(ctx, file)
305 if ctx.Os().Class == android.Host {
306 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
307 }
308}
309
310func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
311 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312}