Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | type 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 Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 36 | |
| 37 | // if set, install a symlink to the preferred architecture |
| 38 | Symlink_preferred_arch bool |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 39 | |
| 40 | DynamicLinker string `blueprint:"mutated"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 44 | android.RegisterModuleType("cc_binary", binaryFactory) |
| 45 | android.RegisterModuleType("cc_binary_host", binaryHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Module factory for binaries |
| 49 | func binaryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 50 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 51 | return module.Init() |
| 52 | } |
| 53 | |
| 54 | // Module factory for host binaries |
| 55 | func binaryHostFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 56 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 57 | return module.Init() |
| 58 | } |
| 59 | |
| 60 | // |
| 61 | // Executables |
| 62 | // |
| 63 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 64 | type binaryDecorator struct { |
| 65 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 66 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 67 | stripper |
| 68 | |
| 69 | Properties BinaryLinkerProperties |
| 70 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 71 | toolPath android.OptionalPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 74 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 75 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 76 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 77 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 78 | &binary.Properties, |
| 79 | &binary.stripper.StripProperties) |
| 80 | |
| 81 | } |
| 82 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 83 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 84 | stem := ctx.baseModuleName() |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 85 | if binary.Properties.Stem != "" { |
| 86 | stem = binary.Properties.Stem |
| 87 | } |
| 88 | |
| 89 | return stem + binary.Properties.Suffix |
| 90 | } |
| 91 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 92 | func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 93 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 94 | if ctx.Device() { |
| 95 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
| 96 | if !ctx.sdk() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 97 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 98 | deps.CrtBegin = "crtbegin_static" |
| 99 | } else { |
| 100 | deps.CrtBegin = "crtbegin_dynamic" |
| 101 | } |
| 102 | deps.CrtEnd = "crtend_android" |
| 103 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 104 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 113 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 114 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 116 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 117 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 118 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 119 | deps.CrtBegin = "ndk_crtbegin_dynamic." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 120 | } |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 121 | deps.CrtEnd = "ndk_crtend_android." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 126 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 127 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 140 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 147 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 148 | return true |
| 149 | } |
| 150 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 151 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 152 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 153 | binary := &binaryDecorator{ |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 154 | baseLinker: NewBaseLinker(), |
| 155 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 156 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 157 | module.compiler = NewBaseCompiler() |
| 158 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 159 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 160 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 163 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 164 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 165 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | if ctx.Host() { |
| 167 | if ctx.Os() == android.Linux { |
| 168 | if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 169 | binary.Properties.Static_executable = proptools.BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 170 | } |
| 171 | } else { |
| 172 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 173 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 176 | |
| 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 Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 181 | if ctx.TargetPrimary() { |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 182 | binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 183 | ctx.baseModuleName()) |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 188 | func (binary *binaryDecorator) static() bool { |
| 189 | return Bool(binary.Properties.Static_executable) |
| 190 | } |
| 191 | |
| 192 | func (binary *binaryDecorator) staticBinary() bool { |
| 193 | return binary.static() |
| 194 | } |
| 195 | |
| 196 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 197 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 198 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 199 | if ctx.Host() && !binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 214 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 215 | // 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 Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 231 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 238 | } |
| 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 250 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 251 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 261 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 262 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 263 | |
| 264 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 265 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 266 | ret := outputFile |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 267 | |
| 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 Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 292 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 293 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 294 | linkerDeps = append(linkerDeps, objs.tidyFiles...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 295 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 296 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 297 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
| 298 | builderFlags, outputFile) |
| 299 | |
| 300 | return ret |
| 301 | } |
| 302 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 303 | func (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 | |
| 310 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 311 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 312 | } |