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 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "fmt" |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 20 | |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 25 | // This file contains the basic functionality for linking against static libraries and shared |
| 26 | // libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc. |
| 27 | |
| 28 | type BaseLinkerProperties struct { |
| 29 | // list of modules whose object files should be linked into this module |
| 30 | // in their entirety. For static library modules, all of the .o files from the intermediate |
| 31 | // directory of the dependency will be linked into this modules .a file. For a shared library, |
| 32 | // the dependency's .a file will be linked into this module using -Wl,--whole-archive. |
| 33 | Whole_static_libs []string `android:"arch_variant,variant_prepend"` |
| 34 | |
| 35 | // list of modules that should be statically linked into this module. |
| 36 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 37 | |
| 38 | // list of modules that should be dynamically linked into this module. |
| 39 | Shared_libs []string `android:"arch_variant"` |
| 40 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 41 | // list of modules that should only provide headers for this module. |
| 42 | Header_libs []string `android:"arch_variant,variant_prepend"` |
| 43 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 44 | // list of module-specific flags that will be used for all link steps |
| 45 | Ldflags []string `android:"arch_variant"` |
| 46 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 47 | // list of system libraries that will be dynamically linked to |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 48 | // shared library and executable modules. If unset, generally defaults to libc, |
| 49 | // libm, and libdl. Set to [] to prevent linking against the defaults. |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 50 | System_shared_libs []string |
| 51 | |
| 52 | // allow the module to contain undefined symbols. By default, |
| 53 | // modules cannot contain undefined symbols that are not satisified by their immediate |
| 54 | // dependencies. Set this flag to true to remove --no-undefined from the linker flags. |
| 55 | // This flag should only be necessary for compiling low-level libraries like libc. |
Colin Cross | be360ae | 2016-12-08 09:45:21 -0800 | [diff] [blame] | 56 | Allow_undefined_symbols *bool `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 57 | |
| 58 | // don't link in libgcc.a |
| 59 | No_libgcc *bool |
| 60 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 61 | // Use clang lld instead of gnu ld. |
Pirama Arumuga Nainar | 2b8959a | 2018-04-20 11:52:42 -0700 | [diff] [blame] | 62 | Use_clang_lld *bool `android:"arch_variant"` |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 63 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 64 | // -l arguments to pass to linker for host-provided shared libraries |
| 65 | Host_ldlibs []string `android:"arch_variant"` |
| 66 | |
| 67 | // list of shared libraries to re-export include directories from. Entries must be |
| 68 | // present in shared_libs. |
| 69 | Export_shared_lib_headers []string `android:"arch_variant"` |
| 70 | |
| 71 | // list of static libraries to re-export include directories from. Entries must be |
| 72 | // present in static_libs. |
| 73 | Export_static_lib_headers []string `android:"arch_variant"` |
| 74 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 75 | // list of header libraries to re-export include directories from. Entries must be |
| 76 | // present in header_libs. |
| 77 | Export_header_lib_headers []string `android:"arch_variant"` |
| 78 | |
Dan Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame] | 79 | // list of generated headers to re-export include directories from. Entries must be |
| 80 | // present in generated_headers. |
| 81 | Export_generated_headers []string `android:"arch_variant"` |
| 82 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 83 | // don't link in crt_begin and crt_end. This flag should only be necessary for |
| 84 | // compiling crt or libc. |
| 85 | Nocrt *bool `android:"arch_variant"` |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 86 | |
| 87 | // group static libraries. This can resolve missing symbols issues with interdependencies |
| 88 | // between static libraries, but it is generally better to order them correctly instead. |
| 89 | Group_static_libs *bool `android:"arch_variant"` |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 90 | |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 91 | // list of modules that should be installed with this module. This is similar to 'required' |
| 92 | // but '.vendor' suffix will be appended to the module names if the shared libraries have |
| 93 | // vendor variants and this module uses VNDK. |
| 94 | Runtime_libs []string `android:"arch_variant"` |
| 95 | |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 96 | Target struct { |
| 97 | Vendor struct { |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 98 | // list of shared libs that should not be used to build the vendor variant |
| 99 | // of the C/C++ module. |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 100 | Exclude_shared_libs []string |
Jiyong Park | 52d25bd | 2017-10-13 09:17:01 +0900 | [diff] [blame] | 101 | |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 102 | // list of static libs that should not be used to build the vendor variant |
| 103 | // of the C/C++ module. |
Jiyong Park | 52d25bd | 2017-10-13 09:17:01 +0900 | [diff] [blame] | 104 | Exclude_static_libs []string |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 105 | |
Jiwen 'Steve' Cai | 8dbc653 | 2018-08-08 10:58:34 -0700 | [diff] [blame] | 106 | // list of header libs that should not be used to build the vendor variant |
| 107 | // of the C/C++ module. |
| 108 | Exclude_header_libs []string |
| 109 | |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 110 | // list of runtime libs that should not be installed along with the vendor |
| 111 | // variant of the C/C++ module. |
| 112 | Exclude_runtime_libs []string |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 113 | } |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 114 | Recovery struct { |
| 115 | // list of shared libs that should not be used to build |
| 116 | // the recovery variant of the C/C++ module. |
| 117 | Exclude_shared_libs []string |
| 118 | |
| 119 | // list of static libs that should not be used to build |
| 120 | // the recovery variant of the C/C++ module. |
| 121 | Exclude_static_libs []string |
Hridya Valsaraju | 280febf | 2018-08-10 09:08:13 -0700 | [diff] [blame] | 122 | |
| 123 | // list of header libs that should not be used to build the recovery variant |
| 124 | // of the C/C++ module. |
| 125 | Exclude_header_libs []string |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 126 | } |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 127 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 128 | |
| 129 | // make android::build:GetBuildNumber() available containing the build ID. |
| 130 | Use_version_lib *bool `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Chih-Hung Hsieh | 8681471 | 2018-05-23 18:30:46 -0700 | [diff] [blame] | 133 | // TODO(http://b/80437643): BaseLinkerProperties is getting too big, |
| 134 | // more than 2^16 bytes. New properties are defined in MoreBaseLinkerProperties. |
| 135 | type MoreBaseLinkerProperties struct { |
| 136 | // Generate compact dynamic relocation table, default true. |
| 137 | Pack_relocations *bool `android:"arch_variant"` |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 138 | |
| 139 | // local file name to pass to the linker as --version_script |
| 140 | Version_script *string `android:"arch_variant"` |
| 141 | |
| 142 | Target struct { |
| 143 | Vendor struct { |
| 144 | // version script for this vendor variant |
| 145 | Version_script *string `android:"arch_variant"` |
| 146 | } |
| 147 | } |
Chih-Hung Hsieh | 8681471 | 2018-05-23 18:30:46 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 150 | func NewBaseLinker(sanitize *sanitize) *baseLinker { |
| 151 | return &baseLinker{sanitize: sanitize} |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 154 | // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties |
| 155 | type baseLinker struct { |
| 156 | Properties BaseLinkerProperties |
Chih-Hung Hsieh | 8681471 | 2018-05-23 18:30:46 -0700 | [diff] [blame] | 157 | MoreProperties MoreBaseLinkerProperties |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | dynamicProperties struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 159 | RunPaths []string `blueprint:"mutated"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 160 | } |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 161 | |
| 162 | sanitize *sanitize |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func (linker *baseLinker) appendLdflags(flags []string) { |
| 166 | linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...) |
| 167 | } |
| 168 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 169 | func (linker *baseLinker) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 170 | if ctx.toolchain().Is64Bit() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 171 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 172 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 173 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 177 | func (linker *baseLinker) linkerProps() []interface{} { |
Chih-Hung Hsieh | 8681471 | 2018-05-23 18:30:46 -0700 | [diff] [blame] | 178 | return []interface{}{&linker.Properties, &linker.MoreProperties, &linker.dynamicProperties} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 181 | func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...) |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 183 | deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 184 | deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...) |
| 185 | deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...) |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 186 | deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 187 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 188 | deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 189 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...) |
| 190 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...) |
Dan Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame] | 191 | deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 192 | |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 193 | if Bool(linker.Properties.Use_version_lib) { |
| 194 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion") |
| 195 | } |
| 196 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 197 | if ctx.useVndk() { |
Jiyong Park | 7447228 | 2017-08-10 00:48:06 +0900 | [diff] [blame] | 198 | deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs) |
| 199 | deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs) |
Jiyong Park | 52d25bd | 2017-10-13 09:17:01 +0900 | [diff] [blame] | 200 | deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs) |
Jiwen 'Steve' Cai | 8dbc653 | 2018-08-08 10:58:34 -0700 | [diff] [blame] | 201 | deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs) |
Jiyong Park | 52d25bd | 2017-10-13 09:17:01 +0900 | [diff] [blame] | 202 | deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs) |
| 203 | deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs) |
Logan Chien | 43d34c3 | 2017-12-20 01:17:32 +0800 | [diff] [blame] | 204 | deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs) |
Jiyong Park | 7447228 | 2017-08-10 00:48:06 +0900 | [diff] [blame] | 205 | } |
| 206 | |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 207 | if ctx.inRecovery() { |
| 208 | deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs) |
| 209 | deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs) |
| 210 | deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs) |
Hridya Valsaraju | 280febf | 2018-08-10 09:08:13 -0700 | [diff] [blame] | 211 | deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs) |
| 212 | deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs) |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 213 | deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs) |
| 214 | deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs) |
| 215 | } |
| 216 | |
Dan Albert | 83705c8 | 2016-09-14 16:47:18 -0700 | [diff] [blame] | 217 | if ctx.ModuleName() != "libcompiler_rt-extras" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 218 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras") |
| 219 | } |
| 220 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 221 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 222 | // libgcc and libatomic have to be last on the command line |
| 223 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic") |
| 224 | if !Bool(linker.Properties.No_libgcc) { |
| 225 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc") |
| 226 | } |
| 227 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 228 | if !ctx.static() { |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 229 | systemSharedLibs := linker.Properties.System_shared_libs |
| 230 | if systemSharedLibs == nil { |
| 231 | systemSharedLibs = []string{"libc", "libm", "libdl"} |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 234 | if inList("libdl", deps.SharedLibs) { |
| 235 | // If system_shared_libs has libc but not libdl, make sure shared_libs does not |
| 236 | // have libdl to avoid loading libdl before libc. |
| 237 | if inList("libc", systemSharedLibs) { |
| 238 | if !inList("libdl", systemSharedLibs) { |
| 239 | ctx.PropertyErrorf("shared_libs", |
| 240 | "libdl must be in system_shared_libs, not shared_libs") |
| 241 | } |
| 242 | _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs) |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 243 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 244 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 245 | |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 246 | // If libc and libdl are both in system_shared_libs make sure libd comes after libc |
| 247 | // to avoid loading libdl before libc. |
| 248 | if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) && |
| 249 | indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) { |
| 250 | ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc") |
| 251 | } |
| 252 | |
| 253 | deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...) |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 254 | } else if ctx.useSdk() || ctx.useVndk() { |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 255 | deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl") |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 256 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 259 | if ctx.Windows() { |
Josh Gao | 7bd4c5c | 2017-02-23 17:52:24 -0800 | [diff] [blame] | 260 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread") |
| 261 | } |
| 262 | |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 263 | android.ExtractSourceDeps(ctx, linker.MoreProperties.Version_script) |
| 264 | android.ExtractSourceDeps(ctx, |
| 265 | linker.MoreProperties.Target.Vendor.Version_script) |
| 266 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 267 | return deps |
| 268 | } |
| 269 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 270 | func (linker *baseLinker) useClangLld(ctx ModuleContext) bool { |
Chih-Hung Hsieh | e5ac609 | 2018-04-24 16:00:01 -0700 | [diff] [blame] | 271 | // Clang lld is not ready for for Darwin host executables yet. |
| 272 | // See https://lld.llvm.org/AtomLLD.html for status of lld for Mach-O. |
| 273 | if ctx.Darwin() { |
| 274 | return false |
| 275 | } |
Pirama Arumuga Nainar | 8a852e7 | 2018-06-19 20:07:54 -0700 | [diff] [blame] | 276 | // http://b/110800681 - lld cannot link Android's Windows modules yet. |
| 277 | if ctx.Windows() { |
| 278 | return false |
| 279 | } |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 280 | if linker.Properties.Use_clang_lld != nil { |
| 281 | return Bool(linker.Properties.Use_clang_lld) |
| 282 | } |
| 283 | return ctx.Config().UseClangLld() |
| 284 | } |
| 285 | |
| 286 | // ModuleContext extends BaseModuleContext |
| 287 | // BaseModuleContext should know if LLD is used? |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 288 | func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 289 | toolchain := ctx.toolchain() |
| 290 | |
Colin Cross | 324a457 | 2017-11-02 23:09:41 -0700 | [diff] [blame] | 291 | hod := "Host" |
| 292 | if ctx.Os().Class == android.Device { |
| 293 | hod = "Device" |
| 294 | } |
| 295 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 296 | if flags.Clang && linker.useClangLld(ctx) { |
| 297 | flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod)) |
Chih-Hung Hsieh | 8681471 | 2018-05-23 18:30:46 -0700 | [diff] [blame] | 298 | if !BoolDefault(linker.MoreProperties.Pack_relocations, true) { |
| 299 | flags.LdFlags = append(flags.LdFlags, "-Wl,--pack-dyn-relocs=none") |
| 300 | } |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 301 | } else { |
| 302 | flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod)) |
| 303 | } |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 304 | if Bool(linker.Properties.Allow_undefined_symbols) { |
| 305 | if ctx.Darwin() { |
| 306 | // darwin defaults to treating undefined symbols as errors |
| 307 | flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 308 | } |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 309 | } else if !ctx.Darwin() { |
| 310 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined") |
| 311 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 312 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 313 | if flags.Clang && linker.useClangLld(ctx) { |
| 314 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLldflags()) |
| 315 | } else if flags.Clang { |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 316 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags()) |
| 317 | } else { |
| 318 | flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags()) |
| 319 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 320 | |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 321 | if !ctx.toolchain().Bionic() { |
| 322 | CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 323 | |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 324 | flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...) |
Colin Cross | c5fdbb8 | 2017-09-08 12:45:18 -0700 | [diff] [blame] | 325 | |
Colin Cross | 87dd963 | 2017-11-03 13:31:05 -0700 | [diff] [blame] | 326 | if !ctx.Windows() { |
| 327 | // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device |
| 328 | // builds |
| 329 | flags.LdFlags = append(flags.LdFlags, |
| 330 | "-ldl", |
| 331 | "-lpthread", |
| 332 | "-lm", |
| 333 | ) |
| 334 | if !ctx.Darwin() { |
| 335 | flags.LdFlags = append(flags.LdFlags, "-lrt") |
Colin Cross | c5fdbb8 | 2017-09-08 12:45:18 -0700 | [diff] [blame] | 336 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags) |
| 341 | |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 342 | flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 343 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 344 | if ctx.Host() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 345 | rpath_prefix := `\$$ORIGIN/` |
| 346 | if ctx.Darwin() { |
| 347 | rpath_prefix = "@loader_path/" |
| 348 | } |
| 349 | |
Dan Willemsen | 6f91fbf | 2017-09-18 22:45:15 -0700 | [diff] [blame] | 350 | if !ctx.static() { |
| 351 | for _, rpath := range linker.dynamicProperties.RunPaths { |
| 352 | flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath) |
| 353 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 357 | if ctx.useSdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) { |
Colin Cross | 6774e28 | 2017-08-11 13:23:57 -0700 | [diff] [blame] | 358 | // The bionic linker now has support gnu style hashes (which are much faster!), but shipping |
| 359 | // to older devices requires the old style hash. Fortunately, we can build with both and |
| 360 | // it'll work anywhere. |
| 361 | // This is not currently supported on MIPS architectures. |
| 362 | flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both") |
| 363 | } |
| 364 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 365 | if flags.Clang { |
| 366 | flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags()) |
| 367 | } else { |
| 368 | flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags()) |
| 369 | } |
| 370 | |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 371 | if Bool(linker.Properties.Group_static_libs) { |
| 372 | flags.GroupStaticLibs = true |
| 373 | } |
| 374 | |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 375 | versionScript := ctx.ExpandOptionalSource( |
| 376 | linker.MoreProperties.Version_script, "version_script") |
| 377 | |
| 378 | if ctx.useVndk() && linker.MoreProperties.Target.Vendor.Version_script != nil { |
| 379 | versionScript = ctx.ExpandOptionalSource( |
| 380 | linker.MoreProperties.Target.Vendor.Version_script, |
| 381 | "target.vendor.version_script") |
| 382 | } |
| 383 | |
| 384 | if versionScript.Valid() { |
| 385 | if ctx.Darwin() { |
| 386 | ctx.PropertyErrorf("version_script", "Not supported on Darwin") |
| 387 | } else { |
| 388 | flags.LdFlags = append(flags.LdFlags, |
| 389 | "-Wl,--version-script,"+versionScript.String()) |
| 390 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path()) |
| 391 | |
| 392 | if linker.sanitize.isSanitizerEnabled(cfi) { |
| 393 | cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath) |
| 394 | flags.LdFlags = append(flags.LdFlags, |
| 395 | "-Wl,--version-script,"+cfiExportsMap.String()) |
| 396 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap) |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 401 | return flags |
| 402 | } |
| 403 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 404 | func (linker *baseLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 405 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 406 | panic(fmt.Errorf("baseLinker doesn't know how to link")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 407 | } |
Colin Cross | 86803cf | 2018-02-15 14:12:26 -0800 | [diff] [blame] | 408 | |
| 409 | // Injecting version symbols |
| 410 | // Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step |
| 411 | // after linking that injects a constant placeholder with the current version number. |
| 412 | |
| 413 | func init() { |
| 414 | pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject") |
| 415 | } |
| 416 | |
| 417 | var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol", |
| 418 | blueprint.RuleParams{ |
| 419 | Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " + |
| 420 | "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $buildNumberFromFile", |
| 421 | CommandDeps: []string{"$symbolInjectCmd"}, |
| 422 | }, |
| 423 | "buildNumberFromFile") |
| 424 | |
| 425 | func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) { |
| 426 | ctx.Build(pctx, android.BuildParams{ |
| 427 | Rule: injectVersionSymbol, |
| 428 | Description: "inject version symbol", |
| 429 | Input: in, |
| 430 | Output: out, |
| 431 | Args: map[string]string{ |
| 432 | "buildNumberFromFile": ctx.Config().BuildNumberFromFile(), |
| 433 | }, |
| 434 | }) |
| 435 | } |