blob: 678b992d18ee02e1d3565575a6204c58971bffa2 [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
46 // don't insert default compiler flags into asflags, cflags,
47 // cppflags, conlyflags, ldflags, or include_dirs
48 No_default_compiler_flags *bool
49
50 // list of system libraries that will be dynamically linked to
Colin Crossef88ae22017-08-18 16:52:25 -070051 // shared library and executable modules. If unset, generally defaults to libc,
52 // libm, and libdl. Set to [] to prevent linking against the defaults.
Colin Cross4d9c2d12016-07-29 12:48:20 -070053 System_shared_libs []string
54
55 // allow the module to contain undefined symbols. By default,
56 // modules cannot contain undefined symbols that are not satisified by their immediate
57 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
58 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Crossbe360ae2016-12-08 09:45:21 -080059 Allow_undefined_symbols *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070060
61 // don't link in libgcc.a
62 No_libgcc *bool
63
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 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
96 }
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
Jiyong Park74472282017-08-10 00:48:06 +0900139 if ctx.vndk() {
140 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)
142 }
143
Dan Albert83705c82016-09-14 16:47:18 -0700144 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
146 }
147
Dan Willemsen2e47b342016-11-17 01:02:25 -0800148 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149 // libgcc and libatomic have to be last on the command line
150 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
151 if !Bool(linker.Properties.No_libgcc) {
152 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
153 }
154
Colin Crossb916a382016-07-29 17:28:03 -0700155 if !ctx.static() {
Colin Crossef88ae22017-08-18 16:52:25 -0700156 systemSharedLibs := linker.Properties.System_shared_libs
157 if systemSharedLibs == nil {
158 systemSharedLibs = []string{"libc", "libm", "libdl"}
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700159 }
160
Colin Crossef88ae22017-08-18 16:52:25 -0700161 if inList("libdl", deps.SharedLibs) {
162 // If system_shared_libs has libc but not libdl, make sure shared_libs does not
163 // have libdl to avoid loading libdl before libc.
164 if inList("libc", systemSharedLibs) {
165 if !inList("libdl", systemSharedLibs) {
166 ctx.PropertyErrorf("shared_libs",
167 "libdl must be in system_shared_libs, not shared_libs")
168 }
169 _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700170 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172
Colin Crossef88ae22017-08-18 16:52:25 -0700173 // If libc and libdl are both in system_shared_libs make sure libd comes after libc
174 // to avoid loading libdl before libc.
175 if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
176 indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
177 ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
178 }
179
180 deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
181 } else if ctx.sdk() || ctx.vndk() {
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700182 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700183 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 }
185
Colin Cross3edeee12017-04-04 12:59:48 -0700186 if ctx.Windows() {
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800187 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
188 }
189
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 return deps
191}
192
Colin Cross42742b82016-08-01 13:20:05 -0700193func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 toolchain := ctx.toolchain()
195
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 if !ctx.noDefaultCompilerFlags() {
Colin Cross46974e22016-10-20 12:41:14 -0700197 if Bool(linker.Properties.Allow_undefined_symbols) {
198 if ctx.Darwin() {
199 // darwin defaults to treating undefined symbols as errors
200 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
201 }
202 } else if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
204 }
205
206 if flags.Clang {
207 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
208 } else {
209 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
210 }
211
Dan Willemsen2e47b342016-11-17 01:02:25 -0800212 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
214
215 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
216 }
217 }
218
219 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
220
Colin Cross4b963f82016-09-29 14:06:02 -0700221 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Colin Crossb916a382016-07-29 17:28:03 -0700223 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224 rpath_prefix := `\$$ORIGIN/`
225 if ctx.Darwin() {
226 rpath_prefix = "@loader_path/"
227 }
228
229 for _, rpath := range linker.dynamicProperties.RunPaths {
230 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
231 }
232 }
233
Colin Cross6774e282017-08-11 13:23:57 -0700234 if ctx.sdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
235 // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
236 // to older devices requires the old style hash. Fortunately, we can build with both and
237 // it'll work anywhere.
238 // This is not currently supported on MIPS architectures.
239 flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both")
240 }
241
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 if flags.Clang {
243 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
244 } else {
245 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
246 }
247
Colin Cross18c0c5a2016-12-01 14:45:23 -0800248 if Bool(linker.Properties.Group_static_libs) {
249 flags.GroupStaticLibs = true
250 }
251
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 return flags
253}
254
Colin Crossb916a382016-07-29 17:28:03 -0700255func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700256 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700257 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258}