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 | |
| 21 | "github.com/google/blueprint/proptools" |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | // This file contains the basic functionality for linking against static libraries and shared |
| 25 | // libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc. |
| 26 | |
| 27 | type BaseLinkerProperties struct { |
| 28 | // list of modules whose object files should be linked into this module |
| 29 | // in their entirety. For static library modules, all of the .o files from the intermediate |
| 30 | // directory of the dependency will be linked into this modules .a file. For a shared library, |
| 31 | // the dependency's .a file will be linked into this module using -Wl,--whole-archive. |
| 32 | Whole_static_libs []string `android:"arch_variant,variant_prepend"` |
| 33 | |
| 34 | // list of modules that should be statically linked into this module. |
| 35 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 36 | |
| 37 | // list of modules that should be dynamically linked into this module. |
| 38 | Shared_libs []string `android:"arch_variant"` |
| 39 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 40 | // list of modules that should only provide headers for this module. |
| 41 | Header_libs []string `android:"arch_variant,variant_prepend"` |
| 42 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 43 | // list of module-specific flags that will be used for all link steps |
| 44 | Ldflags []string `android:"arch_variant"` |
| 45 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 46 | // list of system libraries that will be dynamically linked to |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 47 | // shared library and executable modules. If unset, generally defaults to libc, |
| 48 | // libm, and libdl. Set to [] to prevent linking against the defaults. |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 49 | System_shared_libs []string |
| 50 | |
| 51 | // allow the module to contain undefined symbols. By default, |
| 52 | // modules cannot contain undefined symbols that are not satisified by their immediate |
| 53 | // dependencies. Set this flag to true to remove --no-undefined from the linker flags. |
| 54 | // 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] | 55 | Allow_undefined_symbols *bool `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 56 | |
| 57 | // don't link in libgcc.a |
| 58 | No_libgcc *bool |
| 59 | |
| 60 | // -l arguments to pass to linker for host-provided shared libraries |
| 61 | Host_ldlibs []string `android:"arch_variant"` |
| 62 | |
| 63 | // list of shared libraries to re-export include directories from. Entries must be |
| 64 | // present in shared_libs. |
| 65 | Export_shared_lib_headers []string `android:"arch_variant"` |
| 66 | |
| 67 | // list of static libraries to re-export include directories from. Entries must be |
| 68 | // present in static_libs. |
| 69 | Export_static_lib_headers []string `android:"arch_variant"` |
| 70 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 71 | // list of header libraries to re-export include directories from. Entries must be |
| 72 | // present in header_libs. |
| 73 | Export_header_lib_headers []string `android:"arch_variant"` |
| 74 | |
Dan Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame] | 75 | // list of generated headers to re-export include directories from. Entries must be |
| 76 | // present in generated_headers. |
| 77 | Export_generated_headers []string `android:"arch_variant"` |
| 78 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 79 | // don't link in crt_begin and crt_end. This flag should only be necessary for |
| 80 | // compiling crt or libc. |
| 81 | Nocrt *bool `android:"arch_variant"` |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 82 | |
| 83 | // group static libraries. This can resolve missing symbols issues with interdependencies |
| 84 | // between static libraries, but it is generally better to order them correctly instead. |
| 85 | Group_static_libs *bool `android:"arch_variant"` |
Jiyong Park | 44cf1a7 | 2017-06-16 12:03:55 +0900 | [diff] [blame] | 86 | |
| 87 | Target struct { |
| 88 | Vendor struct { |
| 89 | // list of shared libs that should not be used to build |
| 90 | // the vendor variant of the C/C++ module. |
| 91 | Exclude_shared_libs []string |
| 92 | } |
| 93 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 96 | func NewBaseLinker() *baseLinker { |
| 97 | return &baseLinker{} |
| 98 | } |
| 99 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 100 | // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties |
| 101 | type baseLinker struct { |
| 102 | Properties BaseLinkerProperties |
| 103 | dynamicProperties struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 104 | RunPaths []string `blueprint:"mutated"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | func (linker *baseLinker) appendLdflags(flags []string) { |
| 109 | linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...) |
| 110 | } |
| 111 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 112 | func (linker *baseLinker) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 113 | if ctx.toolchain().Is64Bit() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 114 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 116 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 120 | func (linker *baseLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 121 | return []interface{}{&linker.Properties, &linker.dynamicProperties} |
| 122 | } |
| 123 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 124 | func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 125 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...) |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 126 | deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 127 | deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...) |
| 128 | deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...) |
| 129 | |
Colin Cross | 5950f38 | 2016-12-13 12:50:57 -0800 | [diff] [blame] | 130 | deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...) |
| 132 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...) |
Dan Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame] | 133 | deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 134 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame^] | 135 | if ctx.useVndk() { |
Jiyong Park | 7447228 | 2017-08-10 00:48:06 +0900 | [diff] [blame] | 136 | deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs) |
| 137 | deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs) |
| 138 | } |
| 139 | |
Dan Albert | 83705c8 | 2016-09-14 16:47:18 -0700 | [diff] [blame] | 140 | if ctx.ModuleName() != "libcompiler_rt-extras" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras") |
| 142 | } |
| 143 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 144 | if ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | // libgcc and libatomic have to be last on the command line |
| 146 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic") |
| 147 | if !Bool(linker.Properties.No_libgcc) { |
| 148 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc") |
| 149 | } |
| 150 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 151 | if !ctx.static() { |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 152 | systemSharedLibs := linker.Properties.System_shared_libs |
| 153 | if systemSharedLibs == nil { |
| 154 | systemSharedLibs = []string{"libc", "libm", "libdl"} |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 157 | if inList("libdl", deps.SharedLibs) { |
| 158 | // If system_shared_libs has libc but not libdl, make sure shared_libs does not |
| 159 | // have libdl to avoid loading libdl before libc. |
| 160 | if inList("libc", systemSharedLibs) { |
| 161 | if !inList("libdl", systemSharedLibs) { |
| 162 | ctx.PropertyErrorf("shared_libs", |
| 163 | "libdl must be in system_shared_libs, not shared_libs") |
| 164 | } |
| 165 | _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs) |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 166 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 167 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 168 | |
Colin Cross | ef88ae2 | 2017-08-18 16:52:25 -0700 | [diff] [blame] | 169 | // If libc and libdl are both in system_shared_libs make sure libd comes after libc |
| 170 | // to avoid loading libdl before libc. |
| 171 | if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) && |
| 172 | indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) { |
| 173 | ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc") |
| 174 | } |
| 175 | |
| 176 | deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...) |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame^] | 177 | } else if ctx.useSdk() || ctx.useVndk() { |
Dimitry Ivanov | ba6b552 | 2017-06-26 18:13:56 -0700 | [diff] [blame] | 178 | deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl") |
Dan Willemsen | 4416e5d | 2017-04-06 12:43:22 -0700 | [diff] [blame] | 179 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Colin Cross | 3edeee1 | 2017-04-04 12:59:48 -0700 | [diff] [blame] | 182 | if ctx.Windows() { |
Josh Gao | 7bd4c5c | 2017-02-23 17:52:24 -0800 | [diff] [blame] | 183 | deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread") |
| 184 | } |
| 185 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 186 | return deps |
| 187 | } |
| 188 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 189 | func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 190 | toolchain := ctx.toolchain() |
| 191 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 192 | if !ctx.noDefaultCompilerFlags() { |
Colin Cross | 46974e2 | 2016-10-20 12:41:14 -0700 | [diff] [blame] | 193 | if Bool(linker.Properties.Allow_undefined_symbols) { |
| 194 | if ctx.Darwin() { |
| 195 | // darwin defaults to treating undefined symbols as errors |
| 196 | flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup") |
| 197 | } |
| 198 | } else if !ctx.Darwin() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 199 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined") |
| 200 | } |
| 201 | |
| 202 | if flags.Clang { |
| 203 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags()) |
| 204 | } else { |
| 205 | flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags()) |
| 206 | } |
| 207 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 208 | if !ctx.toolchain().Bionic() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 209 | CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs) |
| 210 | |
| 211 | flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...) |
Colin Cross | c5fdbb8 | 2017-09-08 12:45:18 -0700 | [diff] [blame] | 212 | |
| 213 | if !ctx.Windows() { |
Dan Willemsen | 27991b7 | 2017-09-26 20:23:34 -0700 | [diff] [blame] | 214 | // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device |
Colin Cross | c5fdbb8 | 2017-09-08 12:45:18 -0700 | [diff] [blame] | 215 | // builds |
| 216 | flags.LdFlags = append(flags.LdFlags, |
| 217 | "-ldl", |
| 218 | "-lpthread", |
Dan Willemsen | 27991b7 | 2017-09-26 20:23:34 -0700 | [diff] [blame] | 219 | "-lm", |
Colin Cross | c5fdbb8 | 2017-09-08 12:45:18 -0700 | [diff] [blame] | 220 | ) |
| 221 | if !ctx.Darwin() { |
| 222 | flags.LdFlags = append(flags.LdFlags, "-lrt") |
| 223 | } |
| 224 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags) |
| 229 | |
Colin Cross | 4b963f8 | 2016-09-29 14:06:02 -0700 | [diff] [blame] | 230 | flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 231 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 232 | if ctx.Host() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 233 | rpath_prefix := `\$$ORIGIN/` |
| 234 | if ctx.Darwin() { |
| 235 | rpath_prefix = "@loader_path/" |
| 236 | } |
| 237 | |
Dan Willemsen | 6f91fbf | 2017-09-18 22:45:15 -0700 | [diff] [blame] | 238 | if !ctx.static() { |
| 239 | for _, rpath := range linker.dynamicProperties.RunPaths { |
| 240 | flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath) |
| 241 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame^] | 245 | 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] | 246 | // The bionic linker now has support gnu style hashes (which are much faster!), but shipping |
| 247 | // to older devices requires the old style hash. Fortunately, we can build with both and |
| 248 | // it'll work anywhere. |
| 249 | // This is not currently supported on MIPS architectures. |
| 250 | flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both") |
| 251 | } |
| 252 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 253 | if flags.Clang { |
| 254 | flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags()) |
| 255 | } else { |
| 256 | flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags()) |
| 257 | } |
| 258 | |
Colin Cross | 18c0c5a | 2016-12-01 14:45:23 -0800 | [diff] [blame] | 259 | if Bool(linker.Properties.Group_static_libs) { |
| 260 | flags.GroupStaticLibs = true |
| 261 | } |
| 262 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 263 | return flags |
| 264 | } |
| 265 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 266 | func (linker *baseLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 267 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 268 | panic(fmt.Errorf("baseLinker doesn't know how to link")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 269 | } |