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 { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 104 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 105 | deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion() |
| 106 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 107 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 108 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 117 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 118 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 131 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 132 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 138 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | return true |
| 140 | } |
| 141 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 142 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 143 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 144 | binary := &binaryDecorator{ |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 145 | baseLinker: NewBaseLinker(), |
| 146 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 147 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 148 | module.compiler = NewBaseCompiler() |
| 149 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 150 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 151 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 154 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 155 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 156 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 157 | if ctx.Host() { |
| 158 | if ctx.Os() == android.Linux { |
| 159 | if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 160 | binary.Properties.Static_executable = proptools.BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 161 | } |
| 162 | } else { |
| 163 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 164 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 167 | |
| 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 Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 172 | if ctx.TargetPrimary() { |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 173 | binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks, |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 174 | ctx.baseModuleName()) |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 179 | func (binary *binaryDecorator) static() bool { |
| 180 | return Bool(binary.Properties.Static_executable) |
| 181 | } |
| 182 | |
| 183 | func (binary *binaryDecorator) staticBinary() bool { |
| 184 | return binary.static() |
| 185 | } |
| 186 | |
| 187 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 188 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 189 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 190 | if ctx.Host() && !binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 191 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 205 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 206 | // 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 Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 222 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 229 | } |
| 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 241 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 242 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 252 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 253 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 258 | |
| 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 Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 283 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 284 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
| 285 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 286 | 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 Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 293 | func (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 | |
| 300 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 301 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 302 | } |