blob: b6d66dd15c7af67fa0235cfa80f9634cd692c416 [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
21 "android/soong"
22 "android/soong/android"
23)
24
25type BinaryLinkerProperties struct {
26 // compile executable with -static
27 Static_executable *bool `android:"arch_variant"`
28
29 // set the name of the output
30 Stem string `android:"arch_variant"`
31
32 // append to the name of the output
33 Suffix string `android:"arch_variant"`
34
35 // if set, add an extra objcopy --prefix-symbols= step
36 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070037
38 // if set, install a symlink to the preferred architecture
39 Symlink_preferred_arch bool
Colin Cross4d9c2d12016-07-29 12:48:20 -070040}
41
42func init() {
43 soong.RegisterModuleType("cc_binary", binaryFactory)
44 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
45}
46
47// Module factory for binaries
48func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070049 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 return module.Init()
51}
52
53// Module factory for host binaries
54func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070055 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070056 return module.Init()
57}
58
59//
60// Executables
61//
62
Colin Crossb916a382016-07-29 17:28:03 -070063type binaryDecorator struct {
64 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070065 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 stripper
67
68 Properties BinaryLinkerProperties
69
70 hostToolPath android.OptionalPath
71}
72
Colin Crossb916a382016-07-29 17:28:03 -070073var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070074
Colin Crossb916a382016-07-29 17:28:03 -070075func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070076 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 &binary.Properties,
78 &binary.stripper.StripProperties)
79
80}
81
Colin Crossb916a382016-07-29 17:28:03 -070082func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 stem := ctx.ModuleName()
84 if binary.Properties.Stem != "" {
85 stem = binary.Properties.Stem
86 }
87
88 return stem + binary.Properties.Suffix
89}
90
Colin Crossb916a382016-07-29 17:28:03 -070091func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070092 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 if ctx.Device() {
94 if !Bool(binary.baseLinker.Properties.Nocrt) {
95 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070096 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 deps.CrtBegin = "crtbegin_static"
98 } else {
99 deps.CrtBegin = "crtbegin_dynamic"
100 }
101 deps.CrtEnd = "crtend_android"
102 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700103 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
105 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700106 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
108 } else {
109 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
110 }
111 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
112 }
113 }
114 }
115
Colin Crossb916a382016-07-29 17:28:03 -0700116 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 if inList("libc++_static", deps.StaticLibs) {
118 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
119 }
120 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
121 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
122 // move them to the beginning of deps.LateStaticLibs
123 var groupLibs []string
124 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
125 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
126 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
127 }
128 }
129
Colin Crossb916a382016-07-29 17:28:03 -0700130 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
132 "from static libs or set static_executable: true")
133 }
134 return deps
135}
136
Colin Crossb916a382016-07-29 17:28:03 -0700137func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 return true
139}
140
Colin Crossb916a382016-07-29 17:28:03 -0700141func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700143 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700144 baseLinker: NewBaseLinker(),
145 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146 }
Colin Crossb916a382016-07-29 17:28:03 -0700147 module.compiler = NewBaseCompiler()
148 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700149 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700150 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151}
152
Colin Crossb916a382016-07-29 17:28:03 -0700153func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700154 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 if ctx.Host() {
157 if ctx.Os() == android.Linux {
158 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700159 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 }
161 } else {
162 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700163 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 }
165 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700166
167 if binary.Properties.Symlink_preferred_arch {
168 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
169 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
170 }
Colin Cross20780c82016-09-02 14:06:30 -0700171 prefer32 := false
172 if ctx.Device() {
173 prefer32 = ctx.AConfig().DevicePrefer32BitExecutables()
Colin Cross1e7d3702016-08-24 15:25:47 -0700174 }
Colin Cross20780c82016-09-02 14:06:30 -0700175 if ctx.PrimaryArch() != prefer32 {
Colin Cross1e7d3702016-08-24 15:25:47 -0700176 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
177 ctx.ModuleName())
178 }
179 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180}
181
Colin Crossb916a382016-07-29 17:28:03 -0700182func (binary *binaryDecorator) static() bool {
183 return Bool(binary.Properties.Static_executable)
184}
185
186func (binary *binaryDecorator) staticBinary() bool {
187 return binary.static()
188}
189
190func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700191 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192
Colin Crossb916a382016-07-29 17:28:03 -0700193 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 flags.LdFlags = append(flags.LdFlags, "-pie")
195 if ctx.Os() == android.Windows {
196 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
197 }
198 }
199
200 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
201 // all code is position independent, and then those warnings get promoted to
202 // errors.
203 if ctx.Os() != android.Windows {
204 flags.CFlags = append(flags.CFlags, "-fpie")
205 }
206
207 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700208 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 // Clang driver needs -static to create static executable.
210 // However, bionic/linker uses -shared to overwrite.
211 // Linker for x86 targets does not allow coexistance of -static and -shared,
212 // so we add -static only if -shared is not used.
213 if !inList("-shared", flags.LdFlags) {
214 flags.LdFlags = append(flags.LdFlags, "-static")
215 }
216
217 flags.LdFlags = append(flags.LdFlags,
218 "-nostdlib",
219 "-Bstatic",
220 "-Wl,--gc-sections",
221 )
222
223 } else {
224 if flags.DynamicLinker == "" {
225 flags.DynamicLinker = "/system/bin/linker"
226 if flags.Toolchain.Is64Bit() {
227 flags.DynamicLinker += "64"
228 }
229 }
230
231 flags.LdFlags = append(flags.LdFlags,
232 "-pie",
233 "-nostdlib",
234 "-Bdynamic",
235 "-Wl,--gc-sections",
236 "-Wl,-z,nocopyreloc",
237 )
238 }
239 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700240 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 flags.LdFlags = append(flags.LdFlags, "-static")
242 }
243 if ctx.Darwin() {
244 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
245 }
246 }
247
248 return flags
249}
250
Colin Crossb916a382016-07-29 17:28:03 -0700251func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
253
254 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
255 outputFile := android.PathForModuleOut(ctx, fileName)
256 ret := outputFile
257 if ctx.Os().Class == android.Host {
258 binary.hostToolPath = android.OptionalPathForPath(outputFile)
259 }
260
261 var linkerDeps android.Paths
262
263 sharedLibs := deps.SharedLibs
264 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
265
266 if flags.DynamicLinker != "" {
267 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
268 }
269
270 builderFlags := flagsToBuilderFlags(flags)
271
272 if binary.stripper.needsStrip(ctx) {
273 strippedOutputFile := outputFile
274 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
275 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
276 }
277
278 if binary.Properties.Prefix_symbols != "" {
279 afterPrefixSymbols := outputFile
280 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
281 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
282 flagsToBuilderFlags(flags), afterPrefixSymbols)
283 }
284
285 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
286 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
287 builderFlags, outputFile)
288
289 return ret
290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292func (binary *binaryDecorator) HostToolPath() android.OptionalPath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 return binary.hostToolPath
294}