blob: 63d4daee836d00e9efcc53df381def7449527c9c [file] [log] [blame]
Ryan Prichard80e40f02019-10-31 19:54:46 -07001// ========================================================
2// linker_wrapper - Linux Bionic (on the host)
3// ========================================================
Colin Cross97f0aef2016-07-14 16:05:46 -07004
Dan Willemsen7ccc50d2017-09-18 21:28:14 -07005// This is used for bionic on (host) Linux to bootstrap our linker embedded into
6// a binary.
7//
8// Host bionic binaries do not have a PT_INTERP section, instead this gets
9// embedded as the entry point, and the linker is embedded as ELF sections in
10// each binary. There's a linker script that sets all of that up (generated by
11// extract_linker), and defines the extern symbols used in this file.
Bob Badouraa7d8352021-02-19 13:06:22 -080012package {
13 default_applicable_licenses: ["bionic_linker_license"],
14}
15
16license {
17 name: "bionic_linker_license",
18 visibility: [":__subpackages__"],
19 license_kinds: [
20 "SPDX-license-identifier-BSD",
21 ],
22 license_text: [
23 "NOTICE",
24 ],
25}
26
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070027cc_object {
28 name: "linker_wrapper",
29 host_supported: true,
30 device_supported: false,
Colin Crossda446cc2022-03-08 15:07:57 -080031 enabled: false,
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070032 target: {
Elliott Hughesd50a1de2018-02-05 17:30:57 -080033 linux_bionic: {
34 enabled: true,
35 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070036 },
37
38 cflags: [
39 "-fno-stack-protector",
40 "-Wstrict-overflow=5",
41 "-fvisibility=hidden",
42 "-Wall",
43 "-Wextra",
44 "-Wno-unused",
45 "-Werror",
46 ],
47
48 srcs: [
49 "linker_wrapper.cpp",
50 ],
51 arch: {
Jiyong Park3b47d602020-09-18 16:15:53 +090052 arm64: {
Colin Crossa0a591a2021-06-11 12:46:45 -070053 srcs: ["arch/arm64/linker_wrapper_begin.S"],
Jiyong Park3b47d602020-09-18 16:15:53 +090054 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070055 x86_64: {
Colin Crossa0a591a2021-06-11 12:46:45 -070056 srcs: ["arch/x86_64/linker_wrapper_begin.S"],
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070057 },
58 },
59
Martin Stjernholm82d84bc2020-04-06 20:32:09 +010060 header_libs: ["libc_headers"],
61
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070062 // We need to access Bionic private headers in the linker.
63 include_dirs: ["bionic/libc"],
64}
65
Ryan Prichard80e40f02019-10-31 19:54:46 -070066// ========================================================
67// linker default configuration
68// ========================================================
69
70// Configuration for the linker binary and any of its static libraries.
71cc_defaults {
72 name: "linker_defaults",
Christopher R. Palmerac3ede62017-04-08 22:40:01 +020073 defaults: [
74 "shim_libs_defaults",
75 ],
Ryan Prichard80e40f02019-10-31 19:54:46 -070076 arch: {
77 arm: {
78 cflags: ["-D__work_around_b_24465209__"],
79 },
80 x86: {
81 cflags: ["-D__work_around_b_24465209__"],
82 },
83 },
84
85 cflags: [
86 "-fno-stack-protector",
87 "-Wstrict-overflow=5",
88 "-fvisibility=hidden",
89 "-Wall",
90 "-Wextra",
91 "-Wunused",
92 "-Werror",
93 ],
94
95 // TODO: split out the asflags.
96 asflags: [
97 "-fno-stack-protector",
98 "-Wstrict-overflow=5",
99 "-fvisibility=hidden",
100 "-Wall",
101 "-Wextra",
102 "-Wunused",
103 "-Werror",
104 ],
105
106 product_variables: {
107 debuggable: {
108 cppflags: ["-DUSE_LD_CONFIG_FILE"],
109 },
110 },
111
112 cppflags: ["-Wold-style-cast"],
113
114 static_libs: [
115 "libziparchive",
116 "libbase",
117 "libz",
118
119 "libasync_safe",
120
121 "liblog",
122 ],
123
124 // We need to access Bionic private headers in the linker.
125 include_dirs: ["bionic/libc"],
126}
127
128// ========================================================
129// linker components
130// ========================================================
131
132// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
133// native bridge implementations).
134cc_defaults {
135 name: "linker_all_targets",
136 defaults: ["linux_bionic_supported"],
137 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700138 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700139 native_bridge_supported: true,
140}
141
142cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700143 name: "liblinker_main",
144 defaults: ["linker_defaults", "linker_all_targets"],
145 srcs: ["linker_main.cpp"],
146
147 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
148 cflags: ["-ffreestanding"],
149}
150
151cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700152 name: "liblinker_malloc",
153 defaults: ["linker_defaults", "linker_all_targets"],
154 srcs: ["linker_memory.cpp"],
155}
156
157cc_library_static {
158 name: "liblinker_debuggerd_stub",
159 defaults: ["linker_defaults", "linker_all_targets"],
160 srcs: ["linker_debuggerd_stub.cpp"],
161}
162
163// ========================================================
164// template for the linker binary
165// ========================================================
166
dimitryb8b3a762018-09-25 12:31:11 +0200167filegroup {
168 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700169 srcs: [
170 "dlfcn.cpp",
171 "linker.cpp",
172 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700173 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700174 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800175 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800176 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700177 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700178 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700179 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800180 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700181 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700182 "linker_logger.cpp",
183 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100184 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700185 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800186 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700187 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700188 "linker_soinfo.cpp",
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -0700189 "linker_transparent_hugepage_support.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800190 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700191 "linker_utils.cpp",
192 "rt.cpp",
193 ],
dimitryb8b3a762018-09-25 12:31:11 +0200194}
Colin Cross97f0aef2016-07-14 16:05:46 -0700195
dimitryb8b3a762018-09-25 12:31:11 +0200196filegroup {
197 name: "linker_sources_arm",
198 srcs: [
199 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800200 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200201 ],
202}
203
204filegroup {
205 name: "linker_sources_arm64",
206 srcs: [
207 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800208 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800209 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200210 ],
211}
212
213filegroup {
214 name: "linker_sources_x86",
215 srcs: [
216 "arch/x86/begin.S",
217 ],
218}
219
220filegroup {
221 name: "linker_sources_x86_64",
222 srcs: [
223 "arch/x86_64/begin.S",
224 ],
225}
226
dimitryb8b3a762018-09-25 12:31:11 +0200227cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700228 name: "linker_version_script_overlay",
229 arch: {
230 arm: { version_script: "linker.arm.map" },
231 arm64: { version_script: "linker.generic.map" },
232 x86: { version_script: "linker.generic.map" },
233 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700234 },
235}
236
237// A template for the linker binary. May be inherited by native bridge implementations.
238cc_defaults {
239 name: "linker_bin_template",
240 defaults: ["linker_defaults"],
241
242 srcs: [":linker_sources"],
243
Colin Cross97f0aef2016-07-14 16:05:46 -0700244 arch: {
245 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700246 srcs: [":linker_sources_arm"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700247 },
248 arm64: {
249 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700250 },
251 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700252 srcs: [":linker_sources_x86"],
253 },
254 x86_64: {
255 srcs: [":linker_sources_x86_64"],
256 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700257 },
258
Ryan Prichard80e40f02019-10-31 19:54:46 -0700259 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
260 // static_executable. This dynamic linker is actually a shared object linked with static
261 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700262 ldflags: [
263 "-shared",
264 "-Wl,-Bsymbolic",
265 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100266 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700267 ],
268
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800269 // we are going to link libc++_static manually because
270 // when stl is not set to "none" build system adds libdl
271 // to the list of static libraries which needs to be
272 // avoided in the case of building loader.
273 stl: "none",
274
Colin Cross97f0aef2016-07-14 16:05:46 -0700275 // we don't want crtbegin.o (because we have begin.o), so unset it
276 // just for this module
277 nocrt: true,
278
dimitryb8b3a762018-09-25 12:31:11 +0200279 static_executable: true,
280
281 // Leave the symbols in the shared library so that stack unwinders can produce
282 // meaningful name resolution.
283 strip: {
284 keep_symbols: true,
285 },
286
287 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
288 // looking up symbols in the linker by mistake.
289 prefix_symbols: "__dl_",
290
291 sanitize: {
292 hwaddress: false,
293 },
dimitryb8b3a762018-09-25 12:31:11 +0200294
Colin Cross97f0aef2016-07-14 16:05:46 -0700295 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700296 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700297 "liblinker_malloc",
298
299 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700300 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700301 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800302 "libm",
Ryan Prichardcdf71752020-12-16 03:37:22 -0800303 "libunwind",
Colin Cross97f0aef2016-07-14 16:05:46 -0700304 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700305
Ryan Prichardd9a41152019-10-14 16:58:53 -0700306 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
307 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
308 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
309 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
310 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
311 //
312 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
313 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
314 // linker stops linking against libgcc.a's arm32 unwinder.
315 whole_static_libs: ["libc_unwind_static"],
316
Ryan Prichard80e40f02019-10-31 19:54:46 -0700317 system_shared_libs: [],
318
319 // Opt out of native_coverage when opting out of system_shared_libs
320 native_coverage: false,
321}
322
323// ========================================================
324// linker[_asan][64] binary
325// ========================================================
326
327cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700328 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700329 defaults: [
330 "linker_bin_template",
331 "linux_bionic_supported",
332 "linker_version_script_overlay",
333 ],
334
Victor Khimenkod15229d2020-05-14 22:14:45 +0200335 srcs: [
336 "linker_translate_path.cpp",
337 ],
338
Colin Cross10fffb42016-12-08 09:57:35 -0800339 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700340 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700341 lib64: {
342 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800343 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700344 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800345
Ryan Prichard80e40f02019-10-31 19:54:46 -0700346 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700347
348 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700349 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700350 apex_available: [
351 "//apex_available:platform",
352 "com.android.runtime",
353 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800354
Colin Cross97f0aef2016-07-14 16:05:46 -0700355 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800356 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700357 srcs: [
358 "linker_debuggerd_android.cpp",
359 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700360 static_libs: [
361 "libc++demangle",
362 "libdebuggerd_handler_fallback",
363 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800364 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700365 linux_bionic: {
366 static_libs: [
367 "liblinker_debuggerd_stub",
368 ],
Yi Kong6f6daaa2020-12-10 01:27:44 +0800369 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700370 },
Yi Konga7e363f2020-10-01 04:10:01 +0800371
Yi Konge20a1d92022-01-25 03:19:58 +0800372 afdo: true,
Colin Cross97f0aef2016-07-14 16:05:46 -0700373}
dimitry11da1dc2018-01-05 13:35:43 +0100374
Ryan Prichard80e40f02019-10-31 19:54:46 -0700375// ========================================================
376// assorted modules
377// ========================================================
378
Elliott Hughes90f96b92019-05-09 15:56:39 -0700379sh_binary {
380 name: "ldd",
Rupert Shuttlewortha7e29a82021-02-18 13:35:22 +0000381 src: "ldd.sh",
Elliott Hughes90f96b92019-05-09 15:56:39 -0700382}
383
Collin Fijalkovichc9521e02021-04-29 11:31:50 -0700384// Used to generate binaries that can be backed by transparent hugepages.
385cc_defaults {
386 name: "linker_hugepage_aligned",
387 arch: {
388 arm64: {
389 ldflags: ["-z max-page-size=0x200000"],
390 },
391 x86_64: {
392 ldflags: ["-z max-page-size=0x200000"],
393 },
394 },
395}
396
dimitry11da1dc2018-01-05 13:35:43 +0100397cc_library {
398 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
399 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
400 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
401 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
402 // we use this property to make sure libc.so has its own copy of the code from
403 // libgcc.a it uses.
404 //
405 // DO NOT REMOVE --exclude-libs!
406
Yi Kong7786a342018-08-31 19:01:56 -0700407 ldflags: [
408 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700409 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700410 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
411 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
Ryan Prichardef147872021-01-28 14:06:52 -0800412 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
Yi Kong7786a342018-08-31 19:01:56 -0700413 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
414 ],
dimitry11da1dc2018-01-05 13:35:43 +0100415
416 // for x86, exclude libgcc_eh.a for the same reasons as above
417 arch: {
418 x86: {
419 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
420 },
421 x86_64: {
422 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
423 },
424 },
dimitry581723e2018-01-05 14:31:44 +0100425
426 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100427 cflags: [
428 "-Wall",
429 "-Wextra",
430 "-Wunused",
431 "-Werror",
432 ],
433 stl: "none",
434
435 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700436 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800437 ramdisk_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700438 vendor_ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900439 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200440 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100441
Ryan Prichard470b6662018-03-27 22:10:55 -0700442 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100443 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100444 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100445
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800446 // Opt out of native_coverage when opting out of system_shared_libs
447 native_coverage: false,
448
dimitry11da1dc2018-01-05 13:35:43 +0100449 sanitize: {
450 never: true,
451 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900452
453 apex_available: [
454 "//apex_available:platform",
455 "com.android.runtime",
456 ],
Yi Kong15a05a72020-09-22 00:56:13 +0800457
458 lto: {
459 never: true,
460 },
dimitry11da1dc2018-01-05 13:35:43 +0100461}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800462
463cc_test {
464 name: "linker-unit-tests",
Colin Cross0cc60af2021-09-30 14:04:39 -0700465 test_suites: ["device-tests"],
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800466
467 cflags: [
468 "-g",
469 "-Wall",
470 "-Wextra",
471 "-Wunused",
472 "-Werror",
473 ],
474
475 // We need to access Bionic private headers in the linker.
476 include_dirs: ["bionic/libc"],
477
478 srcs: [
479 // Tests.
480 "linker_block_allocator_test.cpp",
481 "linker_config_test.cpp",
482 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100483 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800484 "linker_sleb128_test.cpp",
485 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800486 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800487
488 // Parts of the linker that we're testing.
489 "linker_block_allocator.cpp",
490 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800491 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100492 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800493 "linker_test_globals.cpp",
494 "linker_utils.cpp",
495 ],
496
497 static_libs: [
498 "libasync_safe",
499 "libbase",
500 "liblog",
501 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800502
503 arch: {
504 arm: {
505 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
506 },
507 arm64: {
508 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
509 },
510 },
511}
512
513cc_benchmark {
514 name: "linker-benchmarks",
515
516 srcs: [
517 "linker_gnu_hash_benchmark.cpp",
518 ],
519
520 arch: {
521 arm: {
522 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
523 },
524 arm64: {
525 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
526 },
527 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800528}