blob: 0965886ff4c8b31f47c13816b4378a9c4733a597 [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 Cross4d9c2d12016-07-29 12:48:20 -070084 stem := ctx.ModuleName()
85 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 {
Colin Crossb916a382016-07-29 17:28:03 -0700104 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
106 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700107 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
109 } else {
110 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
111 }
112 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
113 }
114 }
115 }
116
Colin Crossb916a382016-07-29 17:28:03 -0700117 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 if inList("libc++_static", deps.StaticLibs) {
119 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
120 }
121 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
122 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
123 // move them to the beginning of deps.LateStaticLibs
124 var groupLibs []string
125 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
126 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
127 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
128 }
129 }
130
Colin Crossb916a382016-07-29 17:28:03 -0700131 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
133 "from static libs or set static_executable: true")
134 }
135 return deps
136}
137
Colin Crossb916a382016-07-29 17:28:03 -0700138func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 return true
140}
141
Colin Crossb916a382016-07-29 17:28:03 -0700142func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700144 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700145 baseLinker: NewBaseLinker(),
146 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147 }
Colin Crossb916a382016-07-29 17:28:03 -0700148 module.compiler = NewBaseCompiler()
149 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700150 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700151 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152}
153
Colin Crossb916a382016-07-29 17:28:03 -0700154func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700155 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 if ctx.Host() {
158 if ctx.Os() == android.Linux {
159 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700160 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 }
162 } else {
163 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700164 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 }
166 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700167
168 if binary.Properties.Symlink_preferred_arch {
169 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
170 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
171 }
Colin Cross8b74d172016-09-13 09:59:14 -0700172 if ctx.TargetPrimary() {
Colin Cross1e7d3702016-08-24 15:25:47 -0700173 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
174 ctx.ModuleName())
175 }
176 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179func (binary *binaryDecorator) static() bool {
180 return Bool(binary.Properties.Static_executable)
181}
182
183func (binary *binaryDecorator) staticBinary() bool {
184 return binary.static()
185}
186
187func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700188 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189
Colin Crossb916a382016-07-29 17:28:03 -0700190 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 flags.LdFlags = append(flags.LdFlags, "-pie")
192 if ctx.Os() == android.Windows {
193 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
194 }
195 }
196
197 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
198 // all code is position independent, and then those warnings get promoted to
199 // errors.
200 if ctx.Os() != android.Windows {
201 flags.CFlags = append(flags.CFlags, "-fpie")
202 }
203
204 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700205 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 // Clang driver needs -static to create static executable.
207 // However, bionic/linker uses -shared to overwrite.
208 // Linker for x86 targets does not allow coexistance of -static and -shared,
209 // so we add -static only if -shared is not used.
210 if !inList("-shared", flags.LdFlags) {
211 flags.LdFlags = append(flags.LdFlags, "-static")
212 }
213
214 flags.LdFlags = append(flags.LdFlags,
215 "-nostdlib",
216 "-Bstatic",
217 "-Wl,--gc-sections",
218 )
219
220 } else {
221 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700222 if binary.Properties.DynamicLinker != "" {
223 flags.DynamicLinker = binary.Properties.DynamicLinker
224 } else {
225 flags.DynamicLinker = "/system/bin/linker"
226 if flags.Toolchain.Is64Bit() {
227 flags.DynamicLinker += "64"
228 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 }
230 }
231
232 flags.LdFlags = append(flags.LdFlags,
233 "-pie",
234 "-nostdlib",
235 "-Bdynamic",
236 "-Wl,--gc-sections",
237 "-Wl,-z,nocopyreloc",
238 )
239 }
240 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700241 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 flags.LdFlags = append(flags.LdFlags, "-static")
243 }
244 if ctx.Darwin() {
245 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
246 }
247 }
248
249 return flags
250}
251
Colin Crossb916a382016-07-29 17:28:03 -0700252func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
254
255 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
256 outputFile := android.PathForModuleOut(ctx, fileName)
257 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258
259 var linkerDeps android.Paths
260
261 sharedLibs := deps.SharedLibs
262 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
263
264 if flags.DynamicLinker != "" {
265 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
266 }
267
268 builderFlags := flagsToBuilderFlags(flags)
269
270 if binary.stripper.needsStrip(ctx) {
271 strippedOutputFile := outputFile
272 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
273 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
274 }
275
276 if binary.Properties.Prefix_symbols != "" {
277 afterPrefixSymbols := outputFile
278 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
279 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
280 flagsToBuilderFlags(flags), afterPrefixSymbols)
281 }
282
Colin Cross26c34ed2016-09-30 17:10:16 -0700283 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
284 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
285
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
287 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
288 builderFlags, outputFile)
289
290 return ret
291}
292
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700293func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
294 binary.baseInstaller.install(ctx, file)
295 if ctx.Os().Class == android.Host {
296 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
297 }
298}
299
300func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
301 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302}