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 | |
| 21 | "android/soong" |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | type 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 Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 37 | |
| 38 | // if set, install a symlink to the preferred architecture |
| 39 | Symlink_preferred_arch bool |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame^] | 40 | |
| 41 | DynamicLinker string `blueprint:"mutated"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | func init() { |
| 45 | soong.RegisterModuleType("cc_binary", binaryFactory) |
| 46 | soong.RegisterModuleType("cc_binary_host", binaryHostFactory) |
| 47 | } |
| 48 | |
| 49 | // Module factory for binaries |
| 50 | func binaryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 51 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 52 | return module.Init() |
| 53 | } |
| 54 | |
| 55 | // Module factory for host binaries |
| 56 | func binaryHostFactory() (blueprint.Module, []interface{}) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 57 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 58 | return module.Init() |
| 59 | } |
| 60 | |
| 61 | // |
| 62 | // Executables |
| 63 | // |
| 64 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 65 | type binaryDecorator struct { |
| 66 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 67 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 68 | stripper |
| 69 | |
| 70 | Properties BinaryLinkerProperties |
| 71 | |
| 72 | hostToolPath android.OptionalPath |
| 73 | } |
| 74 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 75 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 76 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 77 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 78 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 79 | &binary.Properties, |
| 80 | &binary.stripper.StripProperties) |
| 81 | |
| 82 | } |
| 83 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 84 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 85 | stem := ctx.ModuleName() |
| 86 | if binary.Properties.Stem != "" { |
| 87 | stem = binary.Properties.Stem |
| 88 | } |
| 89 | |
| 90 | return stem + binary.Properties.Suffix |
| 91 | } |
| 92 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 93 | func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 94 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 95 | if ctx.Device() { |
| 96 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
| 97 | if !ctx.sdk() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 98 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 99 | deps.CrtBegin = "crtbegin_static" |
| 100 | } else { |
| 101 | deps.CrtBegin = "crtbegin_dynamic" |
| 102 | } |
| 103 | deps.CrtEnd = "crtend_android" |
| 104 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 105 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 106 | deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion() |
| 107 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 108 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 109 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 118 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 119 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 132 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 133 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 139 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 140 | return true |
| 141 | } |
| 142 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 143 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 145 | binary := &binaryDecorator{ |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 146 | baseLinker: NewBaseLinker(), |
| 147 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 148 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 149 | module.compiler = NewBaseCompiler() |
| 150 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 151 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 152 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 155 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 156 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 157 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | if ctx.Host() { |
| 159 | if ctx.Os() == android.Linux { |
| 160 | if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 161 | binary.Properties.Static_executable = proptools.BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 162 | } |
| 163 | } else { |
| 164 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 165 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 168 | |
| 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 Cross | 20780c8 | 2016-09-02 14:06:30 -0700 | [diff] [blame] | 173 | prefer32 := false |
| 174 | if ctx.Device() { |
| 175 | prefer32 = ctx.AConfig().DevicePrefer32BitExecutables() |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 176 | } |
Colin Cross | 20780c8 | 2016-09-02 14:06:30 -0700 | [diff] [blame] | 177 | if ctx.PrimaryArch() != prefer32 { |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 178 | binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks, |
| 179 | ctx.ModuleName()) |
| 180 | } |
| 181 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 184 | func (binary *binaryDecorator) static() bool { |
| 185 | return Bool(binary.Properties.Static_executable) |
| 186 | } |
| 187 | |
| 188 | func (binary *binaryDecorator) staticBinary() bool { |
| 189 | return binary.static() |
| 190 | } |
| 191 | |
| 192 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 193 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 194 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | if ctx.Host() && !binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 210 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 211 | // 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 Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame^] | 227 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 234 | } |
| 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 246 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 247 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 257 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 258 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 298 | func (binary *binaryDecorator) HostToolPath() android.OptionalPath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 299 | return binary.hostToolPath |
| 300 | } |