blob: 2dbce66c29f696bde4e528cb5e5129ca99bbc377 [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
37}
38
39func init() {
40 soong.RegisterModuleType("cc_binary", binaryFactory)
41 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
42}
43
44// Module factory for binaries
45func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070046 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070047 return module.Init()
48}
49
50// Module factory for host binaries
51func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070052 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070053 return module.Init()
54}
55
56//
57// Executables
58//
59
Colin Crossb916a382016-07-29 17:28:03 -070060type binaryDecorator struct {
61 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070062 stripper
63
64 Properties BinaryLinkerProperties
65
66 hostToolPath android.OptionalPath
67}
68
Colin Crossb916a382016-07-29 17:28:03 -070069var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070070
Colin Crossb916a382016-07-29 17:28:03 -070071func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070072 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070073 &binary.Properties,
74 &binary.stripper.StripProperties)
75
76}
77
Colin Crossb916a382016-07-29 17:28:03 -070078func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 stem := ctx.ModuleName()
80 if binary.Properties.Stem != "" {
81 stem = binary.Properties.Stem
82 }
83
84 return stem + binary.Properties.Suffix
85}
86
Colin Crossb916a382016-07-29 17:28:03 -070087func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070088 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070089 if ctx.Device() {
90 if !Bool(binary.baseLinker.Properties.Nocrt) {
91 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070092 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 deps.CrtBegin = "crtbegin_static"
94 } else {
95 deps.CrtBegin = "crtbegin_dynamic"
96 }
97 deps.CrtEnd = "crtend_android"
98 } else {
Colin Crossb916a382016-07-29 17:28:03 -070099 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
101 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700102 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
104 } else {
105 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
106 }
107 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
108 }
109 }
110 }
111
Colin Crossb916a382016-07-29 17:28:03 -0700112 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 if inList("libc++_static", deps.StaticLibs) {
114 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
115 }
116 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
117 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
118 // move them to the beginning of deps.LateStaticLibs
119 var groupLibs []string
120 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
121 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
122 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
123 }
124 }
125
Colin Crossb916a382016-07-29 17:28:03 -0700126 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
128 "from static libs or set static_executable: true")
129 }
130 return deps
131}
132
Colin Crossb916a382016-07-29 17:28:03 -0700133func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 return true
135}
136
Colin Crossb916a382016-07-29 17:28:03 -0700137func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700139 binary := &binaryDecorator{
140 baseLinker: NewBaseLinker(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 }
Colin Crossb916a382016-07-29 17:28:03 -0700142 module.compiler = NewBaseCompiler()
143 module.linker = binary
144 module.installer = NewBaseInstaller("bin", "", InstallInSystem)
145 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146}
147
Colin Crossb916a382016-07-29 17:28:03 -0700148func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700149 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151 if ctx.Host() {
152 if ctx.Os() == android.Linux {
153 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700154 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
156 } else {
157 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700158 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 }
160 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161}
162
Colin Crossb916a382016-07-29 17:28:03 -0700163func (binary *binaryDecorator) static() bool {
164 return Bool(binary.Properties.Static_executable)
165}
166
167func (binary *binaryDecorator) staticBinary() bool {
168 return binary.static()
169}
170
171func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700172 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173
Colin Crossb916a382016-07-29 17:28:03 -0700174 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 flags.LdFlags = append(flags.LdFlags, "-pie")
176 if ctx.Os() == android.Windows {
177 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
178 }
179 }
180
181 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
182 // all code is position independent, and then those warnings get promoted to
183 // errors.
184 if ctx.Os() != android.Windows {
185 flags.CFlags = append(flags.CFlags, "-fpie")
186 }
187
188 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700189 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 // Clang driver needs -static to create static executable.
191 // However, bionic/linker uses -shared to overwrite.
192 // Linker for x86 targets does not allow coexistance of -static and -shared,
193 // so we add -static only if -shared is not used.
194 if !inList("-shared", flags.LdFlags) {
195 flags.LdFlags = append(flags.LdFlags, "-static")
196 }
197
198 flags.LdFlags = append(flags.LdFlags,
199 "-nostdlib",
200 "-Bstatic",
201 "-Wl,--gc-sections",
202 )
203
204 } else {
205 if flags.DynamicLinker == "" {
206 flags.DynamicLinker = "/system/bin/linker"
207 if flags.Toolchain.Is64Bit() {
208 flags.DynamicLinker += "64"
209 }
210 }
211
212 flags.LdFlags = append(flags.LdFlags,
213 "-pie",
214 "-nostdlib",
215 "-Bdynamic",
216 "-Wl,--gc-sections",
217 "-Wl,-z,nocopyreloc",
218 )
219 }
220 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700221 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 flags.LdFlags = append(flags.LdFlags, "-static")
223 }
224 if ctx.Darwin() {
225 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
226 }
227 }
228
229 return flags
230}
231
Colin Crossb916a382016-07-29 17:28:03 -0700232func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
234
235 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
236 outputFile := android.PathForModuleOut(ctx, fileName)
237 ret := outputFile
238 if ctx.Os().Class == android.Host {
239 binary.hostToolPath = android.OptionalPathForPath(outputFile)
240 }
241
242 var linkerDeps android.Paths
243
244 sharedLibs := deps.SharedLibs
245 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
246
247 if flags.DynamicLinker != "" {
248 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
249 }
250
251 builderFlags := flagsToBuilderFlags(flags)
252
253 if binary.stripper.needsStrip(ctx) {
254 strippedOutputFile := outputFile
255 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
256 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
257 }
258
259 if binary.Properties.Prefix_symbols != "" {
260 afterPrefixSymbols := outputFile
261 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
262 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
263 flagsToBuilderFlags(flags), afterPrefixSymbols)
264 }
265
266 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
267 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
268 builderFlags, outputFile)
269
270 return ret
271}
272
Colin Crossb916a382016-07-29 17:28:03 -0700273func (binary *binaryDecorator) HostToolPath() android.OptionalPath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 return binary.hostToolPath
275}