blob: cde7a6dd09d781d6d33b9b43d6006b91e385ef72 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
15package cc
16
Colin Crossb916a382016-07-29 17:28:03 -070017import (
18 "android/soong/android"
19 "fmt"
Colin Cross4b963f82016-09-29 14:06:02 -070020
Colin Cross86803cf2018-02-15 14:12:26 -080021 "github.com/google/blueprint"
Colin Cross4b963f82016-09-29 14:06:02 -070022 "github.com/google/blueprint/proptools"
Colin Crossb916a382016-07-29 17:28:03 -070023)
24
Colin Cross4d9c2d12016-07-29 12:48:20 -070025// 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
28type 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 Cross5950f382016-12-13 12:50:57 -080041 // list of modules that should only provide headers for this module.
42 Header_libs []string `android:"arch_variant,variant_prepend"`
43
Colin Cross4d9c2d12016-07-29 12:48:20 -070044 // list of module-specific flags that will be used for all link steps
45 Ldflags []string `android:"arch_variant"`
46
Colin Cross4d9c2d12016-07-29 12:48:20 -070047 // list of system libraries that will be dynamically linked to
Colin Crossef88ae22017-08-18 16:52:25 -070048 // shared library and executable modules. If unset, generally defaults to libc,
49 // libm, and libdl. Set to [] to prevent linking against the defaults.
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 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 Crossbe360ae2016-12-08 09:45:21 -080056 Allow_undefined_symbols *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070057
58 // don't link in libgcc.a
59 No_libgcc *bool
60
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070061 // Use clang lld instead of gnu ld.
Pirama Arumuga Nainar2b8959a2018-04-20 11:52:42 -070062 Use_clang_lld *bool `android:"arch_variant"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070063
Colin Cross4d9c2d12016-07-29 12:48:20 -070064 // -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 Cross5950f382016-12-13 12:50:57 -080075 // 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 Willemsenb3454ab2016-09-28 17:34:58 -070079 // 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 Cross4d9c2d12016-07-29 12:48:20 -070083 // 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 Cross18c0c5a2016-12-01 14:45:23 -080086
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 Park44cf1a72017-06-16 12:03:55 +090090
91 Target struct {
92 Vendor struct {
93 // list of shared libs that should not be used to build
94 // the vendor variant of the C/C++ module.
95 Exclude_shared_libs []string
Jiyong Park52d25bd2017-10-13 09:17:01 +090096
97 // list of static libs that should not be used to build
98 // the vendor variant of the C/C++ module.
99 Exclude_static_libs []string
Jiyong Park44cf1a72017-06-16 12:03:55 +0900100 }
101 }
Colin Cross86803cf2018-02-15 14:12:26 -0800102
103 // make android::build:GetBuildNumber() available containing the build ID.
104 Use_version_lib *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105}
106
Colin Crossb916a382016-07-29 17:28:03 -0700107func NewBaseLinker() *baseLinker {
108 return &baseLinker{}
109}
110
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
112type baseLinker struct {
113 Properties BaseLinkerProperties
114 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700115 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 }
117}
118
119func (linker *baseLinker) appendLdflags(flags []string) {
120 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
121}
122
Colin Cross42742b82016-08-01 13:20:05 -0700123func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700125 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700127 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 }
129}
130
Colin Cross42742b82016-08-01 13:20:05 -0700131func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132 return []interface{}{&linker.Properties, &linker.dynamicProperties}
133}
134
Colin Cross42742b82016-08-01 13:20:05 -0700135func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
Colin Cross5950f382016-12-13 12:50:57 -0800137 deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
139 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
140
Colin Cross5950f382016-12-13 12:50:57 -0800141 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
143 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700144 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145
Colin Cross86803cf2018-02-15 14:12:26 -0800146 if Bool(linker.Properties.Use_version_lib) {
147 deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
148 }
149
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700150 if ctx.useVndk() {
Jiyong Park74472282017-08-10 00:48:06 +0900151 deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
152 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900153 deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
154 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
155 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park74472282017-08-10 00:48:06 +0900156 }
157
Dan Albert83705c82016-09-14 16:47:18 -0700158 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
160 }
161
Dan Willemsen2e47b342016-11-17 01:02:25 -0800162 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 // libgcc and libatomic have to be last on the command line
164 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
165 if !Bool(linker.Properties.No_libgcc) {
166 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
167 }
168
Colin Crossb916a382016-07-29 17:28:03 -0700169 if !ctx.static() {
Colin Crossef88ae22017-08-18 16:52:25 -0700170 systemSharedLibs := linker.Properties.System_shared_libs
171 if systemSharedLibs == nil {
172 systemSharedLibs = []string{"libc", "libm", "libdl"}
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700173 }
174
Colin Crossef88ae22017-08-18 16:52:25 -0700175 if inList("libdl", deps.SharedLibs) {
176 // If system_shared_libs has libc but not libdl, make sure shared_libs does not
177 // have libdl to avoid loading libdl before libc.
178 if inList("libc", systemSharedLibs) {
179 if !inList("libdl", systemSharedLibs) {
180 ctx.PropertyErrorf("shared_libs",
181 "libdl must be in system_shared_libs, not shared_libs")
182 }
183 _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700184 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186
Colin Crossef88ae22017-08-18 16:52:25 -0700187 // If libc and libdl are both in system_shared_libs make sure libd comes after libc
188 // to avoid loading libdl before libc.
189 if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
190 indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
191 ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
192 }
193
194 deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700195 } else if ctx.useSdk() || ctx.useVndk() {
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700196 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700197 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 }
199
Colin Cross3edeee12017-04-04 12:59:48 -0700200 if ctx.Windows() {
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800201 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
202 }
203
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 return deps
205}
206
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700207func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
Chih-Hung Hsiehe5ac6092018-04-24 16:00:01 -0700208 // Clang lld is not ready for for Darwin host executables yet.
209 // See https://lld.llvm.org/AtomLLD.html for status of lld for Mach-O.
210 if ctx.Darwin() {
211 return false
212 }
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700213 if linker.Properties.Use_clang_lld != nil {
214 return Bool(linker.Properties.Use_clang_lld)
215 }
216 return ctx.Config().UseClangLld()
217}
218
219// ModuleContext extends BaseModuleContext
220// BaseModuleContext should know if LLD is used?
Colin Cross42742b82016-08-01 13:20:05 -0700221func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 toolchain := ctx.toolchain()
223
Colin Cross324a4572017-11-02 23:09:41 -0700224 hod := "Host"
225 if ctx.Os().Class == android.Device {
226 hod = "Device"
227 }
228
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700229 if flags.Clang && linker.useClangLld(ctx) {
230 flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
231 } else {
232 flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
233 }
Colin Cross87dd9632017-11-03 13:31:05 -0700234 if Bool(linker.Properties.Allow_undefined_symbols) {
235 if ctx.Darwin() {
236 // darwin defaults to treating undefined symbols as errors
237 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
Colin Cross87dd9632017-11-03 13:31:05 -0700239 } else if !ctx.Darwin() {
240 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
241 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700243 if flags.Clang && linker.useClangLld(ctx) {
244 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLldflags())
245 } else if flags.Clang {
Colin Cross87dd9632017-11-03 13:31:05 -0700246 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
247 } else {
248 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
249 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250
Colin Cross87dd9632017-11-03 13:31:05 -0700251 if !ctx.toolchain().Bionic() {
252 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253
Colin Cross87dd9632017-11-03 13:31:05 -0700254 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
Colin Crossc5fdbb82017-09-08 12:45:18 -0700255
Colin Cross87dd9632017-11-03 13:31:05 -0700256 if !ctx.Windows() {
257 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
258 // builds
259 flags.LdFlags = append(flags.LdFlags,
260 "-ldl",
261 "-lpthread",
262 "-lm",
263 )
264 if !ctx.Darwin() {
265 flags.LdFlags = append(flags.LdFlags, "-lrt")
Colin Crossc5fdbb82017-09-08 12:45:18 -0700266 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 }
268 }
269
270 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
271
Colin Cross4b963f82016-09-29 14:06:02 -0700272 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273
Colin Crossb916a382016-07-29 17:28:03 -0700274 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 rpath_prefix := `\$$ORIGIN/`
276 if ctx.Darwin() {
277 rpath_prefix = "@loader_path/"
278 }
279
Dan Willemsen6f91fbf2017-09-18 22:45:15 -0700280 if !ctx.static() {
281 for _, rpath := range linker.dynamicProperties.RunPaths {
282 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
283 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 }
285 }
286
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700287 if ctx.useSdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
Colin Cross6774e282017-08-11 13:23:57 -0700288 // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
289 // to older devices requires the old style hash. Fortunately, we can build with both and
290 // it'll work anywhere.
291 // This is not currently supported on MIPS architectures.
292 flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both")
293 }
294
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 if flags.Clang {
296 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
297 } else {
298 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
299 }
300
Colin Cross18c0c5a2016-12-01 14:45:23 -0800301 if Bool(linker.Properties.Group_static_libs) {
302 flags.GroupStaticLibs = true
303 }
304
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 return flags
306}
307
Colin Crossb916a382016-07-29 17:28:03 -0700308func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700309 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700310 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311}
Colin Cross86803cf2018-02-15 14:12:26 -0800312
313// Injecting version symbols
314// Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
315// after linking that injects a constant placeholder with the current version number.
316
317func init() {
318 pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
319}
320
321var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
322 blueprint.RuleParams{
323 Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
324 "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $buildNumberFromFile",
325 CommandDeps: []string{"$symbolInjectCmd"},
326 },
327 "buildNumberFromFile")
328
329func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
330 ctx.Build(pctx, android.BuildParams{
331 Rule: injectVersionSymbol,
332 Description: "inject version symbol",
333 Input: in,
334 Output: out,
335 Args: map[string]string{
336 "buildNumberFromFile": ctx.Config().BuildNumberFromFile(),
337 },
338 })
339}