blob: 3620f59ad9bd561c502f2a4942706b3ec8150372 [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
Logan Chien43d34c32017-12-20 01:17:32 +080091 // list of modules that should be installed with this module. This is similar to 'required'
92 // but '.vendor' suffix will be appended to the module names if the shared libraries have
93 // vendor variants and this module uses VNDK.
94 Runtime_libs []string `android:"arch_variant"`
95
Jiyong Park44cf1a72017-06-16 12:03:55 +090096 Target struct {
97 Vendor struct {
Logan Chien43d34c32017-12-20 01:17:32 +080098 // list of shared libs that should not be used to build the vendor variant
99 // of the C/C++ module.
Jiyong Park44cf1a72017-06-16 12:03:55 +0900100 Exclude_shared_libs []string
Jiyong Park52d25bd2017-10-13 09:17:01 +0900101
Logan Chien43d34c32017-12-20 01:17:32 +0800102 // list of static libs that should not be used to build the vendor variant
103 // of the C/C++ module.
Jiyong Park52d25bd2017-10-13 09:17:01 +0900104 Exclude_static_libs []string
Logan Chien43d34c32017-12-20 01:17:32 +0800105
Jiwen 'Steve' Cai8dbc6532018-08-08 10:58:34 -0700106 // list of header libs that should not be used to build the vendor variant
107 // of the C/C++ module.
108 Exclude_header_libs []string
109
Logan Chien43d34c32017-12-20 01:17:32 +0800110 // list of runtime libs that should not be installed along with the vendor
111 // variant of the C/C++ module.
112 Exclude_runtime_libs []string
Jiyong Park44cf1a72017-06-16 12:03:55 +0900113 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900114 Recovery struct {
115 // list of shared libs that should not be used to build
116 // the recovery variant of the C/C++ module.
117 Exclude_shared_libs []string
118
119 // list of static libs that should not be used to build
120 // the recovery variant of the C/C++ module.
121 Exclude_static_libs []string
122 }
Jiyong Park44cf1a72017-06-16 12:03:55 +0900123 }
Colin Cross86803cf2018-02-15 14:12:26 -0800124
125 // make android::build:GetBuildNumber() available containing the build ID.
126 Use_version_lib *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127}
128
Chih-Hung Hsieh86814712018-05-23 18:30:46 -0700129// TODO(http://b/80437643): BaseLinkerProperties is getting too big,
130// more than 2^16 bytes. New properties are defined in MoreBaseLinkerProperties.
131type MoreBaseLinkerProperties struct {
132 // Generate compact dynamic relocation table, default true.
133 Pack_relocations *bool `android:"arch_variant"`
Dan Albert61f32122018-07-26 14:00:24 -0700134
135 // local file name to pass to the linker as --version_script
136 Version_script *string `android:"arch_variant"`
137
138 Target struct {
139 Vendor struct {
140 // version script for this vendor variant
141 Version_script *string `android:"arch_variant"`
142 }
143 }
Chih-Hung Hsieh86814712018-05-23 18:30:46 -0700144}
145
Dan Albert61f32122018-07-26 14:00:24 -0700146func NewBaseLinker(sanitize *sanitize) *baseLinker {
147 return &baseLinker{sanitize: sanitize}
Colin Crossb916a382016-07-29 17:28:03 -0700148}
149
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
151type baseLinker struct {
152 Properties BaseLinkerProperties
Chih-Hung Hsieh86814712018-05-23 18:30:46 -0700153 MoreProperties MoreBaseLinkerProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700155 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 }
Dan Albert61f32122018-07-26 14:00:24 -0700157
158 sanitize *sanitize
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159}
160
161func (linker *baseLinker) appendLdflags(flags []string) {
162 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
163}
164
Colin Cross42742b82016-08-01 13:20:05 -0700165func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700167 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700169 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 }
171}
172
Colin Cross42742b82016-08-01 13:20:05 -0700173func (linker *baseLinker) linkerProps() []interface{} {
Chih-Hung Hsieh86814712018-05-23 18:30:46 -0700174 return []interface{}{&linker.Properties, &linker.MoreProperties, &linker.dynamicProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175}
176
Dan Albert61f32122018-07-26 14:00:24 -0700177func (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
Colin Cross5950f382016-12-13 12:50:57 -0800179 deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
181 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Logan Chien43d34c32017-12-20 01:17:32 +0800182 deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183
Colin Cross5950f382016-12-13 12:50:57 -0800184 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
186 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700187 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188
Colin Cross86803cf2018-02-15 14:12:26 -0800189 if Bool(linker.Properties.Use_version_lib) {
190 deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
191 }
192
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700193 if ctx.useVndk() {
Jiyong Park74472282017-08-10 00:48:06 +0900194 deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
195 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900196 deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
Jiwen 'Steve' Cai8dbc6532018-08-08 10:58:34 -0700197 deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900198 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
199 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
Logan Chien43d34c32017-12-20 01:17:32 +0800200 deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
Jiyong Park74472282017-08-10 00:48:06 +0900201 }
202
Jiyong Parkf9332f12018-02-01 00:54:12 +0900203 if ctx.inRecovery() {
204 deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
205 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
206 deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
207 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
208 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
209 }
210
Dan Albert83705c82016-09-14 16:47:18 -0700211 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
213 }
214
Dan Willemsen2e47b342016-11-17 01:02:25 -0800215 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 // libgcc and libatomic have to be last on the command line
217 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
218 if !Bool(linker.Properties.No_libgcc) {
219 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
220 }
221
Colin Crossb916a382016-07-29 17:28:03 -0700222 if !ctx.static() {
Colin Crossef88ae22017-08-18 16:52:25 -0700223 systemSharedLibs := linker.Properties.System_shared_libs
224 if systemSharedLibs == nil {
225 systemSharedLibs = []string{"libc", "libm", "libdl"}
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700226 }
227
Colin Crossef88ae22017-08-18 16:52:25 -0700228 if inList("libdl", deps.SharedLibs) {
229 // If system_shared_libs has libc but not libdl, make sure shared_libs does not
230 // have libdl to avoid loading libdl before libc.
231 if inList("libc", systemSharedLibs) {
232 if !inList("libdl", systemSharedLibs) {
233 ctx.PropertyErrorf("shared_libs",
234 "libdl must be in system_shared_libs, not shared_libs")
235 }
236 _, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700237 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239
Colin Crossef88ae22017-08-18 16:52:25 -0700240 // If libc and libdl are both in system_shared_libs make sure libd comes after libc
241 // to avoid loading libdl before libc.
242 if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
243 indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
244 ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
245 }
246
247 deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700248 } else if ctx.useSdk() || ctx.useVndk() {
Dimitry Ivanovba6b5522017-06-26 18:13:56 -0700249 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700250 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 }
252
Colin Cross3edeee12017-04-04 12:59:48 -0700253 if ctx.Windows() {
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800254 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
255 }
256
Dan Albert61f32122018-07-26 14:00:24 -0700257 android.ExtractSourceDeps(ctx, linker.MoreProperties.Version_script)
258 android.ExtractSourceDeps(ctx,
259 linker.MoreProperties.Target.Vendor.Version_script)
260
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 return deps
262}
263
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700264func (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
Chih-Hung Hsiehe5ac6092018-04-24 16:00:01 -0700265 // Clang lld is not ready for for Darwin host executables yet.
266 // See https://lld.llvm.org/AtomLLD.html for status of lld for Mach-O.
267 if ctx.Darwin() {
268 return false
269 }
Pirama Arumuga Nainar8a852e72018-06-19 20:07:54 -0700270 // http://b/110800681 - lld cannot link Android's Windows modules yet.
271 if ctx.Windows() {
272 return false
273 }
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700274 if linker.Properties.Use_clang_lld != nil {
275 return Bool(linker.Properties.Use_clang_lld)
276 }
277 return ctx.Config().UseClangLld()
278}
279
280// ModuleContext extends BaseModuleContext
281// BaseModuleContext should know if LLD is used?
Colin Cross42742b82016-08-01 13:20:05 -0700282func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 toolchain := ctx.toolchain()
284
Colin Cross324a4572017-11-02 23:09:41 -0700285 hod := "Host"
286 if ctx.Os().Class == android.Device {
287 hod = "Device"
288 }
289
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700290 if flags.Clang && linker.useClangLld(ctx) {
291 flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
Chih-Hung Hsieh86814712018-05-23 18:30:46 -0700292 if !BoolDefault(linker.MoreProperties.Pack_relocations, true) {
293 flags.LdFlags = append(flags.LdFlags, "-Wl,--pack-dyn-relocs=none")
294 }
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700295 } else {
296 flags.LdFlags = append(flags.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
297 }
Colin Cross87dd9632017-11-03 13:31:05 -0700298 if Bool(linker.Properties.Allow_undefined_symbols) {
299 if ctx.Darwin() {
300 // darwin defaults to treating undefined symbols as errors
301 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302 }
Colin Cross87dd9632017-11-03 13:31:05 -0700303 } else if !ctx.Darwin() {
304 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
305 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700307 if flags.Clang && linker.useClangLld(ctx) {
308 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLldflags())
309 } else if flags.Clang {
Colin Cross87dd9632017-11-03 13:31:05 -0700310 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
311 } else {
312 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
313 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314
Colin Cross87dd9632017-11-03 13:31:05 -0700315 if !ctx.toolchain().Bionic() {
316 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700317
Colin Cross87dd9632017-11-03 13:31:05 -0700318 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
Colin Crossc5fdbb82017-09-08 12:45:18 -0700319
Colin Cross87dd9632017-11-03 13:31:05 -0700320 if !ctx.Windows() {
321 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
322 // builds
323 flags.LdFlags = append(flags.LdFlags,
324 "-ldl",
325 "-lpthread",
326 "-lm",
327 )
328 if !ctx.Darwin() {
329 flags.LdFlags = append(flags.LdFlags, "-lrt")
Colin Crossc5fdbb82017-09-08 12:45:18 -0700330 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 }
332 }
333
334 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
335
Colin Cross4b963f82016-09-29 14:06:02 -0700336 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337
Colin Crossb916a382016-07-29 17:28:03 -0700338 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339 rpath_prefix := `\$$ORIGIN/`
340 if ctx.Darwin() {
341 rpath_prefix = "@loader_path/"
342 }
343
Dan Willemsen6f91fbf2017-09-18 22:45:15 -0700344 if !ctx.static() {
345 for _, rpath := range linker.dynamicProperties.RunPaths {
346 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
347 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 }
349 }
350
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700351 if ctx.useSdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
Colin Cross6774e282017-08-11 13:23:57 -0700352 // The bionic linker now has support gnu style hashes (which are much faster!), but shipping
353 // to older devices requires the old style hash. Fortunately, we can build with both and
354 // it'll work anywhere.
355 // This is not currently supported on MIPS architectures.
356 flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both")
357 }
358
Colin Cross4d9c2d12016-07-29 12:48:20 -0700359 if flags.Clang {
360 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
361 } else {
362 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
363 }
364
Colin Cross18c0c5a2016-12-01 14:45:23 -0800365 if Bool(linker.Properties.Group_static_libs) {
366 flags.GroupStaticLibs = true
367 }
368
Dan Albert61f32122018-07-26 14:00:24 -0700369 versionScript := ctx.ExpandOptionalSource(
370 linker.MoreProperties.Version_script, "version_script")
371
372 if ctx.useVndk() && linker.MoreProperties.Target.Vendor.Version_script != nil {
373 versionScript = ctx.ExpandOptionalSource(
374 linker.MoreProperties.Target.Vendor.Version_script,
375 "target.vendor.version_script")
376 }
377
378 if versionScript.Valid() {
379 if ctx.Darwin() {
380 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
381 } else {
382 flags.LdFlags = append(flags.LdFlags,
383 "-Wl,--version-script,"+versionScript.String())
384 flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
385
386 if linker.sanitize.isSanitizerEnabled(cfi) {
387 cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
388 flags.LdFlags = append(flags.LdFlags,
389 "-Wl,--version-script,"+cfiExportsMap.String())
390 flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
391 }
392 }
393 }
394
Colin Cross4d9c2d12016-07-29 12:48:20 -0700395 return flags
396}
397
Colin Crossb916a382016-07-29 17:28:03 -0700398func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700399 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700400 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700401}
Colin Cross86803cf2018-02-15 14:12:26 -0800402
403// Injecting version symbols
404// Some host modules want a version number, but we don't want to rebuild it every time. Optionally add a step
405// after linking that injects a constant placeholder with the current version number.
406
407func init() {
408 pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
409}
410
411var injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
412 blueprint.RuleParams{
413 Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
414 "-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $buildNumberFromFile",
415 CommandDeps: []string{"$symbolInjectCmd"},
416 },
417 "buildNumberFromFile")
418
419func (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
420 ctx.Build(pctx, android.BuildParams{
421 Rule: injectVersionSymbol,
422 Description: "inject version symbol",
423 Input: in,
424 Output: out,
425 Args: map[string]string{
426 "buildNumberFromFile": ctx.Config().BuildNumberFromFile(),
427 },
428 })
429}