blob: d31cfd68e7afdfc0caf3b7279b7a49cdc0553b35 [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 Cross522e3732016-09-07 13:14:06 -070040
41 DynamicLinker string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070042}
43
44func init() {
45 soong.RegisterModuleType("cc_binary", binaryFactory)
46 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
47}
48
49// Module factory for binaries
50func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070051 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 return module.Init()
53}
54
55// Module factory for host binaries
56func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070057 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 return module.Init()
59}
60
61//
62// Executables
63//
64
Colin Crossb916a382016-07-29 17:28:03 -070065type binaryDecorator struct {
66 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070067 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 stripper
69
70 Properties BinaryLinkerProperties
71
72 hostToolPath android.OptionalPath
73}
74
Colin Crossb916a382016-07-29 17:28:03 -070075var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070076
Colin Crossb916a382016-07-29 17:28:03 -070077func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070078 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 &binary.Properties,
80 &binary.stripper.StripProperties)
81
82}
83
Colin Crossb916a382016-07-29 17:28:03 -070084func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 stem := ctx.ModuleName()
86 if binary.Properties.Stem != "" {
87 stem = binary.Properties.Stem
88 }
89
90 return stem + binary.Properties.Suffix
91}
92
Colin Crossb916a382016-07-29 17:28:03 -070093func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070094 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 if ctx.Device() {
96 if !Bool(binary.baseLinker.Properties.Nocrt) {
97 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070098 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070099 deps.CrtBegin = "crtbegin_static"
100 } else {
101 deps.CrtBegin = "crtbegin_dynamic"
102 }
103 deps.CrtEnd = "crtend_android"
104 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700105 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
107 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700108 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
110 } else {
111 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
112 }
113 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
114 }
115 }
116 }
117
Colin Crossb916a382016-07-29 17:28:03 -0700118 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 if inList("libc++_static", deps.StaticLibs) {
120 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
121 }
122 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
123 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
124 // move them to the beginning of deps.LateStaticLibs
125 var groupLibs []string
126 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
127 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
128 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
129 }
130 }
131
Colin Crossb916a382016-07-29 17:28:03 -0700132 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
134 "from static libs or set static_executable: true")
135 }
136 return deps
137}
138
Colin Crossb916a382016-07-29 17:28:03 -0700139func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return true
141}
142
Colin Crossb916a382016-07-29 17:28:03 -0700143func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700145 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700146 baseLinker: NewBaseLinker(),
147 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
Colin Crossb916a382016-07-29 17:28:03 -0700149 module.compiler = NewBaseCompiler()
150 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700151 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700152 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153}
154
Colin Crossb916a382016-07-29 17:28:03 -0700155func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700156 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 if ctx.Host() {
159 if ctx.Os() == android.Linux {
160 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700161 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163 } else {
164 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700165 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 }
167 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700168
169 if binary.Properties.Symlink_preferred_arch {
170 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
171 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
172 }
Colin Cross20780c82016-09-02 14:06:30 -0700173 prefer32 := false
174 if ctx.Device() {
175 prefer32 = ctx.AConfig().DevicePrefer32BitExecutables()
Colin Cross1e7d3702016-08-24 15:25:47 -0700176 }
Colin Cross20780c82016-09-02 14:06:30 -0700177 if ctx.PrimaryArch() != prefer32 {
Colin Cross1e7d3702016-08-24 15:25:47 -0700178 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
179 ctx.ModuleName())
180 }
181 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184func (binary *binaryDecorator) static() bool {
185 return Bool(binary.Properties.Static_executable)
186}
187
188func (binary *binaryDecorator) staticBinary() bool {
189 return binary.static()
190}
191
192func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700193 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194
Colin Crossb916a382016-07-29 17:28:03 -0700195 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 flags.LdFlags = append(flags.LdFlags, "-pie")
197 if ctx.Os() == android.Windows {
198 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
199 }
200 }
201
202 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
203 // all code is position independent, and then those warnings get promoted to
204 // errors.
205 if ctx.Os() != android.Windows {
206 flags.CFlags = append(flags.CFlags, "-fpie")
207 }
208
209 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700210 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 // Clang driver needs -static to create static executable.
212 // However, bionic/linker uses -shared to overwrite.
213 // Linker for x86 targets does not allow coexistance of -static and -shared,
214 // so we add -static only if -shared is not used.
215 if !inList("-shared", flags.LdFlags) {
216 flags.LdFlags = append(flags.LdFlags, "-static")
217 }
218
219 flags.LdFlags = append(flags.LdFlags,
220 "-nostdlib",
221 "-Bstatic",
222 "-Wl,--gc-sections",
223 )
224
225 } else {
226 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700227 if binary.Properties.DynamicLinker != "" {
228 flags.DynamicLinker = binary.Properties.DynamicLinker
229 } else {
230 flags.DynamicLinker = "/system/bin/linker"
231 if flags.Toolchain.Is64Bit() {
232 flags.DynamicLinker += "64"
233 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 }
235 }
236
237 flags.LdFlags = append(flags.LdFlags,
238 "-pie",
239 "-nostdlib",
240 "-Bdynamic",
241 "-Wl,--gc-sections",
242 "-Wl,-z,nocopyreloc",
243 )
244 }
245 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700246 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 flags.LdFlags = append(flags.LdFlags, "-static")
248 }
249 if ctx.Darwin() {
250 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
251 }
252 }
253
254 return flags
255}
256
Colin Crossb916a382016-07-29 17:28:03 -0700257func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
259
260 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
261 outputFile := android.PathForModuleOut(ctx, fileName)
262 ret := outputFile
263 if ctx.Os().Class == android.Host {
264 binary.hostToolPath = android.OptionalPathForPath(outputFile)
265 }
266
267 var linkerDeps android.Paths
268
269 sharedLibs := deps.SharedLibs
270 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
271
272 if flags.DynamicLinker != "" {
273 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
274 }
275
276 builderFlags := flagsToBuilderFlags(flags)
277
278 if binary.stripper.needsStrip(ctx) {
279 strippedOutputFile := outputFile
280 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
281 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
282 }
283
284 if binary.Properties.Prefix_symbols != "" {
285 afterPrefixSymbols := outputFile
286 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
287 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
288 flagsToBuilderFlags(flags), afterPrefixSymbols)
289 }
290
291 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
292 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
293 builderFlags, outputFile)
294
295 return ret
296}
297
Colin Crossb916a382016-07-29 17:28:03 -0700298func (binary *binaryDecorator) HostToolPath() android.OptionalPath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299 return binary.hostToolPath
300}