| 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 ( | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 18 | "android/soong/android" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | type BinaryLinkerProperties struct { | 
|  | 22 | // compile executable with -static | 
|  | 23 | Static_executable *bool `android:"arch_variant"` | 
|  | 24 |  | 
|  | 25 | // set the name of the output | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 26 | Stem *string `android:"arch_variant"` | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | // append to the name of the output | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 29 | Suffix *string `android:"arch_variant"` | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | // if set, add an extra objcopy --prefix-symbols= step | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 32 | Prefix_symbols *string | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 33 |  | 
| dimitry | feda20b | 2017-08-29 15:00:01 +0200 | [diff] [blame] | 34 | // local file name to pass to the linker as --version_script | 
|  | 35 | Version_script *string `android:"arch_variant"` | 
|  | 36 |  | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 37 | // if set, install a symlink to the preferred architecture | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 38 | Symlink_preferred_arch *bool | 
| Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 39 |  | 
| Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 40 | // install symlinks to the binary.  Symlink names will have the suffix and the binary | 
|  | 41 | // extension (if any) appended | 
|  | 42 | Symlinks []string `android:"arch_variant"` | 
|  | 43 |  | 
| Colin Cross | 7a108bc | 2017-01-30 22:44:19 -0800 | [diff] [blame] | 44 | // do not pass -pie | 
|  | 45 | No_pie *bool `android:"arch_variant"` | 
|  | 46 |  | 
| Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 47 | DynamicLinker string `blueprint:"mutated"` | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | func init() { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 51 | android.RegisterModuleType("cc_binary", binaryFactory) | 
|  | 52 | android.RegisterModuleType("cc_binary_host", binaryHostFactory) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | // Module factory for binaries | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 56 | func binaryFactory() android.Module { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 57 | module, _ := NewBinary(android.HostAndDeviceSupported) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 58 | return module.Init() | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | // Module factory for host binaries | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 62 | func binaryHostFactory() android.Module { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 63 | module, _ := NewBinary(android.HostSupported) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 64 | return module.Init() | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // | 
|  | 68 | // Executables | 
|  | 69 | // | 
|  | 70 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 71 | type binaryDecorator struct { | 
|  | 72 | *baseLinker | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 73 | *baseInstaller | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 74 | stripper | 
|  | 75 |  | 
|  | 76 | Properties BinaryLinkerProperties | 
|  | 77 |  | 
| Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 78 | toolPath android.OptionalPath | 
| Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS | 
|  | 81 | symlinks []string | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | // Output archive of gcno coverage information | 
|  | 84 | coverageOutputFile android.OptionalPath | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 87 | var _ linker = (*binaryDecorator)(nil) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 88 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 89 | func (binary *binaryDecorator) linkerProps() []interface{} { | 
| Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 90 | return append(binary.baseLinker.linkerProps(), | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 91 | &binary.Properties, | 
|  | 92 | &binary.stripper.StripProperties) | 
|  | 93 |  | 
|  | 94 | } | 
|  | 95 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 96 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 97 | stem := ctx.baseModuleName() | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 98 | if String(binary.Properties.Stem) != "" { | 
|  | 99 | stem = String(binary.Properties.Stem) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 102 | return stem + String(binary.Properties.Suffix) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 105 | func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 106 | deps = binary.baseLinker.linkerDeps(ctx, deps) | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 107 | if ctx.toolchain().Bionic() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 108 | if !Bool(binary.baseLinker.Properties.Nocrt) { | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 109 | if !ctx.useSdk() { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 110 | if binary.static() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 111 | deps.CrtBegin = "crtbegin_static" | 
|  | 112 | } else { | 
|  | 113 | deps.CrtBegin = "crtbegin_dynamic" | 
|  | 114 | } | 
|  | 115 | deps.CrtEnd = "crtend_android" | 
|  | 116 | } else { | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 117 | // TODO(danalbert): Add generation of crt objects. | 
|  | 118 | // For `sdk_version: "current"`, we don't actually have a | 
|  | 119 | // freshly generated set of CRT objects. Use the last stable | 
|  | 120 | // version. | 
|  | 121 | version := ctx.sdkVersion() | 
|  | 122 | if version == "current" { | 
| Jayant Chowdhary | 6e8115a | 2017-05-09 10:21:52 -0700 | [diff] [blame] | 123 | version = getCurrentNdkPrebuiltVersion(ctx) | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 126 | if binary.static() { | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 127 | deps.CrtBegin = "ndk_crtbegin_static." + version | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 128 | } else { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 129 | if binary.static() { | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 130 | deps.CrtBegin = "ndk_crtbegin_static." + version | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | } else { | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 132 | deps.CrtBegin = "ndk_crtbegin_dynamic." + version | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 133 | } | 
| Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 134 | deps.CrtEnd = "ndk_crtend_android." + version | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 135 | } | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 139 | if binary.static() { | 
| Dan Albert | dc2597d | 2017-01-26 17:44:26 -0800 | [diff] [blame] | 140 | if ctx.selectedStl() == "libc++_static" { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") | 
|  | 142 | } | 
|  | 143 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with | 
|  | 144 | // --start-group/--end-group along with libgcc.  If they are in deps.StaticLibs, | 
|  | 145 | // move them to the beginning of deps.LateStaticLibs | 
|  | 146 | var groupLibs []string | 
|  | 147 | deps.StaticLibs, groupLibs = filterList(deps.StaticLibs, | 
|  | 148 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) | 
|  | 149 | deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...) | 
|  | 150 | } | 
| Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 151 |  | 
|  | 152 | if ctx.Os() == android.LinuxBionic && !binary.static() { | 
|  | 153 | deps.LinkerScript = "host_bionic_linker_script" | 
|  | 154 | } | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 157 | if !binary.static() && inList("libc", deps.StaticLibs) { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + | 
|  | 159 | "from static libs or set static_executable: true") | 
|  | 160 | } | 
| Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 161 |  | 
|  | 162 | android.ExtractSourceDeps(ctx, binary.Properties.Version_script) | 
|  | 163 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 164 | return deps | 
|  | 165 | } | 
|  | 166 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 167 | func (binary *binaryDecorator) isDependencyRoot() bool { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 168 | return true | 
|  | 169 | } | 
|  | 170 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 171 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 172 | module := newModule(hod, android.MultilibFirst) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 173 | binary := &binaryDecorator{ | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 174 | baseLinker:    NewBaseLinker(), | 
|  | 175 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 176 | } | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 177 | module.compiler = NewBaseCompiler() | 
|  | 178 | module.linker = binary | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 179 | module.installer = binary | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 180 | return module, binary | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 183 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { | 
| Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 184 | binary.baseLinker.linkerInit(ctx) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 185 |  | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 186 | if !ctx.toolchain().Bionic() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 187 | if ctx.Os() == android.Linux { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 188 | if binary.Properties.Static_executable == nil && Bool(ctx.Config().ProductVariables.HostStaticBinaries) { | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 189 | binary.Properties.Static_executable = BoolPtr(true) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 190 | } | 
|  | 191 | } else { | 
|  | 192 | // Static executables are not supported on Darwin or Windows | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 193 | binary.Properties.Static_executable = nil | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 194 | } | 
|  | 195 | } | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 198 | func (binary *binaryDecorator) static() bool { | 
|  | 199 | return Bool(binary.Properties.Static_executable) | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | func (binary *binaryDecorator) staticBinary() bool { | 
|  | 203 | return binary.static() | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 207 | flags = binary.baseLinker.linkerFlags(ctx, flags) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 208 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 209 | if ctx.Host() && !binary.static() { | 
| Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 210 | if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") { | 
| Colin Cross | 7a108bc | 2017-01-30 22:44:19 -0800 | [diff] [blame] | 211 | flags.LdFlags = append(flags.LdFlags, "-pie") | 
| Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 212 | if ctx.Windows() { | 
| Colin Cross | 7a108bc | 2017-01-30 22:44:19 -0800 | [diff] [blame] | 213 | flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup") | 
|  | 214 | } | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because | 
|  | 219 | // all code is position independent, and then those warnings get promoted to | 
|  | 220 | // errors. | 
| Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 221 | if !ctx.Windows() { | 
| Vishwath Mohan | e87b768 | 2017-04-17 16:21:41 -0700 | [diff] [blame] | 222 | flags.CFlags = append(flags.CFlags, "-fPIE") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
| Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 225 | if ctx.toolchain().Bionic() { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 226 | if binary.static() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 227 | // Clang driver needs -static to create static executable. | 
|  | 228 | // However, bionic/linker uses -shared to overwrite. | 
|  | 229 | // Linker for x86 targets does not allow coexistance of -static and -shared, | 
|  | 230 | // so we add -static only if -shared is not used. | 
|  | 231 | if !inList("-shared", flags.LdFlags) { | 
|  | 232 | flags.LdFlags = append(flags.LdFlags, "-static") | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | flags.LdFlags = append(flags.LdFlags, | 
|  | 236 | "-nostdlib", | 
|  | 237 | "-Bstatic", | 
|  | 238 | "-Wl,--gc-sections", | 
|  | 239 | ) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 240 | } else { | 
|  | 241 | if flags.DynamicLinker == "" { | 
| Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 242 | if binary.Properties.DynamicLinker != "" { | 
|  | 243 | flags.DynamicLinker = binary.Properties.DynamicLinker | 
|  | 244 | } else { | 
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 245 | switch ctx.Os() { | 
|  | 246 | case android.Android: | 
|  | 247 | flags.DynamicLinker = "/system/bin/linker" | 
|  | 248 | case android.LinuxBionic: | 
| Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 249 | flags.DynamicLinker = "" | 
| Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 250 | default: | 
|  | 251 | ctx.ModuleErrorf("unknown dynamic linker") | 
|  | 252 | } | 
| Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 253 | if flags.Toolchain.Is64Bit() { | 
|  | 254 | flags.DynamicLinker += "64" | 
|  | 255 | } | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 256 | } | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | flags.LdFlags = append(flags.LdFlags, | 
|  | 260 | "-pie", | 
|  | 261 | "-nostdlib", | 
|  | 262 | "-Bdynamic", | 
|  | 263 | "-Wl,--gc-sections", | 
|  | 264 | "-Wl,-z,nocopyreloc", | 
|  | 265 | ) | 
| dimitry | feda20b | 2017-08-29 15:00:01 +0200 | [diff] [blame] | 266 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 267 | } | 
|  | 268 | } else { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 269 | if binary.static() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 270 | flags.LdFlags = append(flags.LdFlags, "-static") | 
|  | 271 | } | 
|  | 272 | if ctx.Darwin() { | 
|  | 273 | flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names") | 
|  | 274 | } | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | return flags | 
|  | 278 | } | 
|  | 279 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 280 | func (binary *binaryDecorator) link(ctx ModuleContext, | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 281 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 282 |  | 
| Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 283 | versionScript := ctx.ExpandOptionalSource(binary.Properties.Version_script, "version_script") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 284 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() | 
|  | 285 | outputFile := android.PathForModuleOut(ctx, fileName) | 
|  | 286 | ret := outputFile | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 287 |  | 
|  | 288 | var linkerDeps android.Paths | 
|  | 289 |  | 
|  | 290 | sharedLibs := deps.SharedLibs | 
|  | 291 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) | 
|  | 292 |  | 
| dimitry | feda20b | 2017-08-29 15:00:01 +0200 | [diff] [blame] | 293 | if versionScript.Valid() { | 
|  | 294 | if ctx.Darwin() { | 
|  | 295 | ctx.PropertyErrorf("version_script", "Not supported on Darwin") | 
|  | 296 | } else { | 
|  | 297 | flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String()) | 
|  | 298 | linkerDeps = append(linkerDeps, versionScript.Path()) | 
|  | 299 | } | 
|  | 300 | } | 
|  | 301 |  | 
| Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 302 | if deps.LinkerScript.Valid() { | 
|  | 303 | flags.LdFlags = append(flags.LdFlags, "-Wl,-T,"+deps.LinkerScript.String()) | 
|  | 304 | linkerDeps = append(linkerDeps, deps.LinkerScript.Path()) | 
|  | 305 | } | 
|  | 306 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 307 | if flags.DynamicLinker != "" { | 
|  | 308 | flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker) | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | builderFlags := flagsToBuilderFlags(flags) | 
|  | 312 |  | 
|  | 313 | if binary.stripper.needsStrip(ctx) { | 
|  | 314 | strippedOutputFile := outputFile | 
|  | 315 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) | 
|  | 316 | binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags) | 
|  | 317 | } | 
|  | 318 |  | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 319 | if String(binary.Properties.Prefix_symbols) != "" { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 320 | afterPrefixSymbols := outputFile | 
|  | 321 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 322 | TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 323 | flagsToBuilderFlags(flags), afterPrefixSymbols) | 
|  | 324 | } | 
|  | 325 |  | 
| Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 326 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) | 
|  | 327 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) | 
| Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 328 | linkerDeps = append(linkerDeps, objs.tidyFiles...) | 
| Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 329 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) | 
| Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 330 |  | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 331 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 332 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, | 
|  | 333 | builderFlags, outputFile) | 
|  | 334 |  | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 335 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) | 
|  | 336 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) | 
|  | 337 | binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx)) | 
|  | 338 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 339 | return ret | 
|  | 340 | } | 
|  | 341 |  | 
| Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 342 | func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) { | 
|  | 343 | binary.baseInstaller.install(ctx, file) | 
| Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 344 | for _, symlink := range binary.Properties.Symlinks { | 
|  | 345 | binary.symlinks = append(binary.symlinks, | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 346 | symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix()) | 
| Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 349 | if Bool(binary.Properties.Symlink_preferred_arch) { | 
|  | 350 | if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" { | 
| Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 351 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix") | 
|  | 352 | } | 
|  | 353 | if ctx.TargetPrimary() { | 
|  | 354 | binary.symlinks = append(binary.symlinks, ctx.baseModuleName()) | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | for _, symlink := range binary.symlinks { | 
|  | 359 | ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path) | 
|  | 360 | } | 
|  | 361 |  | 
| Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 362 | if ctx.Os().Class == android.Host { | 
|  | 363 | binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path) | 
|  | 364 | } | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { | 
|  | 368 | return binary.toolPath | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 369 | } |