xref: /aosp_15_r20/build/soong/cc/linker.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2016 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage cc
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"fmt"
19*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
22*333d2b36SAndroid Build Coastguard Worker	"android/soong/cc/config"
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
25*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
26*333d2b36SAndroid Build Coastguard Worker)
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Worker// This file contains the basic functionality for linking against static libraries and shared
29*333d2b36SAndroid Build Coastguard Worker// libraries.  Final linking into libraries or executables is handled in library.go, binary.go, etc.
30*333d2b36SAndroid Build Coastguard Worker
31*333d2b36SAndroid Build Coastguard Workerconst (
32*333d2b36SAndroid Build Coastguard Worker	packRelocationsDefault = true
33*333d2b36SAndroid Build Coastguard Worker)
34*333d2b36SAndroid Build Coastguard Worker
35*333d2b36SAndroid Build Coastguard Workertype BaseLinkerProperties struct {
36*333d2b36SAndroid Build Coastguard Worker	// list of modules whose object files should be linked into this module
37*333d2b36SAndroid Build Coastguard Worker	// in their entirety.  For static library modules, all of the .o files from the intermediate
38*333d2b36SAndroid Build Coastguard Worker	// directory of the dependency will be linked into this modules .a file.  For a shared library,
39*333d2b36SAndroid Build Coastguard Worker	// the dependency's .a file will be linked into this module using -Wl,--whole-archive.
40*333d2b36SAndroid Build Coastguard Worker	Whole_static_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
41*333d2b36SAndroid Build Coastguard Worker
42*333d2b36SAndroid Build Coastguard Worker	// list of modules that should be statically linked into this module.
43*333d2b36SAndroid Build Coastguard Worker	Static_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
44*333d2b36SAndroid Build Coastguard Worker
45*333d2b36SAndroid Build Coastguard Worker	// list of modules that should be dynamically linked into this module.
46*333d2b36SAndroid Build Coastguard Worker	Shared_libs proptools.Configurable[[]string] `android:"arch_variant"`
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Worker	// list of modules that should only provide headers for this module.
49*333d2b36SAndroid Build Coastguard Worker	Header_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
50*333d2b36SAndroid Build Coastguard Worker
51*333d2b36SAndroid Build Coastguard Worker	// list of module-specific flags that will be used for all link steps
52*333d2b36SAndroid Build Coastguard Worker	Ldflags []string `android:"arch_variant"`
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Worker	// list of system libraries that will be dynamically linked to
55*333d2b36SAndroid Build Coastguard Worker	// shared library and executable modules.  If unset, generally defaults to libc,
56*333d2b36SAndroid Build Coastguard Worker	// libm, and libdl.  Set to [] to prevent linking against the defaults.
57*333d2b36SAndroid Build Coastguard Worker	System_shared_libs []string `android:"arch_variant"`
58*333d2b36SAndroid Build Coastguard Worker
59*333d2b36SAndroid Build Coastguard Worker	// allow the module to contain undefined symbols.  By default,
60*333d2b36SAndroid Build Coastguard Worker	// modules cannot contain undefined symbols that are not satisified by their immediate
61*333d2b36SAndroid Build Coastguard Worker	// dependencies.  Set this flag to true to remove --no-undefined from the linker flags.
62*333d2b36SAndroid Build Coastguard Worker	// This flag should only be necessary for compiling low-level libraries like libc.
63*333d2b36SAndroid Build Coastguard Worker	Allow_undefined_symbols *bool `android:"arch_variant"`
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Worker	// ignore max page size. By default, max page size must be the
66*333d2b36SAndroid Build Coastguard Worker	// max page size set for the target.
67*333d2b36SAndroid Build Coastguard Worker	Ignore_max_page_size *bool `android:"arch_variant"`
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker	// don't link in libclang_rt.builtins-*.a
70*333d2b36SAndroid Build Coastguard Worker	No_libcrt *bool `android:"arch_variant"`
71*333d2b36SAndroid Build Coastguard Worker
72*333d2b36SAndroid Build Coastguard Worker	// Use clang lld instead of gnu ld.
73*333d2b36SAndroid Build Coastguard Worker	Use_clang_lld *bool `android:"arch_variant"`
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker	// -l arguments to pass to linker for host-provided shared libraries
76*333d2b36SAndroid Build Coastguard Worker	Host_ldlibs []string `android:"arch_variant"`
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker	// list of shared libraries to re-export include directories from. Entries must be
79*333d2b36SAndroid Build Coastguard Worker	// present in shared_libs.
80*333d2b36SAndroid Build Coastguard Worker	Export_shared_lib_headers []string `android:"arch_variant"`
81*333d2b36SAndroid Build Coastguard Worker
82*333d2b36SAndroid Build Coastguard Worker	// list of static libraries to re-export include directories from. Entries must be
83*333d2b36SAndroid Build Coastguard Worker	// present in static_libs.
84*333d2b36SAndroid Build Coastguard Worker	Export_static_lib_headers []string `android:"arch_variant"`
85*333d2b36SAndroid Build Coastguard Worker
86*333d2b36SAndroid Build Coastguard Worker	// list of header libraries to re-export include directories from. Entries must be
87*333d2b36SAndroid Build Coastguard Worker	// present in header_libs.
88*333d2b36SAndroid Build Coastguard Worker	Export_header_lib_headers proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
89*333d2b36SAndroid Build Coastguard Worker
90*333d2b36SAndroid Build Coastguard Worker	// list of generated headers to re-export include directories from. Entries must be
91*333d2b36SAndroid Build Coastguard Worker	// present in generated_headers.
92*333d2b36SAndroid Build Coastguard Worker	Export_generated_headers []string `android:"arch_variant"`
93*333d2b36SAndroid Build Coastguard Worker
94*333d2b36SAndroid Build Coastguard Worker	// don't link in crt_begin and crt_end.  This flag should only be necessary for
95*333d2b36SAndroid Build Coastguard Worker	// compiling crt or libc.
96*333d2b36SAndroid Build Coastguard Worker	Nocrt *bool `android:"arch_variant"`
97*333d2b36SAndroid Build Coastguard Worker
98*333d2b36SAndroid Build Coastguard Worker	// don't link in crt_pad_segment. This flag is currently only used internal to
99*333d2b36SAndroid Build Coastguard Worker	// soong for testing and for vndk prebuilt shared libraries.
100*333d2b36SAndroid Build Coastguard Worker	No_crt_pad_segment *bool `android:"arch_variant"`
101*333d2b36SAndroid Build Coastguard Worker
102*333d2b36SAndroid Build Coastguard Worker	// deprecated and ignored because lld makes it unnecessary. See b/189475744.
103*333d2b36SAndroid Build Coastguard Worker	Group_static_libs *bool `android:"arch_variant"`
104*333d2b36SAndroid Build Coastguard Worker
105*333d2b36SAndroid Build Coastguard Worker	// list of modules that should be installed with this module.  This is similar to 'required'
106*333d2b36SAndroid Build Coastguard Worker	// but '.vendor' suffix will be appended to the module names if the shared libraries have
107*333d2b36SAndroid Build Coastguard Worker	// vendor variants and this module uses VNDK.
108*333d2b36SAndroid Build Coastguard Worker	Runtime_libs []string `android:"arch_variant"`
109*333d2b36SAndroid Build Coastguard Worker
110*333d2b36SAndroid Build Coastguard Worker	// list of runtime libs that should not be installed along with this module.
111*333d2b36SAndroid Build Coastguard Worker	Exclude_runtime_libs []string `android:"arch_variant"`
112*333d2b36SAndroid Build Coastguard Worker
113*333d2b36SAndroid Build Coastguard Worker	Target struct {
114*333d2b36SAndroid Build Coastguard Worker		Vendor, Product struct {
115*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that only should be used to build vendor or
116*333d2b36SAndroid Build Coastguard Worker			// product variant of the C/C++ module.
117*333d2b36SAndroid Build Coastguard Worker			Shared_libs []string
118*333d2b36SAndroid Build Coastguard Worker
119*333d2b36SAndroid Build Coastguard Worker			// list of static libs that only should be used to build vendor or
120*333d2b36SAndroid Build Coastguard Worker			// product variant of the C/C++ module.
121*333d2b36SAndroid Build Coastguard Worker			Static_libs []string
122*333d2b36SAndroid Build Coastguard Worker
123*333d2b36SAndroid Build Coastguard Worker			// list of header libs that only should be used to build vendor or product
124*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
125*333d2b36SAndroid Build Coastguard Worker			Header_libs []string
126*333d2b36SAndroid Build Coastguard Worker
127*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build vendor or
128*333d2b36SAndroid Build Coastguard Worker			// product variant of the C/C++ module.
129*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
130*333d2b36SAndroid Build Coastguard Worker
131*333d2b36SAndroid Build Coastguard Worker			// list of static libs that should not be used to build vendor or
132*333d2b36SAndroid Build Coastguard Worker			// product variant of the C/C++ module.
133*333d2b36SAndroid Build Coastguard Worker			Exclude_static_libs []string
134*333d2b36SAndroid Build Coastguard Worker
135*333d2b36SAndroid Build Coastguard Worker			// list of header libs that should not be used to build vendor or
136*333d2b36SAndroid Build Coastguard Worker			// product variant of the C/C++ module.
137*333d2b36SAndroid Build Coastguard Worker			Exclude_header_libs []string
138*333d2b36SAndroid Build Coastguard Worker
139*333d2b36SAndroid Build Coastguard Worker			// list of runtime libs that should not be installed along with the
140*333d2b36SAndroid Build Coastguard Worker			// vendor or product variant of the C/C++ module.
141*333d2b36SAndroid Build Coastguard Worker			Exclude_runtime_libs []string
142*333d2b36SAndroid Build Coastguard Worker
143*333d2b36SAndroid Build Coastguard Worker			// version script for vendor or product variant
144*333d2b36SAndroid Build Coastguard Worker			Version_script *string `android:"arch_variant"`
145*333d2b36SAndroid Build Coastguard Worker		} `android:"arch_variant"`
146*333d2b36SAndroid Build Coastguard Worker		Recovery struct {
147*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that only should be used to build the recovery
148*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
149*333d2b36SAndroid Build Coastguard Worker			Shared_libs []string
150*333d2b36SAndroid Build Coastguard Worker
151*333d2b36SAndroid Build Coastguard Worker			// list of static libs that only should be used to build the recovery
152*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
153*333d2b36SAndroid Build Coastguard Worker			Static_libs []string
154*333d2b36SAndroid Build Coastguard Worker
155*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build
156*333d2b36SAndroid Build Coastguard Worker			// the recovery variant of the C/C++ module.
157*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
158*333d2b36SAndroid Build Coastguard Worker
159*333d2b36SAndroid Build Coastguard Worker			// list of static libs that should not be used to build
160*333d2b36SAndroid Build Coastguard Worker			// the recovery variant of the C/C++ module.
161*333d2b36SAndroid Build Coastguard Worker			Exclude_static_libs []string
162*333d2b36SAndroid Build Coastguard Worker
163*333d2b36SAndroid Build Coastguard Worker			// list of header libs that should not be used to build the recovery variant
164*333d2b36SAndroid Build Coastguard Worker			// of the C/C++ module.
165*333d2b36SAndroid Build Coastguard Worker			Exclude_header_libs []string
166*333d2b36SAndroid Build Coastguard Worker
167*333d2b36SAndroid Build Coastguard Worker			// list of runtime libs that should not be installed along with the
168*333d2b36SAndroid Build Coastguard Worker			// recovery variant of the C/C++ module.
169*333d2b36SAndroid Build Coastguard Worker			Exclude_runtime_libs []string
170*333d2b36SAndroid Build Coastguard Worker		}
171*333d2b36SAndroid Build Coastguard Worker		Ramdisk struct {
172*333d2b36SAndroid Build Coastguard Worker			// list of static libs that only should be used to build the ramdisk
173*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
174*333d2b36SAndroid Build Coastguard Worker			Static_libs []string
175*333d2b36SAndroid Build Coastguard Worker
176*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build
177*333d2b36SAndroid Build Coastguard Worker			// the ramdisk variant of the C/C++ module.
178*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
179*333d2b36SAndroid Build Coastguard Worker
180*333d2b36SAndroid Build Coastguard Worker			// list of static libs that should not be used to build
181*333d2b36SAndroid Build Coastguard Worker			// the ramdisk variant of the C/C++ module.
182*333d2b36SAndroid Build Coastguard Worker			Exclude_static_libs []string
183*333d2b36SAndroid Build Coastguard Worker
184*333d2b36SAndroid Build Coastguard Worker			// list of runtime libs that should not be installed along with the
185*333d2b36SAndroid Build Coastguard Worker			// ramdisk variant of the C/C++ module.
186*333d2b36SAndroid Build Coastguard Worker			Exclude_runtime_libs []string
187*333d2b36SAndroid Build Coastguard Worker		}
188*333d2b36SAndroid Build Coastguard Worker		Vendor_ramdisk struct {
189*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build
190*333d2b36SAndroid Build Coastguard Worker			// the vendor ramdisk variant of the C/C++ module.
191*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
192*333d2b36SAndroid Build Coastguard Worker
193*333d2b36SAndroid Build Coastguard Worker			// list of static libs that should not be used to build
194*333d2b36SAndroid Build Coastguard Worker			// the vendor ramdisk variant of the C/C++ module.
195*333d2b36SAndroid Build Coastguard Worker			Exclude_static_libs []string
196*333d2b36SAndroid Build Coastguard Worker
197*333d2b36SAndroid Build Coastguard Worker			// list of runtime libs that should not be installed along with the
198*333d2b36SAndroid Build Coastguard Worker			// vendor ramdisk variant of the C/C++ module.
199*333d2b36SAndroid Build Coastguard Worker			Exclude_runtime_libs []string
200*333d2b36SAndroid Build Coastguard Worker		}
201*333d2b36SAndroid Build Coastguard Worker		Platform struct {
202*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should be use to build the platform variant
203*333d2b36SAndroid Build Coastguard Worker			// of a module that sets sdk_version.  This should rarely be necessary,
204*333d2b36SAndroid Build Coastguard Worker			// in most cases the same libraries are available for the SDK and platform
205*333d2b36SAndroid Build Coastguard Worker			// variants.
206*333d2b36SAndroid Build Coastguard Worker			Shared_libs []string
207*333d2b36SAndroid Build Coastguard Worker
208*333d2b36SAndroid Build Coastguard Worker			// list of ehader libs that only should be used to build platform variant of
209*333d2b36SAndroid Build Coastguard Worker			// the C/C++ module.
210*333d2b36SAndroid Build Coastguard Worker			Header_libs []string
211*333d2b36SAndroid Build Coastguard Worker
212*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build the platform variant
213*333d2b36SAndroid Build Coastguard Worker			// of the C/C++ module.
214*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
215*333d2b36SAndroid Build Coastguard Worker		}
216*333d2b36SAndroid Build Coastguard Worker		Apex struct {
217*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build the apex variant of
218*333d2b36SAndroid Build Coastguard Worker			// the C/C++ module.
219*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
220*333d2b36SAndroid Build Coastguard Worker
221*333d2b36SAndroid Build Coastguard Worker			// list of static libs that should not be used to build the apex
222*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
223*333d2b36SAndroid Build Coastguard Worker			Exclude_static_libs []string
224*333d2b36SAndroid Build Coastguard Worker		}
225*333d2b36SAndroid Build Coastguard Worker		Non_apex struct {
226*333d2b36SAndroid Build Coastguard Worker			// list of shared libs that should not be used to build the non-apex
227*333d2b36SAndroid Build Coastguard Worker			// variant of the C/C++ module.
228*333d2b36SAndroid Build Coastguard Worker			Exclude_shared_libs []string
229*333d2b36SAndroid Build Coastguard Worker		}
230*333d2b36SAndroid Build Coastguard Worker	} `android:"arch_variant"`
231*333d2b36SAndroid Build Coastguard Worker
232*333d2b36SAndroid Build Coastguard Worker	// make android::build:GetBuildNumber() available containing the build ID.
233*333d2b36SAndroid Build Coastguard Worker	Use_version_lib *bool `android:"arch_variant"`
234*333d2b36SAndroid Build Coastguard Worker
235*333d2b36SAndroid Build Coastguard Worker	// Generate compact dynamic relocation table, default true.
236*333d2b36SAndroid Build Coastguard Worker	Pack_relocations *bool `android:"arch_variant"`
237*333d2b36SAndroid Build Coastguard Worker
238*333d2b36SAndroid Build Coastguard Worker	// local file name to pass to the linker as --version-script.  Not supported on darwin, and will fail to build
239*333d2b36SAndroid Build Coastguard Worker	// if provided to the darwin variant of a module.
240*333d2b36SAndroid Build Coastguard Worker	Version_script *string `android:"path,arch_variant"`
241*333d2b36SAndroid Build Coastguard Worker
242*333d2b36SAndroid Build Coastguard Worker	// local file name to pass to the linker as --dynamic-list.  Not supported on darwin, and will fail to build
243*333d2b36SAndroid Build Coastguard Worker	// if provided to the darwin variant of a module.
244*333d2b36SAndroid Build Coastguard Worker	Dynamic_list *string `android:"path,arch_variant"`
245*333d2b36SAndroid Build Coastguard Worker
246*333d2b36SAndroid Build Coastguard Worker	// local files to pass to the linker as --script.  Not supported on darwin or windows, and will fail to build
247*333d2b36SAndroid Build Coastguard Worker	// if provided to the darwin or windows variant of a module.
248*333d2b36SAndroid Build Coastguard Worker	Linker_scripts []string `android:"path,arch_variant"`
249*333d2b36SAndroid Build Coastguard Worker
250*333d2b36SAndroid Build Coastguard Worker	// list of static libs that should not be used to build this module
251*333d2b36SAndroid Build Coastguard Worker	Exclude_static_libs []string `android:"arch_variant"`
252*333d2b36SAndroid Build Coastguard Worker
253*333d2b36SAndroid Build Coastguard Worker	// list of shared libs that should not be used to build this module
254*333d2b36SAndroid Build Coastguard Worker	Exclude_shared_libs []string `android:"arch_variant"`
255*333d2b36SAndroid Build Coastguard Worker}
256*333d2b36SAndroid Build Coastguard Worker
257*333d2b36SAndroid Build Coastguard Workerfunc (blp *BaseLinkerProperties) crt() bool {
258*333d2b36SAndroid Build Coastguard Worker	// Since crt is enabled for almost every module compiling against the Bionic runtime,
259*333d2b36SAndroid Build Coastguard Worker	// we interpret `nil` as  enabled.
260*333d2b36SAndroid Build Coastguard Worker	return blp.Nocrt == nil || !*blp.Nocrt
261*333d2b36SAndroid Build Coastguard Worker}
262*333d2b36SAndroid Build Coastguard Worker
263*333d2b36SAndroid Build Coastguard Workerfunc (blp *BaseLinkerProperties) libCrt() bool {
264*333d2b36SAndroid Build Coastguard Worker	return blp.No_libcrt == nil || !*blp.No_libcrt
265*333d2b36SAndroid Build Coastguard Worker}
266*333d2b36SAndroid Build Coastguard Worker
267*333d2b36SAndroid Build Coastguard Workerfunc (blp *BaseLinkerProperties) crtPadSegment() bool {
268*333d2b36SAndroid Build Coastguard Worker	return blp.No_crt_pad_segment == nil || !*blp.No_crt_pad_segment
269*333d2b36SAndroid Build Coastguard Worker}
270*333d2b36SAndroid Build Coastguard Worker
271*333d2b36SAndroid Build Coastguard Workerfunc NewBaseLinker(sanitize *sanitize) *baseLinker {
272*333d2b36SAndroid Build Coastguard Worker	return &baseLinker{sanitize: sanitize}
273*333d2b36SAndroid Build Coastguard Worker}
274*333d2b36SAndroid Build Coastguard Worker
275*333d2b36SAndroid Build Coastguard Worker// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
276*333d2b36SAndroid Build Coastguard Workertype baseLinker struct {
277*333d2b36SAndroid Build Coastguard Worker	Properties        BaseLinkerProperties
278*333d2b36SAndroid Build Coastguard Worker	dynamicProperties struct {
279*333d2b36SAndroid Build Coastguard Worker		BuildStubs bool `blueprint:"mutated"`
280*333d2b36SAndroid Build Coastguard Worker	}
281*333d2b36SAndroid Build Coastguard Worker
282*333d2b36SAndroid Build Coastguard Worker	sanitize *sanitize
283*333d2b36SAndroid Build Coastguard Worker}
284*333d2b36SAndroid Build Coastguard Worker
285*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) appendLdflags(flags []string) {
286*333d2b36SAndroid Build Coastguard Worker	linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
287*333d2b36SAndroid Build Coastguard Worker}
288*333d2b36SAndroid Build Coastguard Worker
289*333d2b36SAndroid Build Coastguard Worker// linkerInit initializes dynamic properties of the linker.
290*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
291*333d2b36SAndroid Build Coastguard Worker}
292*333d2b36SAndroid Build Coastguard Worker
293*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) linkerProps() []interface{} {
294*333d2b36SAndroid Build Coastguard Worker	return []interface{}{&linker.Properties, &linker.dynamicProperties}
295*333d2b36SAndroid Build Coastguard Worker}
296*333d2b36SAndroid Build Coastguard Worker
297*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) baseLinkerProps() BaseLinkerProperties {
298*333d2b36SAndroid Build Coastguard Worker	return linker.Properties
299*333d2b36SAndroid Build Coastguard Worker}
300*333d2b36SAndroid Build Coastguard Worker
301*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
302*333d2b36SAndroid Build Coastguard Worker	deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs.GetOrDefault(ctx, nil)...)
303*333d2b36SAndroid Build Coastguard Worker	deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs.GetOrDefault(ctx, nil)...)
304*333d2b36SAndroid Build Coastguard Worker	deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs.GetOrDefault(ctx, nil)...)
305*333d2b36SAndroid Build Coastguard Worker	deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs.GetOrDefault(ctx, nil)...)
306*333d2b36SAndroid Build Coastguard Worker	deps.RuntimeLibs = append(deps.RuntimeLibs, linker.Properties.Runtime_libs...)
307*333d2b36SAndroid Build Coastguard Worker
308*333d2b36SAndroid Build Coastguard Worker	deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers.GetOrDefault(ctx, nil)...)
309*333d2b36SAndroid Build Coastguard Worker	deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
310*333d2b36SAndroid Build Coastguard Worker	deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
311*333d2b36SAndroid Build Coastguard Worker	deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
312*333d2b36SAndroid Build Coastguard Worker
313*333d2b36SAndroid Build Coastguard Worker	deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Exclude_shared_libs)
314*333d2b36SAndroid Build Coastguard Worker	deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Exclude_static_libs)
315*333d2b36SAndroid Build Coastguard Worker	deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Exclude_static_libs)
316*333d2b36SAndroid Build Coastguard Worker	deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Exclude_runtime_libs)
317*333d2b36SAndroid Build Coastguard Worker
318*333d2b36SAndroid Build Coastguard Worker	// Record the libraries that need to be excluded when building for APEX. Unlike other
319*333d2b36SAndroid Build Coastguard Worker	// target.*.exclude_* properties, SharedLibs and StaticLibs are not modified here because
320*333d2b36SAndroid Build Coastguard Worker	// this module hasn't yet passed the apexMutator. Therefore, we can't tell whether this is
321*333d2b36SAndroid Build Coastguard Worker	// an apex variant of not. Record the exclude list in the deps struct for now. The info is
322*333d2b36SAndroid Build Coastguard Worker	// used to mark the dependency tag when adding dependencies to the deps. Then inside
323*333d2b36SAndroid Build Coastguard Worker	// GenerateAndroidBuildActions, the marked dependencies are ignored (i.e. not used) for APEX
324*333d2b36SAndroid Build Coastguard Worker	// variants.
325*333d2b36SAndroid Build Coastguard Worker	deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_shared_libs...)
326*333d2b36SAndroid Build Coastguard Worker	deps.ExcludeLibsForApex = append(deps.ExcludeLibsForApex, linker.Properties.Target.Apex.Exclude_static_libs...)
327*333d2b36SAndroid Build Coastguard Worker	// Record the libraries that need to be excluded when building for non-APEX variants
328*333d2b36SAndroid Build Coastguard Worker	// for the same reason above. This is used for marking deps and marked deps are
329*333d2b36SAndroid Build Coastguard Worker	// ignored for non-apex variants.
330*333d2b36SAndroid Build Coastguard Worker	deps.ExcludeLibsForNonApex = append(deps.ExcludeLibsForNonApex, linker.Properties.Target.Non_apex.Exclude_shared_libs...)
331*333d2b36SAndroid Build Coastguard Worker
332*333d2b36SAndroid Build Coastguard Worker	if Bool(linker.Properties.Use_version_lib) {
333*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = append(deps.WholeStaticLibs, "libbuildversion")
334*333d2b36SAndroid Build Coastguard Worker	}
335*333d2b36SAndroid Build Coastguard Worker
336*333d2b36SAndroid Build Coastguard Worker	if ctx.inVendor() {
337*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Vendor.Shared_libs...)
338*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
339*333d2b36SAndroid Build Coastguard Worker		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
340*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Vendor.Static_libs...)
341*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
342*333d2b36SAndroid Build Coastguard Worker		deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Vendor.Header_libs...)
343*333d2b36SAndroid Build Coastguard Worker		deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Vendor.Exclude_header_libs)
344*333d2b36SAndroid Build Coastguard Worker		deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Vendor.Exclude_header_libs)
345*333d2b36SAndroid Build Coastguard Worker		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor.Exclude_static_libs)
346*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor.Exclude_static_libs)
347*333d2b36SAndroid Build Coastguard Worker		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor.Exclude_runtime_libs)
348*333d2b36SAndroid Build Coastguard Worker	}
349*333d2b36SAndroid Build Coastguard Worker
350*333d2b36SAndroid Build Coastguard Worker	if ctx.inProduct() {
351*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Product.Shared_libs...)
352*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Product.Exclude_shared_libs)
353*333d2b36SAndroid Build Coastguard Worker		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Product.Exclude_shared_libs)
354*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Product.Static_libs...)
355*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
356*333d2b36SAndroid Build Coastguard Worker		deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Product.Exclude_header_libs)
357*333d2b36SAndroid Build Coastguard Worker		deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Product.Exclude_header_libs)
358*333d2b36SAndroid Build Coastguard Worker		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Product.Exclude_static_libs)
359*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Product.Exclude_static_libs)
360*333d2b36SAndroid Build Coastguard Worker		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Product.Exclude_runtime_libs)
361*333d2b36SAndroid Build Coastguard Worker	}
362*333d2b36SAndroid Build Coastguard Worker
363*333d2b36SAndroid Build Coastguard Worker	if ctx.inRecovery() {
364*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Recovery.Shared_libs...)
365*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Recovery.Exclude_shared_libs)
366*333d2b36SAndroid Build Coastguard Worker		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Recovery.Exclude_shared_libs)
367*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Recovery.Static_libs...)
368*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
369*333d2b36SAndroid Build Coastguard Worker		deps.HeaderLibs = removeListFromList(deps.HeaderLibs, linker.Properties.Target.Recovery.Exclude_header_libs)
370*333d2b36SAndroid Build Coastguard Worker		deps.ReexportHeaderLibHeaders = removeListFromList(deps.ReexportHeaderLibHeaders, linker.Properties.Target.Recovery.Exclude_header_libs)
371*333d2b36SAndroid Build Coastguard Worker		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Recovery.Exclude_static_libs)
372*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Recovery.Exclude_static_libs)
373*333d2b36SAndroid Build Coastguard Worker		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Recovery.Exclude_runtime_libs)
374*333d2b36SAndroid Build Coastguard Worker	}
375*333d2b36SAndroid Build Coastguard Worker
376*333d2b36SAndroid Build Coastguard Worker	if ctx.inRamdisk() {
377*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
378*333d2b36SAndroid Build Coastguard Worker		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Ramdisk.Exclude_shared_libs)
379*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Target.Ramdisk.Static_libs...)
380*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
381*333d2b36SAndroid Build Coastguard Worker		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Ramdisk.Exclude_static_libs)
382*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Ramdisk.Exclude_static_libs)
383*333d2b36SAndroid Build Coastguard Worker		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Ramdisk.Exclude_runtime_libs)
384*333d2b36SAndroid Build Coastguard Worker	}
385*333d2b36SAndroid Build Coastguard Worker
386*333d2b36SAndroid Build Coastguard Worker	if ctx.inVendorRamdisk() {
387*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
388*333d2b36SAndroid Build Coastguard Worker		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
389*333d2b36SAndroid Build Coastguard Worker		deps.StaticLibs = removeListFromList(deps.StaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
390*333d2b36SAndroid Build Coastguard Worker		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
391*333d2b36SAndroid Build Coastguard Worker		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
392*333d2b36SAndroid Build Coastguard Worker		deps.RuntimeLibs = removeListFromList(deps.RuntimeLibs, linker.Properties.Target.Vendor_ramdisk.Exclude_runtime_libs)
393*333d2b36SAndroid Build Coastguard Worker	}
394*333d2b36SAndroid Build Coastguard Worker
395*333d2b36SAndroid Build Coastguard Worker	if !ctx.useSdk() {
396*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
397*333d2b36SAndroid Build Coastguard Worker		deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Platform.Exclude_shared_libs)
398*333d2b36SAndroid Build Coastguard Worker		deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Target.Platform.Header_libs...)
399*333d2b36SAndroid Build Coastguard Worker	}
400*333d2b36SAndroid Build Coastguard Worker
401*333d2b36SAndroid Build Coastguard Worker	deps.SystemSharedLibs = linker.Properties.System_shared_libs
402*333d2b36SAndroid Build Coastguard Worker	if deps.SystemSharedLibs == nil {
403*333d2b36SAndroid Build Coastguard Worker		// Provide a default system_shared_libs if it is unspecified. Note: If an
404*333d2b36SAndroid Build Coastguard Worker		// empty list [] is specified, it implies that the module declines the
405*333d2b36SAndroid Build Coastguard Worker		// default system_shared_libs.
406*333d2b36SAndroid Build Coastguard Worker		deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
407*333d2b36SAndroid Build Coastguard Worker	}
408*333d2b36SAndroid Build Coastguard Worker
409*333d2b36SAndroid Build Coastguard Worker	if ctx.toolchain().Bionic() {
410*333d2b36SAndroid Build Coastguard Worker		// libclang_rt.builtins has to be last on the command line
411*333d2b36SAndroid Build Coastguard Worker		if linker.Properties.libCrt() && !ctx.header() {
412*333d2b36SAndroid Build Coastguard Worker			deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary())
413*333d2b36SAndroid Build Coastguard Worker		}
414*333d2b36SAndroid Build Coastguard Worker
415*333d2b36SAndroid Build Coastguard Worker		if inList("libdl", deps.SharedLibs) {
416*333d2b36SAndroid Build Coastguard Worker			// If system_shared_libs has libc but not libdl, make sure shared_libs does not
417*333d2b36SAndroid Build Coastguard Worker			// have libdl to avoid loading libdl before libc.
418*333d2b36SAndroid Build Coastguard Worker			if inList("libc", deps.SystemSharedLibs) {
419*333d2b36SAndroid Build Coastguard Worker				if !inList("libdl", deps.SystemSharedLibs) {
420*333d2b36SAndroid Build Coastguard Worker					ctx.PropertyErrorf("shared_libs",
421*333d2b36SAndroid Build Coastguard Worker						"libdl must be in system_shared_libs, not shared_libs")
422*333d2b36SAndroid Build Coastguard Worker				}
423*333d2b36SAndroid Build Coastguard Worker				_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
424*333d2b36SAndroid Build Coastguard Worker			}
425*333d2b36SAndroid Build Coastguard Worker		}
426*333d2b36SAndroid Build Coastguard Worker
427*333d2b36SAndroid Build Coastguard Worker		// If libc and libdl are both in system_shared_libs make sure libdl comes after libc
428*333d2b36SAndroid Build Coastguard Worker		// to avoid loading libdl before libc.
429*333d2b36SAndroid Build Coastguard Worker		if inList("libdl", deps.SystemSharedLibs) && inList("libc", deps.SystemSharedLibs) &&
430*333d2b36SAndroid Build Coastguard Worker			indexList("libdl", deps.SystemSharedLibs) < indexList("libc", deps.SystemSharedLibs) {
431*333d2b36SAndroid Build Coastguard Worker			ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
432*333d2b36SAndroid Build Coastguard Worker		}
433*333d2b36SAndroid Build Coastguard Worker	} else if ctx.toolchain().Musl() {
434*333d2b36SAndroid Build Coastguard Worker		if linker.Properties.libCrt() && !ctx.header() {
435*333d2b36SAndroid Build Coastguard Worker			deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary())
436*333d2b36SAndroid Build Coastguard Worker		}
437*333d2b36SAndroid Build Coastguard Worker	}
438*333d2b36SAndroid Build Coastguard Worker
439*333d2b36SAndroid Build Coastguard Worker	deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
440*333d2b36SAndroid Build Coastguard Worker
441*333d2b36SAndroid Build Coastguard Worker	if ctx.Windows() && ctx.ModuleName() != "libwinpthread" {
442*333d2b36SAndroid Build Coastguard Worker		deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
443*333d2b36SAndroid Build Coastguard Worker	}
444*333d2b36SAndroid Build Coastguard Worker
445*333d2b36SAndroid Build Coastguard Worker	return deps
446*333d2b36SAndroid Build Coastguard Worker}
447*333d2b36SAndroid Build Coastguard Worker
448*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) useClangLld(ctx ModuleContext) bool {
449*333d2b36SAndroid Build Coastguard Worker	if linker.Properties.Use_clang_lld != nil {
450*333d2b36SAndroid Build Coastguard Worker		return Bool(linker.Properties.Use_clang_lld)
451*333d2b36SAndroid Build Coastguard Worker	}
452*333d2b36SAndroid Build Coastguard Worker	return true
453*333d2b36SAndroid Build Coastguard Worker}
454*333d2b36SAndroid Build Coastguard Worker
455*333d2b36SAndroid Build Coastguard Worker// Check whether the SDK version is not older than the specific one
456*333d2b36SAndroid Build Coastguard Workerfunc CheckSdkVersionAtLeast(ctx ModuleContext, SdkVersion android.ApiLevel) bool {
457*333d2b36SAndroid Build Coastguard Worker	if ctx.minSdkVersion() == "current" {
458*333d2b36SAndroid Build Coastguard Worker		return true
459*333d2b36SAndroid Build Coastguard Worker	}
460*333d2b36SAndroid Build Coastguard Worker	parsedSdkVersion, err := nativeApiLevelFromUser(ctx, ctx.minSdkVersion())
461*333d2b36SAndroid Build Coastguard Worker	if err != nil {
462*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("min_sdk_version",
463*333d2b36SAndroid Build Coastguard Worker			"Invalid min_sdk_version value (must be int or current): %q",
464*333d2b36SAndroid Build Coastguard Worker			ctx.minSdkVersion())
465*333d2b36SAndroid Build Coastguard Worker	}
466*333d2b36SAndroid Build Coastguard Worker	if parsedSdkVersion.LessThan(SdkVersion) {
467*333d2b36SAndroid Build Coastguard Worker		return false
468*333d2b36SAndroid Build Coastguard Worker	}
469*333d2b36SAndroid Build Coastguard Worker	return true
470*333d2b36SAndroid Build Coastguard Worker}
471*333d2b36SAndroid Build Coastguard Worker
472*333d2b36SAndroid Build Coastguard Worker// ModuleContext extends BaseModuleContext
473*333d2b36SAndroid Build Coastguard Worker// BaseModuleContext should know if LLD is used?
474*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
475*333d2b36SAndroid Build Coastguard Worker	toolchain := ctx.toolchain()
476*333d2b36SAndroid Build Coastguard Worker
477*333d2b36SAndroid Build Coastguard Worker	hod := "Host"
478*333d2b36SAndroid Build Coastguard Worker	if ctx.Os().Class == android.Device {
479*333d2b36SAndroid Build Coastguard Worker		hod = "Device"
480*333d2b36SAndroid Build Coastguard Worker	}
481*333d2b36SAndroid Build Coastguard Worker
482*333d2b36SAndroid Build Coastguard Worker	if linker.useClangLld(ctx) {
483*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLldflags}", hod))
484*333d2b36SAndroid Build Coastguard Worker		if !BoolDefault(linker.Properties.Pack_relocations, packRelocationsDefault) {
485*333d2b36SAndroid Build Coastguard Worker			flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=none")
486*333d2b36SAndroid Build Coastguard Worker		} else if ctx.Device() {
487*333d2b36SAndroid Build Coastguard Worker			// SHT_RELR relocations are only supported at API level >= 30.
488*333d2b36SAndroid Build Coastguard Worker			// ANDROID_RELR relocations were supported at API level >= 28.
489*333d2b36SAndroid Build Coastguard Worker			// Relocation packer was supported at API level >= 23.
490*333d2b36SAndroid Build Coastguard Worker			// Do the best we can...
491*333d2b36SAndroid Build Coastguard Worker			if (!ctx.useSdk() && ctx.minSdkVersion() == "") || CheckSdkVersionAtLeast(ctx, android.FirstShtRelrVersion) {
492*333d2b36SAndroid Build Coastguard Worker				flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android+relr")
493*333d2b36SAndroid Build Coastguard Worker			} else if CheckSdkVersionAtLeast(ctx, android.FirstAndroidRelrVersion) {
494*333d2b36SAndroid Build Coastguard Worker				flags.Global.LdFlags = append(flags.Global.LdFlags,
495*333d2b36SAndroid Build Coastguard Worker					"-Wl,--pack-dyn-relocs=android+relr",
496*333d2b36SAndroid Build Coastguard Worker					"-Wl,--use-android-relr-tags")
497*333d2b36SAndroid Build Coastguard Worker			} else if CheckSdkVersionAtLeast(ctx, android.FirstPackedRelocationsVersion) {
498*333d2b36SAndroid Build Coastguard Worker				flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--pack-dyn-relocs=android")
499*333d2b36SAndroid Build Coastguard Worker			}
500*333d2b36SAndroid Build Coastguard Worker		}
501*333d2b36SAndroid Build Coastguard Worker	} else {
502*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, fmt.Sprintf("${config.%sGlobalLdflags}", hod))
503*333d2b36SAndroid Build Coastguard Worker	}
504*333d2b36SAndroid Build Coastguard Worker	if Bool(linker.Properties.Allow_undefined_symbols) {
505*333d2b36SAndroid Build Coastguard Worker		if ctx.Darwin() {
506*333d2b36SAndroid Build Coastguard Worker			// darwin defaults to treating undefined symbols as errors
507*333d2b36SAndroid Build Coastguard Worker			flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-undefined,dynamic_lookup")
508*333d2b36SAndroid Build Coastguard Worker		}
509*333d2b36SAndroid Build Coastguard Worker	} else if !ctx.Darwin() && !ctx.Windows() {
510*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,--no-undefined")
511*333d2b36SAndroid Build Coastguard Worker	}
512*333d2b36SAndroid Build Coastguard Worker
513*333d2b36SAndroid Build Coastguard Worker	if linker.useClangLld(ctx) {
514*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Lldflags())
515*333d2b36SAndroid Build Coastguard Worker	} else {
516*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.Ldflags())
517*333d2b36SAndroid Build Coastguard Worker	}
518*333d2b36SAndroid Build Coastguard Worker
519*333d2b36SAndroid Build Coastguard Worker	if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl {
520*333d2b36SAndroid Build Coastguard Worker		CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
521*333d2b36SAndroid Build Coastguard Worker
522*333d2b36SAndroid Build Coastguard Worker		flags.Local.LdFlags = append(flags.Local.LdFlags, linker.Properties.Host_ldlibs...)
523*333d2b36SAndroid Build Coastguard Worker
524*333d2b36SAndroid Build Coastguard Worker		if !ctx.Windows() {
525*333d2b36SAndroid Build Coastguard Worker			// Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
526*333d2b36SAndroid Build Coastguard Worker			// builds
527*333d2b36SAndroid Build Coastguard Worker			flags.Global.LdFlags = append(flags.Global.LdFlags,
528*333d2b36SAndroid Build Coastguard Worker				"-ldl",
529*333d2b36SAndroid Build Coastguard Worker				"-lpthread",
530*333d2b36SAndroid Build Coastguard Worker				"-lm",
531*333d2b36SAndroid Build Coastguard Worker			)
532*333d2b36SAndroid Build Coastguard Worker			if !ctx.Darwin() {
533*333d2b36SAndroid Build Coastguard Worker				flags.Global.LdFlags = append(flags.Global.LdFlags, "-lrt")
534*333d2b36SAndroid Build Coastguard Worker			}
535*333d2b36SAndroid Build Coastguard Worker		}
536*333d2b36SAndroid Build Coastguard Worker	}
537*333d2b36SAndroid Build Coastguard Worker
538*333d2b36SAndroid Build Coastguard Worker	CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
539*333d2b36SAndroid Build Coastguard Worker
540*333d2b36SAndroid Build Coastguard Worker	flags.Local.LdFlags = append(flags.Local.LdFlags, proptools.NinjaAndShellEscapeList(linker.Properties.Ldflags)...)
541*333d2b36SAndroid Build Coastguard Worker
542*333d2b36SAndroid Build Coastguard Worker	if ctx.Host() && !ctx.Windows() && !ctx.static() {
543*333d2b36SAndroid Build Coastguard Worker		flags.Global.LdFlags = append(flags.Global.LdFlags, RpathFlags(ctx)...)
544*333d2b36SAndroid Build Coastguard Worker	}
545*333d2b36SAndroid Build Coastguard Worker
546*333d2b36SAndroid Build Coastguard Worker	flags.Global.LdFlags = append(flags.Global.LdFlags, toolchain.ToolchainLdflags())
547*333d2b36SAndroid Build Coastguard Worker
548*333d2b36SAndroid Build Coastguard Worker	// Version_script is not needed when linking stubs lib where the version
549*333d2b36SAndroid Build Coastguard Worker	// script is created from the symbol map file.
550*333d2b36SAndroid Build Coastguard Worker	if !linker.dynamicProperties.BuildStubs {
551*333d2b36SAndroid Build Coastguard Worker		versionScript := ctx.ExpandOptionalSource(
552*333d2b36SAndroid Build Coastguard Worker			linker.Properties.Version_script, "version_script")
553*333d2b36SAndroid Build Coastguard Worker
554*333d2b36SAndroid Build Coastguard Worker		if ctx.inVendor() && linker.Properties.Target.Vendor.Version_script != nil {
555*333d2b36SAndroid Build Coastguard Worker			versionScript = ctx.ExpandOptionalSource(
556*333d2b36SAndroid Build Coastguard Worker				linker.Properties.Target.Vendor.Version_script,
557*333d2b36SAndroid Build Coastguard Worker				"target.vendor.version_script")
558*333d2b36SAndroid Build Coastguard Worker		} else if ctx.inProduct() && linker.Properties.Target.Product.Version_script != nil {
559*333d2b36SAndroid Build Coastguard Worker			versionScript = ctx.ExpandOptionalSource(
560*333d2b36SAndroid Build Coastguard Worker				linker.Properties.Target.Product.Version_script,
561*333d2b36SAndroid Build Coastguard Worker				"target.product.version_script")
562*333d2b36SAndroid Build Coastguard Worker		}
563*333d2b36SAndroid Build Coastguard Worker
564*333d2b36SAndroid Build Coastguard Worker		if versionScript.Valid() {
565*333d2b36SAndroid Build Coastguard Worker			if ctx.Darwin() {
566*333d2b36SAndroid Build Coastguard Worker				ctx.AddMissingDependencies([]string{"version_script_not_supported_on_darwin"})
567*333d2b36SAndroid Build Coastguard Worker			} else {
568*333d2b36SAndroid Build Coastguard Worker				flags.Local.LdFlags = append(flags.Local.LdFlags,
569*333d2b36SAndroid Build Coastguard Worker					config.VersionScriptFlagPrefix+versionScript.String())
570*333d2b36SAndroid Build Coastguard Worker				flags.LdFlagsDeps = append(flags.LdFlagsDeps, versionScript.Path())
571*333d2b36SAndroid Build Coastguard Worker
572*333d2b36SAndroid Build Coastguard Worker				if linker.sanitize.isSanitizerEnabled(cfi) {
573*333d2b36SAndroid Build Coastguard Worker					cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath+"/"+cfiExportsMapFilename)
574*333d2b36SAndroid Build Coastguard Worker					flags.Local.LdFlags = append(flags.Local.LdFlags,
575*333d2b36SAndroid Build Coastguard Worker						config.VersionScriptFlagPrefix+cfiExportsMap.String())
576*333d2b36SAndroid Build Coastguard Worker					flags.LdFlagsDeps = append(flags.LdFlagsDeps, cfiExportsMap)
577*333d2b36SAndroid Build Coastguard Worker				}
578*333d2b36SAndroid Build Coastguard Worker			}
579*333d2b36SAndroid Build Coastguard Worker		}
580*333d2b36SAndroid Build Coastguard Worker
581*333d2b36SAndroid Build Coastguard Worker		dynamicList := android.OptionalPathForModuleSrc(ctx, linker.Properties.Dynamic_list)
582*333d2b36SAndroid Build Coastguard Worker		if dynamicList.Valid() {
583*333d2b36SAndroid Build Coastguard Worker			if ctx.Darwin() {
584*333d2b36SAndroid Build Coastguard Worker				ctx.AddMissingDependencies([]string{"dynamic_list_not_supported_on_darwin"})
585*333d2b36SAndroid Build Coastguard Worker			} else {
586*333d2b36SAndroid Build Coastguard Worker				flags.Local.LdFlags = append(flags.Local.LdFlags,
587*333d2b36SAndroid Build Coastguard Worker					"-Wl,--dynamic-list,"+dynamicList.String())
588*333d2b36SAndroid Build Coastguard Worker				flags.LdFlagsDeps = append(flags.LdFlagsDeps, dynamicList.Path())
589*333d2b36SAndroid Build Coastguard Worker			}
590*333d2b36SAndroid Build Coastguard Worker		}
591*333d2b36SAndroid Build Coastguard Worker
592*333d2b36SAndroid Build Coastguard Worker		linkerScriptPaths := android.PathsForModuleSrc(ctx, linker.Properties.Linker_scripts)
593*333d2b36SAndroid Build Coastguard Worker		if len(linkerScriptPaths) > 0 {
594*333d2b36SAndroid Build Coastguard Worker			if ctx.Darwin() {
595*333d2b36SAndroid Build Coastguard Worker				ctx.AddMissingDependencies([]string{"linker_scripts_not_supported_on_darwin"})
596*333d2b36SAndroid Build Coastguard Worker			} else if ctx.Windows() {
597*333d2b36SAndroid Build Coastguard Worker				ctx.PropertyErrorf("linker_scripts", "Only supported for ELF files")
598*333d2b36SAndroid Build Coastguard Worker			} else {
599*333d2b36SAndroid Build Coastguard Worker				for _, linkerScriptPath := range linkerScriptPaths {
600*333d2b36SAndroid Build Coastguard Worker					flags.Local.LdFlags = append(flags.Local.LdFlags,
601*333d2b36SAndroid Build Coastguard Worker						"-Wl,--script,"+linkerScriptPath.String())
602*333d2b36SAndroid Build Coastguard Worker					flags.LdFlagsDeps = append(flags.LdFlagsDeps, linkerScriptPath)
603*333d2b36SAndroid Build Coastguard Worker				}
604*333d2b36SAndroid Build Coastguard Worker			}
605*333d2b36SAndroid Build Coastguard Worker		}
606*333d2b36SAndroid Build Coastguard Worker	}
607*333d2b36SAndroid Build Coastguard Worker
608*333d2b36SAndroid Build Coastguard Worker	return flags
609*333d2b36SAndroid Build Coastguard Worker}
610*333d2b36SAndroid Build Coastguard Worker
611*333d2b36SAndroid Build Coastguard Worker// RpathFlags returns the rpath linker flags for current target to search the following directories relative
612*333d2b36SAndroid Build Coastguard Worker// to the binary:
613*333d2b36SAndroid Build Coastguard Worker//
614*333d2b36SAndroid Build Coastguard Worker//   - "." to find libraries alongside tests
615*333d2b36SAndroid Build Coastguard Worker//   - "lib[64]" to find libraries in a subdirectory of the binaries' directory
616*333d2b36SAndroid Build Coastguard Worker//   - "../lib[64]" to find libraries when the binaries are in a bin directory
617*333d2b36SAndroid Build Coastguard Worker//   - "../../lib[64]" to find libraries in out/host/linux-x86/lib64 when the test or binary is in
618*333d2b36SAndroid Build Coastguard Worker//     out/host/linux-x86/nativetest/<test dir>/<test>
619*333d2b36SAndroid Build Coastguard Worker//   - "../../../lib[[64] to find libraries in out/host/linux-x86/lib64 when the test or binary is in
620*333d2b36SAndroid Build Coastguard Worker//     out/host/linux-x86/testcases/<test dir>/<CPU>/<test>
621*333d2b36SAndroid Build Coastguard Workerfunc RpathFlags(ctx android.ModuleContext) []string {
622*333d2b36SAndroid Build Coastguard Worker	key := struct {
623*333d2b36SAndroid Build Coastguard Worker		os   android.OsType
624*333d2b36SAndroid Build Coastguard Worker		arch android.ArchType
625*333d2b36SAndroid Build Coastguard Worker	}{ctx.Target().Os, ctx.Target().Arch.ArchType}
626*333d2b36SAndroid Build Coastguard Worker
627*333d2b36SAndroid Build Coastguard Worker	return ctx.Config().OnceStringSlice(android.NewCustomOnceKey(key), func() []string {
628*333d2b36SAndroid Build Coastguard Worker		rpathPrefix := `\$$ORIGIN/`
629*333d2b36SAndroid Build Coastguard Worker		if key.os == android.Darwin {
630*333d2b36SAndroid Build Coastguard Worker			rpathPrefix = "@loader_path/"
631*333d2b36SAndroid Build Coastguard Worker		}
632*333d2b36SAndroid Build Coastguard Worker
633*333d2b36SAndroid Build Coastguard Worker		var libDir string
634*333d2b36SAndroid Build Coastguard Worker		if key.arch.Multilib == "lib64" {
635*333d2b36SAndroid Build Coastguard Worker			libDir = "lib64"
636*333d2b36SAndroid Build Coastguard Worker		} else {
637*333d2b36SAndroid Build Coastguard Worker			libDir = "lib"
638*333d2b36SAndroid Build Coastguard Worker		}
639*333d2b36SAndroid Build Coastguard Worker
640*333d2b36SAndroid Build Coastguard Worker		return []string{
641*333d2b36SAndroid Build Coastguard Worker			"-Wl,-rpath," + rpathPrefix,
642*333d2b36SAndroid Build Coastguard Worker			"-Wl,-rpath," + rpathPrefix + libDir,
643*333d2b36SAndroid Build Coastguard Worker			"-Wl,-rpath," + rpathPrefix + filepath.Join("..", libDir),
644*333d2b36SAndroid Build Coastguard Worker			"-Wl,-rpath," + rpathPrefix + filepath.Join("../..", libDir),
645*333d2b36SAndroid Build Coastguard Worker			"-Wl,-rpath," + rpathPrefix + filepath.Join("../../..", libDir),
646*333d2b36SAndroid Build Coastguard Worker		}
647*333d2b36SAndroid Build Coastguard Worker	})
648*333d2b36SAndroid Build Coastguard Worker}
649*333d2b36SAndroid Build Coastguard Worker
650*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) link(ctx ModuleContext,
651*333d2b36SAndroid Build Coastguard Worker	flags Flags, deps PathDeps, objs Objects) android.Path {
652*333d2b36SAndroid Build Coastguard Worker	panic(fmt.Errorf("baseLinker doesn't know how to link"))
653*333d2b36SAndroid Build Coastguard Worker}
654*333d2b36SAndroid Build Coastguard Worker
655*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) linkerSpecifiedDeps(ctx android.ConfigurableEvaluatorContext, module *Module, specifiedDeps specifiedDeps) specifiedDeps {
656*333d2b36SAndroid Build Coastguard Worker	eval := module.ConfigurableEvaluator(ctx)
657*333d2b36SAndroid Build Coastguard Worker	specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, linker.Properties.Shared_libs.GetOrDefault(eval, nil)...)
658*333d2b36SAndroid Build Coastguard Worker
659*333d2b36SAndroid Build Coastguard Worker	// Must distinguish nil and [] in system_shared_libs - ensure that [] in
660*333d2b36SAndroid Build Coastguard Worker	// either input list doesn't come out as nil.
661*333d2b36SAndroid Build Coastguard Worker	if specifiedDeps.systemSharedLibs == nil {
662*333d2b36SAndroid Build Coastguard Worker		specifiedDeps.systemSharedLibs = linker.Properties.System_shared_libs
663*333d2b36SAndroid Build Coastguard Worker	} else {
664*333d2b36SAndroid Build Coastguard Worker		specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
665*333d2b36SAndroid Build Coastguard Worker	}
666*333d2b36SAndroid Build Coastguard Worker
667*333d2b36SAndroid Build Coastguard Worker	return specifiedDeps
668*333d2b36SAndroid Build Coastguard Worker}
669*333d2b36SAndroid Build Coastguard Worker
670*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
671*333d2b36SAndroid Build Coastguard Worker}
672*333d2b36SAndroid Build Coastguard Worker
673*333d2b36SAndroid Build Coastguard Worker// Injecting version symbols
674*333d2b36SAndroid Build Coastguard Worker// Some host modules want a version number, but we don't want to rebuild it every time.  Optionally add a step
675*333d2b36SAndroid Build Coastguard Worker// after linking that injects a constant placeholder with the current version number.
676*333d2b36SAndroid Build Coastguard Worker
677*333d2b36SAndroid Build Coastguard Workerfunc init() {
678*333d2b36SAndroid Build Coastguard Worker	pctx.HostBinToolVariable("symbolInjectCmd", "symbol_inject")
679*333d2b36SAndroid Build Coastguard Worker}
680*333d2b36SAndroid Build Coastguard Worker
681*333d2b36SAndroid Build Coastguard Workervar injectVersionSymbol = pctx.AndroidStaticRule("injectVersionSymbol",
682*333d2b36SAndroid Build Coastguard Worker	blueprint.RuleParams{
683*333d2b36SAndroid Build Coastguard Worker		Command: "$symbolInjectCmd -i $in -o $out -s soong_build_number " +
684*333d2b36SAndroid Build Coastguard Worker			"-from 'SOONG BUILD NUMBER PLACEHOLDER' -v $$(cat $buildNumberFile)",
685*333d2b36SAndroid Build Coastguard Worker		CommandDeps: []string{"$symbolInjectCmd"},
686*333d2b36SAndroid Build Coastguard Worker	},
687*333d2b36SAndroid Build Coastguard Worker	"buildNumberFile")
688*333d2b36SAndroid Build Coastguard Worker
689*333d2b36SAndroid Build Coastguard Workerfunc (linker *baseLinker) injectVersionSymbol(ctx ModuleContext, in android.Path, out android.WritablePath) {
690*333d2b36SAndroid Build Coastguard Worker	buildNumberFile := ctx.Config().BuildNumberFile(ctx)
691*333d2b36SAndroid Build Coastguard Worker	ctx.Build(pctx, android.BuildParams{
692*333d2b36SAndroid Build Coastguard Worker		Rule:        injectVersionSymbol,
693*333d2b36SAndroid Build Coastguard Worker		Description: "inject version symbol",
694*333d2b36SAndroid Build Coastguard Worker		Input:       in,
695*333d2b36SAndroid Build Coastguard Worker		Output:      out,
696*333d2b36SAndroid Build Coastguard Worker		OrderOnly:   android.Paths{buildNumberFile},
697*333d2b36SAndroid Build Coastguard Worker		Args: map[string]string{
698*333d2b36SAndroid Build Coastguard Worker			"buildNumberFile": buildNumberFile.String(),
699*333d2b36SAndroid Build Coastguard Worker		},
700*333d2b36SAndroid Build Coastguard Worker	})
701*333d2b36SAndroid Build Coastguard Worker}
702