blob: dd376da86890b8f3144f753a10ae6e3480af641b [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,
31 target: {
Elliott Hughesd50a1de2018-02-05 17:30:57 -080032 linux_bionic: {
33 enabled: true,
34 },
35 linux_glibc: {
36 enabled: false,
37 },
38 darwin: {
39 enabled: false,
40 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070041 },
42
43 cflags: [
44 "-fno-stack-protector",
45 "-Wstrict-overflow=5",
46 "-fvisibility=hidden",
47 "-Wall",
48 "-Wextra",
49 "-Wno-unused",
50 "-Werror",
51 ],
52
53 srcs: [
54 "linker_wrapper.cpp",
55 ],
56 arch: {
Jiyong Park3b47d602020-09-18 16:15:53 +090057 arm64: {
58 srcs: ["arch/arm64/begin.S"],
59 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070060 x86_64: {
61 srcs: ["arch/x86_64/begin.S"],
62 },
63 },
64
65 prefix_symbols: "__dlwrap_",
66
Martin Stjernholm82d84bc2020-04-06 20:32:09 +010067 header_libs: ["libc_headers"],
68
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070069 // We need to access Bionic private headers in the linker.
70 include_dirs: ["bionic/libc"],
71}
72
Ryan Prichard80e40f02019-10-31 19:54:46 -070073// ========================================================
74// linker default configuration
75// ========================================================
76
77// Configuration for the linker binary and any of its static libraries.
78cc_defaults {
79 name: "linker_defaults",
80 arch: {
81 arm: {
82 cflags: ["-D__work_around_b_24465209__"],
83 },
84 x86: {
85 cflags: ["-D__work_around_b_24465209__"],
86 },
87 },
88
89 cflags: [
90 "-fno-stack-protector",
91 "-Wstrict-overflow=5",
92 "-fvisibility=hidden",
93 "-Wall",
94 "-Wextra",
95 "-Wunused",
96 "-Werror",
97 ],
98
99 // TODO: split out the asflags.
100 asflags: [
101 "-fno-stack-protector",
102 "-Wstrict-overflow=5",
103 "-fvisibility=hidden",
104 "-Wall",
105 "-Wextra",
106 "-Wunused",
107 "-Werror",
108 ],
109
110 product_variables: {
111 debuggable: {
112 cppflags: ["-DUSE_LD_CONFIG_FILE"],
113 },
114 },
115
116 cppflags: ["-Wold-style-cast"],
117
118 static_libs: [
119 "libziparchive",
120 "libbase",
121 "libz",
122
123 "libasync_safe",
124
125 "liblog",
126 ],
127
128 // We need to access Bionic private headers in the linker.
129 include_dirs: ["bionic/libc"],
130}
131
132// ========================================================
133// linker components
134// ========================================================
135
136// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
137// native bridge implementations).
138cc_defaults {
139 name: "linker_all_targets",
140 defaults: ["linux_bionic_supported"],
141 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700142 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700143 native_bridge_supported: true,
144}
145
146cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700147 name: "liblinker_main",
148 defaults: ["linker_defaults", "linker_all_targets"],
149 srcs: ["linker_main.cpp"],
150
151 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
152 cflags: ["-ffreestanding"],
153}
154
155cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700156 name: "liblinker_malloc",
157 defaults: ["linker_defaults", "linker_all_targets"],
158 srcs: ["linker_memory.cpp"],
159}
160
161cc_library_static {
162 name: "liblinker_debuggerd_stub",
163 defaults: ["linker_defaults", "linker_all_targets"],
164 srcs: ["linker_debuggerd_stub.cpp"],
165}
166
167// ========================================================
168// template for the linker binary
169// ========================================================
170
dimitryb8b3a762018-09-25 12:31:11 +0200171filegroup {
172 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700173 srcs: [
174 "dlfcn.cpp",
175 "linker.cpp",
176 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700177 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700178 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800179 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800180 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700181 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700182 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700183 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800184 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700185 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700186 "linker_logger.cpp",
187 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100188 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700189 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800190 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700191 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700192 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800193 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700194 "linker_utils.cpp",
195 "rt.cpp",
196 ],
dimitryb8b3a762018-09-25 12:31:11 +0200197}
Colin Cross97f0aef2016-07-14 16:05:46 -0700198
dimitryb8b3a762018-09-25 12:31:11 +0200199filegroup {
200 name: "linker_sources_arm",
201 srcs: [
202 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800203 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200204 ],
205}
206
207filegroup {
208 name: "linker_sources_arm64",
209 srcs: [
210 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800211 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800212 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200213 ],
214}
215
216filegroup {
217 name: "linker_sources_x86",
218 srcs: [
219 "arch/x86/begin.S",
220 ],
221}
222
223filegroup {
224 name: "linker_sources_x86_64",
225 srcs: [
226 "arch/x86_64/begin.S",
227 ],
228}
229
dimitryb8b3a762018-09-25 12:31:11 +0200230cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700231 name: "linker_version_script_overlay",
232 arch: {
233 arm: { version_script: "linker.arm.map" },
234 arm64: { version_script: "linker.generic.map" },
235 x86: { version_script: "linker.generic.map" },
236 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700237 },
238}
239
240// A template for the linker binary. May be inherited by native bridge implementations.
241cc_defaults {
242 name: "linker_bin_template",
243 defaults: ["linker_defaults"],
244
245 srcs: [":linker_sources"],
246
Colin Cross97f0aef2016-07-14 16:05:46 -0700247 arch: {
248 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700249 srcs: [":linker_sources_arm"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700250 },
251 arm64: {
252 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700253 },
254 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700255 srcs: [":linker_sources_x86"],
256 },
257 x86_64: {
258 srcs: [":linker_sources_x86_64"],
259 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700260 },
261
Ryan Prichard80e40f02019-10-31 19:54:46 -0700262 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
263 // static_executable. This dynamic linker is actually a shared object linked with static
264 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700265 ldflags: [
266 "-shared",
267 "-Wl,-Bsymbolic",
268 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100269 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700270 ],
271
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800272 // we are going to link libc++_static manually because
273 // when stl is not set to "none" build system adds libdl
274 // to the list of static libraries which needs to be
275 // avoided in the case of building loader.
276 stl: "none",
277
Colin Cross97f0aef2016-07-14 16:05:46 -0700278 // we don't want crtbegin.o (because we have begin.o), so unset it
279 // just for this module
280 nocrt: true,
281
dimitryb8b3a762018-09-25 12:31:11 +0200282 static_executable: true,
283
284 // Leave the symbols in the shared library so that stack unwinders can produce
285 // meaningful name resolution.
286 strip: {
287 keep_symbols: true,
288 },
289
290 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
291 // looking up symbols in the linker by mistake.
292 prefix_symbols: "__dl_",
293
294 sanitize: {
295 hwaddress: false,
296 },
dimitryb8b3a762018-09-25 12:31:11 +0200297
Colin Cross97f0aef2016-07-14 16:05:46 -0700298 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700299 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700300 "liblinker_malloc",
301
302 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700303 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700304 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800305 "libm",
Ryan Prichardcdf71752020-12-16 03:37:22 -0800306 "libunwind",
Colin Cross97f0aef2016-07-14 16:05:46 -0700307 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700308
Ryan Prichardd9a41152019-10-14 16:58:53 -0700309 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
310 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
311 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
312 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
313 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
314 //
315 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
316 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
317 // linker stops linking against libgcc.a's arm32 unwinder.
318 whole_static_libs: ["libc_unwind_static"],
319
Ryan Prichard80e40f02019-10-31 19:54:46 -0700320 system_shared_libs: [],
321
322 // Opt out of native_coverage when opting out of system_shared_libs
323 native_coverage: false,
324}
325
326// ========================================================
327// linker[_asan][64] binary
328// ========================================================
329
330cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700331 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700332 defaults: [
333 "linker_bin_template",
334 "linux_bionic_supported",
335 "linker_version_script_overlay",
336 ],
337
Victor Khimenkod15229d2020-05-14 22:14:45 +0200338 srcs: [
339 "linker_translate_path.cpp",
340 ],
341
Colin Cross10fffb42016-12-08 09:57:35 -0800342 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700343 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700344 lib64: {
345 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800346 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700347 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800348
Ryan Prichard80e40f02019-10-31 19:54:46 -0700349 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700350
351 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700352 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700353 apex_available: [
354 "//apex_available:platform",
355 "com.android.runtime",
356 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800357
Colin Cross97f0aef2016-07-14 16:05:46 -0700358 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800359 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700360 srcs: [
361 "linker_debuggerd_android.cpp",
362 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700363 static_libs: [
364 "libc++demangle",
365 "libdebuggerd_handler_fallback",
366 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800367 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700368 linux_bionic: {
369 static_libs: [
370 "liblinker_debuggerd_stub",
371 ],
Yi Kong6f6daaa2020-12-10 01:27:44 +0800372 },
373 android_arm64: {
374 pgo: {
375 profile_file: "bionic/linker_arm_arm64.profdata",
376 },
377 },
378 android_arm: {
379 pgo: {
380 profile_file: "bionic/linker_arm_arm64.profdata",
381 },
382 },
383 android_x86_64: {
384 pgo: {
385 profile_file: "bionic/linker_x86_x86_64.profdata",
386 },
387 },
388 android_x86: {
389 pgo: {
390 profile_file: "bionic/linker_x86_x86_64.profdata",
391 },
392 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700393 },
Yi Konga7e363f2020-10-01 04:10:01 +0800394
395 lto: {
396 never: true,
397 },
Yi Kong6f6daaa2020-12-10 01:27:44 +0800398 pgo: {
399 sampling: true,
400 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700401}
dimitry11da1dc2018-01-05 13:35:43 +0100402
Ryan Prichard80e40f02019-10-31 19:54:46 -0700403// ========================================================
404// assorted modules
405// ========================================================
406
Elliott Hughes90f96b92019-05-09 15:56:39 -0700407sh_binary {
408 name: "ldd",
Rupert Shuttlewortha7e29a82021-02-18 13:35:22 +0000409 src: "ldd.sh",
Rupert Shuttleworth344b8da2021-02-09 11:35:04 +0000410 bazel_module: { bp2build_available: true },
Elliott Hughes90f96b92019-05-09 15:56:39 -0700411}
412
dimitry11da1dc2018-01-05 13:35:43 +0100413cc_library {
414 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
415 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
416 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
417 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
418 // we use this property to make sure libc.so has its own copy of the code from
419 // libgcc.a it uses.
420 //
421 // DO NOT REMOVE --exclude-libs!
422
Yi Kong7786a342018-08-31 19:01:56 -0700423 ldflags: [
424 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700425 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700426 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
427 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
Ryan Prichardef147872021-01-28 14:06:52 -0800428 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
Yi Kong7786a342018-08-31 19:01:56 -0700429 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
430 ],
dimitry11da1dc2018-01-05 13:35:43 +0100431
432 // for x86, exclude libgcc_eh.a for the same reasons as above
433 arch: {
434 x86: {
435 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
436 },
437 x86_64: {
438 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
439 },
440 },
dimitry581723e2018-01-05 14:31:44 +0100441
442 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100443 cflags: [
444 "-Wall",
445 "-Wextra",
446 "-Wunused",
447 "-Werror",
448 ],
449 stl: "none",
450
451 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700452 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800453 ramdisk_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700454 vendor_ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900455 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200456 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100457
Ryan Prichard470b6662018-03-27 22:10:55 -0700458 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100459 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100460 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100461
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800462 // Opt out of native_coverage when opting out of system_shared_libs
463 native_coverage: false,
464
dimitry11da1dc2018-01-05 13:35:43 +0100465 sanitize: {
466 never: true,
467 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900468
469 apex_available: [
470 "//apex_available:platform",
471 "com.android.runtime",
472 ],
Yi Kong15a05a72020-09-22 00:56:13 +0800473
474 lto: {
475 never: true,
476 },
dimitry11da1dc2018-01-05 13:35:43 +0100477}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800478
479cc_test {
480 name: "linker-unit-tests",
481
482 cflags: [
483 "-g",
484 "-Wall",
485 "-Wextra",
486 "-Wunused",
487 "-Werror",
488 ],
489
490 // We need to access Bionic private headers in the linker.
491 include_dirs: ["bionic/libc"],
492
493 srcs: [
494 // Tests.
495 "linker_block_allocator_test.cpp",
496 "linker_config_test.cpp",
497 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100498 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800499 "linker_sleb128_test.cpp",
500 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800501 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800502
503 // Parts of the linker that we're testing.
504 "linker_block_allocator.cpp",
505 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800506 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100507 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800508 "linker_test_globals.cpp",
509 "linker_utils.cpp",
510 ],
511
512 static_libs: [
513 "libasync_safe",
514 "libbase",
515 "liblog",
516 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800517
518 arch: {
519 arm: {
520 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
521 },
522 arm64: {
523 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
524 },
525 },
526}
527
528cc_benchmark {
529 name: "linker-benchmarks",
530
531 srcs: [
532 "linker_gnu_hash_benchmark.cpp",
533 ],
534
535 arch: {
536 arm: {
537 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
538 },
539 arm64: {
540 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
541 },
542 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800543}