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 ( |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 18 | "path/filepath" |
| 19 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 30 | Stem *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 31 | |
| 32 | // append to the name of the output |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 33 | Suffix *string `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 34 | |
| 35 | // if set, add an extra objcopy --prefix-symbols= step |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 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 |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 39 | Symlink_preferred_arch *bool |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 40 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 41 | // install symlinks to the binary. Symlink names will have the suffix and the binary |
| 42 | // extension (if any) appended |
| 43 | Symlinks []string `android:"arch_variant"` |
| 44 | |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 45 | DynamicLinker string `blueprint:"mutated"` |
Yifan Hong | 946e32e | 2018-04-03 13:22:50 -0700 | [diff] [blame] | 46 | |
| 47 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 48 | // (in Make or Soong). |
| 49 | // This does not completely prevent installation of the overridden binaries, but if both |
| 50 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 51 | // from PRODUCT_PACKAGES. |
| 52 | Overrides []string |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func init() { |
Jiyong Park | 16e91a0 | 2018-12-20 18:18:08 +0900 | [diff] [blame] | 56 | android.RegisterModuleType("cc_binary", BinaryFactory) |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 57 | android.RegisterModuleType("cc_binary_host", binaryHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 60 | // cc_binary produces a binary that is runnable on a device. |
Jiyong Park | 16e91a0 | 2018-12-20 18:18:08 +0900 | [diff] [blame] | 61 | func BinaryFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 62 | module, _ := NewBinary(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 63 | return module.Init() |
| 64 | } |
| 65 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 66 | // cc_binary_host produces a binary that is runnable on a host. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 67 | func binaryHostFactory() android.Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 68 | module, _ := NewBinary(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 69 | return module.Init() |
| 70 | } |
| 71 | |
| 72 | // |
| 73 | // Executables |
| 74 | // |
| 75 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 76 | type binaryDecorator struct { |
| 77 | *baseLinker |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 78 | *baseInstaller |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 79 | stripper |
| 80 | |
| 81 | Properties BinaryLinkerProperties |
| 82 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 83 | toolPath android.OptionalPath |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 84 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 85 | // Location of the linked, unstripped binary |
| 86 | unstrippedOutputFile android.Path |
| 87 | |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 88 | // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS |
| 89 | symlinks []string |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 90 | |
| 91 | // Output archive of gcno coverage information |
| 92 | coverageOutputFile android.OptionalPath |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 93 | |
| 94 | // Location of the file that should be copied to dist dir when requested |
| 95 | distFile android.OptionalPath |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 96 | |
| 97 | post_install_cmds []string |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 100 | var _ linker = (*binaryDecorator)(nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 101 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 102 | func (binary *binaryDecorator) linkerProps() []interface{} { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 103 | return append(binary.baseLinker.linkerProps(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 104 | &binary.Properties, |
| 105 | &binary.stripper.StripProperties) |
| 106 | |
| 107 | } |
| 108 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 109 | func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 110 | stem := ctx.baseModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 111 | if String(binary.Properties.Stem) != "" { |
| 112 | stem = String(binary.Properties.Stem) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 115 | return stem + String(binary.Properties.Suffix) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 118 | func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 119 | deps = binary.baseLinker.linkerDeps(ctx, deps) |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 120 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 121 | if !Bool(binary.baseLinker.Properties.Nocrt) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 122 | if !ctx.useSdk() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 123 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 124 | deps.CrtBegin = "crtbegin_static" |
| 125 | } else { |
| 126 | deps.CrtBegin = "crtbegin_dynamic" |
| 127 | } |
| 128 | deps.CrtEnd = "crtend_android" |
| 129 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 130 | // TODO(danalbert): Add generation of crt objects. |
| 131 | // For `sdk_version: "current"`, we don't actually have a |
| 132 | // freshly generated set of CRT objects. Use the last stable |
| 133 | // version. |
| 134 | version := ctx.sdkVersion() |
| 135 | if version == "current" { |
Jayant Chowdhary | 6e8115a | 2017-05-09 10:21:52 -0700 | [diff] [blame] | 136 | version = getCurrentNdkPrebuiltVersion(ctx) |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 139 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 140 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 142 | if binary.static() { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 143 | deps.CrtBegin = "ndk_crtbegin_static." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | } else { |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 145 | deps.CrtBegin = "ndk_crtbegin_dynamic." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 146 | } |
Dan Albert | ebedf67 | 2016-11-08 15:06:22 -0800 | [diff] [blame] | 147 | deps.CrtEnd = "ndk_crtend_android." + version |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 152 | if binary.static() { |
Dan Albert | dc2597d | 2017-01-26 17:44:26 -0800 | [diff] [blame] | 153 | if ctx.selectedStl() == "libc++_static" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 154 | deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") |
| 155 | } |
| 156 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with |
| 157 | // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, |
| 158 | // move them to the beginning of deps.LateStaticLibs |
| 159 | var groupLibs []string |
| 160 | deps.StaticLibs, groupLibs = filterList(deps.StaticLibs, |
| 161 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) |
| 162 | deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...) |
| 163 | } |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 164 | |
| 165 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 166 | deps.DynamicLinker = "linker" |
| 167 | deps.LinkerFlagsFile = "host_bionic_linker_flags" |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 168 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 171 | if !binary.static() && inList("libc", deps.StaticLibs) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 172 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 173 | "from static libs or set static_executable: true") |
| 174 | } |
Colin Cross | 2383f3b | 2018-02-06 14:40:13 -0800 | [diff] [blame] | 175 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 176 | return deps |
| 177 | } |
| 178 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 179 | func (binary *binaryDecorator) isDependencyRoot() bool { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 180 | return true |
| 181 | } |
| 182 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 183 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 184 | module := newModule(hod, android.MultilibFirst) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 185 | binary := &binaryDecorator{ |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 186 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 187 | baseInstaller: NewBaseInstaller("bin", "", InstallInSystem), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 188 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 189 | module.compiler = NewBaseCompiler() |
| 190 | module.linker = binary |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 191 | module.installer = binary |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 192 | return module, binary |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 195 | func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 196 | binary.baseLinker.linkerInit(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 197 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 198 | if !ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 199 | if ctx.Os() == android.Linux { |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 200 | if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 201 | binary.Properties.Static_executable = BoolPtr(true) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 202 | } |
Doug Horn | c32c6b0 | 2019-01-17 14:44:05 -0800 | [diff] [blame] | 203 | } else if !ctx.Fuchsia() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 204 | // Static executables are not supported on Darwin or Windows |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 205 | binary.Properties.Static_executable = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 210 | func (binary *binaryDecorator) static() bool { |
| 211 | return Bool(binary.Properties.Static_executable) |
| 212 | } |
| 213 | |
| 214 | func (binary *binaryDecorator) staticBinary() bool { |
| 215 | return binary.static() |
| 216 | } |
| 217 | |
| 218 | func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 219 | flags = binary.baseLinker.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 220 | |
Colin Cross | 446c666 | 2018-09-14 16:00:16 -0700 | [diff] [blame] | 221 | if ctx.Host() && !ctx.Windows() && !binary.static() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 222 | if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") { |
Colin Cross | 7a108bc | 2017-01-30 22:44:19 -0800 | [diff] [blame] | 223 | flags.LdFlags = append(flags.LdFlags, "-pie") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because |
| 228 | // all code is position independent, and then those warnings get promoted to |
| 229 | // errors. |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 230 | if !ctx.Windows() { |
Vishwath Mohan | e87b768 | 2017-04-17 16:21:41 -0700 | [diff] [blame] | 231 | flags.CFlags = append(flags.CFlags, "-fPIE") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 234 | if ctx.toolchain().Bionic() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 235 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 236 | // Clang driver needs -static to create static executable. |
| 237 | // However, bionic/linker uses -shared to overwrite. |
| 238 | // Linker for x86 targets does not allow coexistance of -static and -shared, |
| 239 | // so we add -static only if -shared is not used. |
| 240 | if !inList("-shared", flags.LdFlags) { |
| 241 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 242 | } |
| 243 | |
| 244 | flags.LdFlags = append(flags.LdFlags, |
| 245 | "-nostdlib", |
| 246 | "-Bstatic", |
| 247 | "-Wl,--gc-sections", |
| 248 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 249 | } else { |
| 250 | if flags.DynamicLinker == "" { |
Colin Cross | 522e373 | 2016-09-07 13:14:06 -0700 | [diff] [blame] | 251 | if binary.Properties.DynamicLinker != "" { |
| 252 | flags.DynamicLinker = binary.Properties.DynamicLinker |
| 253 | } else { |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 254 | switch ctx.Os() { |
| 255 | case android.Android: |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 256 | if ctx.bootstrap() && !ctx.inRecovery() { |
Jiyong Park | a4b9dd0 | 2019-01-16 22:53:13 +0900 | [diff] [blame] | 257 | flags.DynamicLinker = "/system/bin/bootstrap/linker" |
| 258 | } else { |
| 259 | flags.DynamicLinker = "/system/bin/linker" |
| 260 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 261 | if flags.Toolchain.Is64Bit() { |
| 262 | flags.DynamicLinker += "64" |
| 263 | } |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 264 | case android.LinuxBionic: |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 265 | flags.DynamicLinker = "" |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 266 | default: |
| 267 | ctx.ModuleErrorf("unknown dynamic linker") |
| 268 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if ctx.Os() == android.LinuxBionic { |
| 272 | // Use the dlwrap entry point, but keep _start around so |
| 273 | // that it can be used by host_bionic_inject |
| 274 | flags.LdFlags = append(flags.LdFlags, |
| 275 | "-Wl,--entry=__dlwrap__start", |
| 276 | "-Wl,--undefined=_start", |
| 277 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | flags.LdFlags = append(flags.LdFlags, |
| 282 | "-pie", |
| 283 | "-nostdlib", |
| 284 | "-Bdynamic", |
| 285 | "-Wl,--gc-sections", |
| 286 | "-Wl,-z,nocopyreloc", |
| 287 | ) |
| 288 | } |
| 289 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 290 | if binary.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 291 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 292 | } |
| 293 | if ctx.Darwin() { |
| 294 | flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names") |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return flags |
| 299 | } |
| 300 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 301 | func (binary *binaryDecorator) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 302 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 303 | |
| 304 | fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 305 | outputFile := android.PathForModuleOut(ctx, fileName) |
| 306 | ret := outputFile |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 307 | |
| 308 | var linkerDeps android.Paths |
| 309 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 310 | if deps.LinkerFlagsFile.Valid() { |
| 311 | flags.LdFlags = append(flags.LdFlags, "$$(cat "+deps.LinkerFlagsFile.String()+")") |
| 312 | linkerDeps = append(linkerDeps, deps.LinkerFlagsFile.Path()) |
Dan Willemsen | c77a0b3 | 2017-09-18 23:19:12 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 315 | if flags.DynamicLinker != "" { |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 316 | flags.LdFlags = append(flags.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker) |
| 317 | } else if ctx.toolchain().Bionic() && !binary.static() { |
| 318 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-dynamic-linker") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | builderFlags := flagsToBuilderFlags(flags) |
| 322 | |
| 323 | if binary.stripper.needsStrip(ctx) { |
Yi Kong | b5c34d7 | 2018-11-07 16:28:49 -0800 | [diff] [blame] | 324 | if ctx.Darwin() { |
| 325 | builderFlags.stripUseGnuStrip = true |
| 326 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 327 | strippedOutputFile := outputFile |
| 328 | outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame^] | 329 | binary.stripper.stripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, builderFlags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 332 | binary.unstrippedOutputFile = outputFile |
| 333 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 334 | if String(binary.Properties.Prefix_symbols) != "" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 335 | afterPrefixSymbols := outputFile |
| 336 | outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName) |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 337 | TransformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 338 | flagsToBuilderFlags(flags), afterPrefixSymbols) |
| 339 | } |
| 340 | |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 341 | if Bool(binary.baseLinker.Properties.Use_version_lib) { |
| 342 | if ctx.Host() { |
| 343 | versionedOutputFile := outputFile |
| 344 | outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) |
| 345 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 346 | } else { |
| 347 | versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) |
| 348 | binary.distFile = android.OptionalPathForPath(versionedOutputFile) |
| 349 | |
| 350 | if binary.stripper.needsStrip(ctx) { |
| 351 | out := android.PathForModuleOut(ctx, "versioned-stripped", fileName) |
| 352 | binary.distFile = android.OptionalPathForPath(out) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame^] | 353 | binary.stripper.stripExecutableOrSharedLib(ctx, versionedOutputFile, out, builderFlags) |
Dan Willemsen | 569edc5 | 2018-11-19 09:33:29 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile) |
| 357 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 360 | if ctx.Os() == android.LinuxBionic && !binary.static() { |
| 361 | injectedOutputFile := outputFile |
| 362 | outputFile = android.PathForModuleOut(ctx, "prelinker", fileName) |
| 363 | |
| 364 | if !deps.DynamicLinker.Valid() { |
| 365 | panic("Non-static host bionic modules must have a dynamic linker") |
| 366 | } |
| 367 | |
| 368 | binary.injectHostBionicLinkerSymbols(ctx, outputFile, deps.DynamicLinker.Path(), injectedOutputFile) |
| 369 | } |
| 370 | |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 371 | var sharedLibs android.Paths |
| 372 | // Ignore shared libs for static executables. |
| 373 | if !binary.static() { |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 374 | sharedLibs = deps.EarlySharedLibs |
| 375 | sharedLibs = append(sharedLibs, deps.SharedLibs...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 376 | sharedLibs = append(sharedLibs, deps.LateSharedLibs...) |
Jiyong Park | 64a44f2 | 2019-01-18 14:37:08 +0900 | [diff] [blame] | 377 | linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...) |
Jaewoong Jung | 232c07c | 2018-12-18 11:08:25 -0800 | [diff] [blame] | 378 | linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) |
| 379 | linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) |
| 380 | } |
| 381 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 382 | linkerDeps = append(linkerDeps, objs.tidyFiles...) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 383 | linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) |
Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 384 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 385 | TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 386 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
Pirama Arumuga Nainar | 6d8c0a5 | 2019-04-15 23:03:38 -0700 | [diff] [blame] | 387 | builderFlags, outputFile, nil) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 388 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 389 | objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) |
| 390 | objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) |
| 391 | binary.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, binary.getStem(ctx)) |
| 392 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 393 | // Need to determine symlinks early since some targets (ie APEX) need this |
| 394 | // information but will not call 'install' |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 395 | for _, symlink := range binary.Properties.Symlinks { |
| 396 | binary.symlinks = append(binary.symlinks, |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 397 | symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix()) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 400 | if Bool(binary.Properties.Symlink_preferred_arch) { |
| 401 | if String(binary.Properties.Stem) == "" && String(binary.Properties.Suffix) == "" { |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 402 | ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix") |
| 403 | } |
| 404 | if ctx.TargetPrimary() { |
| 405 | binary.symlinks = append(binary.symlinks, ctx.baseModuleName()) |
| 406 | } |
| 407 | } |
| 408 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 409 | return ret |
| 410 | } |
| 411 | |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 412 | func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path { |
| 413 | return binary.unstrippedOutputFile |
| 414 | } |
| 415 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 416 | func (binary *binaryDecorator) symlinkList() []string { |
| 417 | return binary.symlinks |
| 418 | } |
| 419 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 420 | func (binary *binaryDecorator) nativeCoverage() bool { |
| 421 | return true |
| 422 | } |
| 423 | |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 424 | // /system/bin/linker -> /apex/com.android.runtime/bin/linker |
| 425 | func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) { |
| 426 | dir := binary.baseInstaller.installDir(ctx) |
| 427 | dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir) |
| 428 | target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base()) |
| 429 | |
| 430 | ctx.InstallAbsoluteSymlink(dir, file.Base(), target) |
| 431 | binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, file.Base(), target)) |
| 432 | |
| 433 | for _, symlink := range binary.symlinks { |
| 434 | ctx.InstallAbsoluteSymlink(dir, symlink, target) |
| 435 | binary.post_install_cmds = append(binary.post_install_cmds, makeSymlinkCmd(dirOnDevice, symlink, target)) |
| 436 | } |
| 437 | } |
| 438 | |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 439 | func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) { |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 440 | // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory. |
| 441 | // The original path becomes a symlink to the corresponding file in the |
| 442 | // runtime APEX. |
Peter Collingbourne | 3478bb2 | 2019-04-24 14:41:12 -0700 | [diff] [blame] | 443 | if installToBootstrap(ctx.baseModuleName(), ctx.Config()) && ctx.Arch().Native && ctx.apexName() == "" && !ctx.inRecovery() { |
| 444 | if ctx.Device() && isBionic(ctx.baseModuleName()) { |
Jiyong Park | c3e2c86 | 2019-03-16 01:10:08 +0900 | [diff] [blame] | 445 | binary.installSymlinkToRuntimeApex(ctx, file) |
| 446 | } |
Jiyong Park | f119435 | 2019-02-25 11:05:47 +0900 | [diff] [blame] | 447 | binary.baseInstaller.subDir = "bootstrap" |
| 448 | } |
Alex Light | 3d67359 | 2019-01-18 14:37:31 -0800 | [diff] [blame] | 449 | binary.baseInstaller.install(ctx, file) |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame] | 450 | for _, symlink := range binary.symlinks { |
| 451 | ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink, binary.baseInstaller.path) |
| 452 | } |
| 453 | |
Dan Willemsen | 4aa75ca | 2016-09-28 16:18:03 -0700 | [diff] [blame] | 454 | if ctx.Os().Class == android.Host { |
| 455 | binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func (binary *binaryDecorator) hostToolPath() android.OptionalPath { |
| 460 | return binary.toolPath |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 461 | } |
Dan Willemsen | a0790e3 | 2018-10-12 00:24:23 -0700 | [diff] [blame] | 462 | |
| 463 | func init() { |
| 464 | pctx.HostBinToolVariable("hostBionicSymbolsInjectCmd", "host_bionic_inject") |
| 465 | } |
| 466 | |
| 467 | var injectHostBionicSymbols = pctx.AndroidStaticRule("injectHostBionicSymbols", |
| 468 | blueprint.RuleParams{ |
| 469 | Command: "$hostBionicSymbolsInjectCmd -i $in -l $linker -o $out", |
| 470 | CommandDeps: []string{"$hostBionicSymbolsInjectCmd"}, |
| 471 | }, "linker") |
| 472 | |
| 473 | func (binary *binaryDecorator) injectHostBionicLinkerSymbols(ctx ModuleContext, in, linker android.Path, out android.WritablePath) { |
| 474 | ctx.Build(pctx, android.BuildParams{ |
| 475 | Rule: injectHostBionicSymbols, |
| 476 | Description: "inject host bionic symbols", |
| 477 | Input: in, |
| 478 | Implicit: linker, |
| 479 | Output: out, |
| 480 | Args: map[string]string{ |
| 481 | "linker": linker.String(), |
| 482 | }, |
| 483 | }) |
| 484 | } |