blob: 6ec56307e776024ad359ee01d610ebcd3ccf25a8 [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
92 }
93 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070094}
95
Colin Crossb916a382016-07-29 17:28:03 -070096func NewBaseLinker() *baseLinker {
97 return &baseLinker{}
98}
99
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
101type baseLinker struct {
102 Properties BaseLinkerProperties
103 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700104 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 }
106}
107
108func (linker *baseLinker) appendLdflags(flags []string) {
109 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
110}
111
Colin Cross42742b82016-08-01 13:20:05 -0700112func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700114 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700116 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 }
118}
119
Colin Cross42742b82016-08-01 13:20:05 -0700120func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 return []interface{}{&linker.Properties, &linker.dynamicProperties}
122}
123
Colin Cross42742b82016-08-01 13:20:05 -0700124func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
Colin Cross5950f382016-12-13 12:50:57 -0800126 deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
128 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
129
Colin Cross5950f382016-12-13 12:50:57 -0800130 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
132 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700133 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700135 if ctx.useVndk() {
Jiyong Park74472282017-08-10 00:48:06 +0900136 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 Albert83705c82016-09-14 16:47:18 -0700140 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
142 }
143
Dan Willemsen2e47b342016-11-17 01:02:25 -0800144 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 // 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 Crossb916a382016-07-29 17:28:03 -0700151 if !ctx.static() {
Colin Crossef88ae22017-08-18 16:52:25 -0700152 systemSharedLibs := linker.Properties.System_shared_libs
153 if systemSharedLibs == nil {
154 systemSharedLibs = []string{"libc", "libm", "libdl"}
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700155 }
156
Colin Crossef88ae22017-08-18 16:52:25 -0700157 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 Ivanovba6b5522017-06-26 18:13:56 -0700166 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168
Colin Crossef88ae22017-08-18 16:52:25 -0700169 // 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 Gastonaf3cc2d2017-09-27 17:01:44 -0700177 } else if ctx.useSdk() || ctx.useVndk() {
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700178 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700179 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 }
181
Colin Cross3edeee12017-04-04 12:59:48 -0700182 if ctx.Windows() {
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800183 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
184 }
185
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 return deps
187}
188
Colin Cross42742b82016-08-01 13:20:05 -0700189func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 toolchain := ctx.toolchain()
191
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 if !ctx.noDefaultCompilerFlags() {
Colin Cross46974e22016-10-20 12:41:14 -0700193 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 Cross4d9c2d12016-07-29 12:48:20 -0700199 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 Willemsen2e47b342016-11-17 01:02:25 -0800208 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
210
211 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
Colin Crossc5fdbb82017-09-08 12:45:18 -0700212
213 if !ctx.Windows() {
Dan Willemsen27991b72017-09-26 20:23:34 -0700214 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
Colin Crossc5fdbb82017-09-08 12:45:18 -0700215 // builds
216 flags.LdFlags = append(flags.LdFlags,
217 "-ldl",
218 "-lpthread",
Dan Willemsen27991b72017-09-26 20:23:34 -0700219 "-lm",
Colin Crossc5fdbb82017-09-08 12:45:18 -0700220 )
221 if !ctx.Darwin() {
222 flags.LdFlags = append(flags.LdFlags, "-lrt")
223 }
224 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
226 }
227
228 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
229
Colin Cross4b963f82016-09-29 14:06:02 -0700230 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231
Colin Crossb916a382016-07-29 17:28:03 -0700232 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 rpath_prefix := `\$$ORIGIN/`
234 if ctx.Darwin() {
235 rpath_prefix = "@loader_path/"
236 }
237
Dan Willemsen6f91fbf2017-09-18 22:45:15 -0700238 if !ctx.static() {
239 for _, rpath := range linker.dynamicProperties.RunPaths {
240 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
241 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 }
243 }
244
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700245 if ctx.useSdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
Colin Cross6774e282017-08-11 13:23:57 -0700246 // 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 Cross4d9c2d12016-07-29 12:48:20 -0700253 if flags.Clang {
254 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
255 } else {
256 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
257 }
258
Colin Cross18c0c5a2016-12-01 14:45:23 -0800259 if Bool(linker.Properties.Group_static_libs) {
260 flags.GroupStaticLibs = true
261 }
262
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 return flags
264}
265
Colin Crossb916a382016-07-29 17:28:03 -0700266func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700267 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700268 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269}