blob: d5727aa07802ea5a5f26c5932b874f1bbcccb72a [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
21 "github.com/google/blueprint/proptools"
Colin Crossb916a382016-07-29 17:28:03 -070022)
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024// 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
27type 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 Cross5950f382016-12-13 12:50:57 -080040 // list of modules that should only provide headers for this module.
41 Header_libs []string `android:"arch_variant,variant_prepend"`
42
Colin Cross4d9c2d12016-07-29 12:48:20 -070043 // list of module-specific flags that will be used for all link steps
44 Ldflags []string `android:"arch_variant"`
45
Colin Cross4d9c2d12016-07-29 12:48:20 -070046 // list of system libraries that will be dynamically linked to
Colin Crossef88ae22017-08-18 16:52:25 -070047 // shared library and executable modules. If unset, generally defaults to libc,
48 // libm, and libdl. Set to [] to prevent linking against the defaults.
Colin Cross4d9c2d12016-07-29 12:48:20 -070049 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 Crossbe360ae2016-12-08 09:45:21 -080055 Allow_undefined_symbols *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070056
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 Cross5950f382016-12-13 12:50:57 -080071 // 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 Willemsenb3454ab2016-09-28 17:34:58 -070075 // 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 Cross4d9c2d12016-07-29 12:48:20 -070079 // 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 Cross18c0c5a2016-12-01 14:45:23 -080082
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 Park44cf1a72017-06-16 12:03:55 +090086
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
Jiyong Park52d25bd2017-10-13 09:17:01 +090092
93 // list of static libs that should not be used to build
94 // the vendor variant of the C/C++ module.
95 Exclude_static_libs []string
Jiyong Park44cf1a72017-06-16 12:03:55 +090096 }
97 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070098}
99
Colin Crossb916a382016-07-29 17:28:03 -0700100func NewBaseLinker() *baseLinker {
101 return &baseLinker{}
102}
103
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
105type baseLinker struct {
106 Properties BaseLinkerProperties
107 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700108 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 }
110}
111
112func (linker *baseLinker) appendLdflags(flags []string) {
113 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
114}
115
Colin Cross42742b82016-08-01 13:20:05 -0700116func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700118 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700120 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 }
122}
123
Colin Cross42742b82016-08-01 13:20:05 -0700124func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 return []interface{}{&linker.Properties, &linker.dynamicProperties}
126}
127
Colin Cross42742b82016-08-01 13:20:05 -0700128func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
Colin Cross5950f382016-12-13 12:50:57 -0800130 deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
132 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
133
Colin Cross5950f382016-12-13 12:50:57 -0800134 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
136 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700137 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700139 if ctx.useVndk() {
Jiyong Park74472282017-08-10 00:48:06 +0900140 deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
141 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900142 deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
143 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
144 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park74472282017-08-10 00:48:06 +0900145 }
146
Dan Albert83705c82016-09-14 16:47:18 -0700147 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
149 }
150
Dan Willemsen2e47b342016-11-17 01:02:25 -0800151 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 // libgcc and libatomic have to be last on the command line
153 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
154 if !Bool(linker.Properties.No_libgcc) {
155 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
156 }
157
Colin Crossb916a382016-07-29 17:28:03 -0700158 if !ctx.static() {
Colin Crossef88ae22017-08-18 16:52:25 -0700159 systemSharedLibs := linker.Properties.System_shared_libs
160 if systemSharedLibs == nil {
161 systemSharedLibs = []string{"libc", "libm", "libdl"}
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700162 }
163
Colin Crossef88ae22017-08-18 16:52:25 -0700164 if inList("libdl", deps.SharedLibs) {
165 // If system_shared_libs has libc but not libdl, make sure shared_libs does not
166 // have libdl to avoid loading libdl before libc.
167 if inList("libc", systemSharedLibs) {
168 if !inList("libdl", systemSharedLibs) {
169 ctx.PropertyErrorf("shared_libs",
170 "libdl must be in system_shared_libs, not shared_libs")
171 }
172 _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700173 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175
Colin Crossef88ae22017-08-18 16:52:25 -0700176 // If libc and libdl are both in system_shared_libs make sure libd comes after libc
177 // to avoid loading libdl before libc.
178 if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
179 indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
180 ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
181 }
182
183 deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700184 } else if ctx.useSdk() || ctx.useVndk() {
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700185 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700186 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187 }
188
Colin Cross3edeee12017-04-04 12:59:48 -0700189 if ctx.Windows() {
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800190 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
191 }
192
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 return deps
194}
195
Colin Cross42742b82016-08-01 13:20:05 -0700196func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 toolchain := ctx.toolchain()
198
Colin Cross324a4572017-11-02 23:09:41 -0700199 hod := "Host"
200 if ctx.Os().Class == android.Device {
201 hod = "Device"
202 }
203
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 if !ctx.noDefaultCompilerFlags() {
Colin Cross324a4572017-11-02 23:09:41 -0700205 flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
Colin Cross46974e22016-10-20 12:41:14 -0700206 if Bool(linker.Properties.Allow_undefined_symbols) {
207 if ctx.Darwin() {
208 // darwin defaults to treating undefined symbols as errors
209 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
210 }
211 } else if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
213 }
214
215 if flags.Clang {
216 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
217 } else {
218 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
219 }
220
Dan Willemsen2e47b342016-11-17 01:02:25 -0800221 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
223
224 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
Colin Crossc5fdbb82017-09-08 12:45:18 -0700225
226 if !ctx.Windows() {
Dan Willemsen27991b72017-09-26 20:23:34 -0700227 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
Colin Crossc5fdbb82017-09-08 12:45:18 -0700228 // builds
229 flags.LdFlags = append(flags.LdFlags,
230 "-ldl",
231 "-lpthread",
Dan Willemsen27991b72017-09-26 20:23:34 -0700232 "-lm",
Colin Crossc5fdbb82017-09-08 12:45:18 -0700233 )
234 if !ctx.Darwin() {
235 flags.LdFlags = append(flags.LdFlags, "-lrt")
236 }
237 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
239 }
240
241 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
242
Colin Cross4b963f82016-09-29 14:06:02 -0700243 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244
Colin Crossb916a382016-07-29 17:28:03 -0700245 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 rpath_prefix := `\$$ORIGIN/`
247 if ctx.Darwin() {
248 rpath_prefix = "@loader_path/"
249 }
250
Dan Willemsen6f91fbf2017-09-18 22:45:15 -0700251 if !ctx.static() {
252 for _, rpath := range linker.dynamicProperties.RunPaths {
253 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
254 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
256 }
257
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700258 if ctx.useSdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
Colin Cross6774e282017-08-11 13:23:57 -0700259 // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
260 // to older devices requires the old style hash. Fortunately, we can build with both and
261 // it'll work anywhere.
262 // This is not currently supported on MIPS architectures.
263 flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both")
264 }
265
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 if flags.Clang {
267 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
268 } else {
269 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
270 }
271
Colin Cross18c0c5a2016-12-01 14:45:23 -0800272 if Bool(linker.Properties.Group_static_libs) {
273 flags.GroupStaticLibs = true
274 }
275
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 return flags
277}
278
Colin Crossb916a382016-07-29 17:28:03 -0700279func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700280 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700281 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282}