blob: d6a3d71eaaea0b59ce1c542820d69fd15feb3497 [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"],
Jingwen Chenc5794782021-03-10 03:24:48 -050071
72 // b/182338959
73 bazel_module: { bp2build_available: false },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070074}
75
Ryan Prichard80e40f02019-10-31 19:54:46 -070076// ========================================================
77// linker default configuration
78// ========================================================
79
80// Configuration for the linker binary and any of its static libraries.
81cc_defaults {
82 name: "linker_defaults",
83 arch: {
84 arm: {
85 cflags: ["-D__work_around_b_24465209__"],
86 },
87 x86: {
88 cflags: ["-D__work_around_b_24465209__"],
89 },
90 },
91
92 cflags: [
93 "-fno-stack-protector",
94 "-Wstrict-overflow=5",
95 "-fvisibility=hidden",
96 "-Wall",
97 "-Wextra",
98 "-Wunused",
99 "-Werror",
100 ],
101
102 // TODO: split out the asflags.
103 asflags: [
104 "-fno-stack-protector",
105 "-Wstrict-overflow=5",
106 "-fvisibility=hidden",
107 "-Wall",
108 "-Wextra",
109 "-Wunused",
110 "-Werror",
111 ],
112
113 product_variables: {
114 debuggable: {
115 cppflags: ["-DUSE_LD_CONFIG_FILE"],
116 },
117 },
118
119 cppflags: ["-Wold-style-cast"],
120
121 static_libs: [
122 "libziparchive",
123 "libbase",
124 "libz",
125
126 "libasync_safe",
127
128 "liblog",
129 ],
130
131 // We need to access Bionic private headers in the linker.
132 include_dirs: ["bionic/libc"],
133}
134
135// ========================================================
136// linker components
137// ========================================================
138
139// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
140// native bridge implementations).
141cc_defaults {
142 name: "linker_all_targets",
143 defaults: ["linux_bionic_supported"],
144 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700145 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700146 native_bridge_supported: true,
147}
148
149cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700150 name: "liblinker_main",
151 defaults: ["linker_defaults", "linker_all_targets"],
152 srcs: ["linker_main.cpp"],
153
154 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
155 cflags: ["-ffreestanding"],
156}
157
158cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700159 name: "liblinker_malloc",
160 defaults: ["linker_defaults", "linker_all_targets"],
161 srcs: ["linker_memory.cpp"],
162}
163
164cc_library_static {
165 name: "liblinker_debuggerd_stub",
166 defaults: ["linker_defaults", "linker_all_targets"],
167 srcs: ["linker_debuggerd_stub.cpp"],
168}
169
170// ========================================================
171// template for the linker binary
172// ========================================================
173
dimitryb8b3a762018-09-25 12:31:11 +0200174filegroup {
175 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700176 srcs: [
177 "dlfcn.cpp",
178 "linker.cpp",
179 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700180 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700181 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800182 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800183 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700184 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700185 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700186 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800187 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700188 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700189 "linker_logger.cpp",
190 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100191 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700192 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800193 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700194 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700195 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800196 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700197 "linker_utils.cpp",
198 "rt.cpp",
199 ],
dimitryb8b3a762018-09-25 12:31:11 +0200200}
Colin Cross97f0aef2016-07-14 16:05:46 -0700201
dimitryb8b3a762018-09-25 12:31:11 +0200202filegroup {
203 name: "linker_sources_arm",
204 srcs: [
205 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800206 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200207 ],
208}
209
210filegroup {
211 name: "linker_sources_arm64",
212 srcs: [
213 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800214 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800215 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200216 ],
217}
218
219filegroup {
220 name: "linker_sources_x86",
221 srcs: [
222 "arch/x86/begin.S",
223 ],
224}
225
226filegroup {
227 name: "linker_sources_x86_64",
228 srcs: [
229 "arch/x86_64/begin.S",
230 ],
231}
232
dimitryb8b3a762018-09-25 12:31:11 +0200233cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700234 name: "linker_version_script_overlay",
235 arch: {
236 arm: { version_script: "linker.arm.map" },
237 arm64: { version_script: "linker.generic.map" },
238 x86: { version_script: "linker.generic.map" },
239 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700240 },
241}
242
243// A template for the linker binary. May be inherited by native bridge implementations.
244cc_defaults {
245 name: "linker_bin_template",
246 defaults: ["linker_defaults"],
247
248 srcs: [":linker_sources"],
249
Colin Cross97f0aef2016-07-14 16:05:46 -0700250 arch: {
251 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700252 srcs: [":linker_sources_arm"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700253 },
254 arm64: {
255 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700256 },
257 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700258 srcs: [":linker_sources_x86"],
259 },
260 x86_64: {
261 srcs: [":linker_sources_x86_64"],
262 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700263 },
264
Ryan Prichard80e40f02019-10-31 19:54:46 -0700265 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
266 // static_executable. This dynamic linker is actually a shared object linked with static
267 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700268 ldflags: [
269 "-shared",
270 "-Wl,-Bsymbolic",
271 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100272 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700273 ],
274
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800275 // we are going to link libc++_static manually because
276 // when stl is not set to "none" build system adds libdl
277 // to the list of static libraries which needs to be
278 // avoided in the case of building loader.
279 stl: "none",
280
Colin Cross97f0aef2016-07-14 16:05:46 -0700281 // we don't want crtbegin.o (because we have begin.o), so unset it
282 // just for this module
283 nocrt: true,
284
dimitryb8b3a762018-09-25 12:31:11 +0200285 static_executable: true,
286
287 // Leave the symbols in the shared library so that stack unwinders can produce
288 // meaningful name resolution.
289 strip: {
290 keep_symbols: true,
291 },
292
293 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
294 // looking up symbols in the linker by mistake.
295 prefix_symbols: "__dl_",
296
297 sanitize: {
298 hwaddress: false,
299 },
dimitryb8b3a762018-09-25 12:31:11 +0200300
Colin Cross97f0aef2016-07-14 16:05:46 -0700301 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700302 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700303 "liblinker_malloc",
304
305 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700306 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700307 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800308 "libm",
Ryan Prichardcdf71752020-12-16 03:37:22 -0800309 "libunwind",
Colin Cross97f0aef2016-07-14 16:05:46 -0700310 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700311
Ryan Prichardd9a41152019-10-14 16:58:53 -0700312 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
313 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
314 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
315 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
316 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
317 //
318 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
319 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
320 // linker stops linking against libgcc.a's arm32 unwinder.
321 whole_static_libs: ["libc_unwind_static"],
322
Ryan Prichard80e40f02019-10-31 19:54:46 -0700323 system_shared_libs: [],
324
325 // Opt out of native_coverage when opting out of system_shared_libs
326 native_coverage: false,
327}
328
329// ========================================================
330// linker[_asan][64] binary
331// ========================================================
332
333cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700334 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700335 defaults: [
336 "linker_bin_template",
337 "linux_bionic_supported",
338 "linker_version_script_overlay",
339 ],
340
Victor Khimenkod15229d2020-05-14 22:14:45 +0200341 srcs: [
342 "linker_translate_path.cpp",
343 ],
344
Colin Cross10fffb42016-12-08 09:57:35 -0800345 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700346 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700347 lib64: {
348 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800349 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700350 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800351
Ryan Prichard80e40f02019-10-31 19:54:46 -0700352 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700353
354 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700355 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700356 apex_available: [
357 "//apex_available:platform",
358 "com.android.runtime",
359 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800360
Colin Cross97f0aef2016-07-14 16:05:46 -0700361 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800362 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700363 srcs: [
364 "linker_debuggerd_android.cpp",
365 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700366 static_libs: [
367 "libc++demangle",
368 "libdebuggerd_handler_fallback",
369 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800370 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700371 linux_bionic: {
372 static_libs: [
373 "liblinker_debuggerd_stub",
374 ],
Yi Kong6f6daaa2020-12-10 01:27:44 +0800375 },
376 android_arm64: {
377 pgo: {
378 profile_file: "bionic/linker_arm_arm64.profdata",
379 },
380 },
381 android_arm: {
382 pgo: {
383 profile_file: "bionic/linker_arm_arm64.profdata",
384 },
385 },
386 android_x86_64: {
387 pgo: {
388 profile_file: "bionic/linker_x86_x86_64.profdata",
389 },
390 },
391 android_x86: {
392 pgo: {
393 profile_file: "bionic/linker_x86_x86_64.profdata",
394 },
395 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700396 },
Yi Konga7e363f2020-10-01 04:10:01 +0800397
398 lto: {
399 never: true,
400 },
Yi Kong6f6daaa2020-12-10 01:27:44 +0800401 pgo: {
402 sampling: true,
403 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700404}
dimitry11da1dc2018-01-05 13:35:43 +0100405
Ryan Prichard80e40f02019-10-31 19:54:46 -0700406// ========================================================
407// assorted modules
408// ========================================================
409
Elliott Hughes90f96b92019-05-09 15:56:39 -0700410sh_binary {
411 name: "ldd",
Rupert Shuttlewortha7e29a82021-02-18 13:35:22 +0000412 src: "ldd.sh",
Rupert Shuttleworth344b8da2021-02-09 11:35:04 +0000413 bazel_module: { bp2build_available: true },
Elliott Hughes90f96b92019-05-09 15:56:39 -0700414}
415
Collin Fijalkovichc9521e02021-04-29 11:31:50 -0700416// Used to generate binaries that can be backed by transparent hugepages.
417cc_defaults {
418 name: "linker_hugepage_aligned",
419 arch: {
420 arm64: {
421 ldflags: ["-z max-page-size=0x200000"],
422 },
423 x86_64: {
424 ldflags: ["-z max-page-size=0x200000"],
425 },
426 },
427}
428
dimitry11da1dc2018-01-05 13:35:43 +0100429cc_library {
430 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
431 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
432 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
433 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
434 // we use this property to make sure libc.so has its own copy of the code from
435 // libgcc.a it uses.
436 //
437 // DO NOT REMOVE --exclude-libs!
438
Yi Kong7786a342018-08-31 19:01:56 -0700439 ldflags: [
440 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700441 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700442 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
443 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
Ryan Prichardef147872021-01-28 14:06:52 -0800444 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
Yi Kong7786a342018-08-31 19:01:56 -0700445 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
446 ],
dimitry11da1dc2018-01-05 13:35:43 +0100447
448 // for x86, exclude libgcc_eh.a for the same reasons as above
449 arch: {
450 x86: {
451 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
452 },
453 x86_64: {
454 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
455 },
456 },
dimitry581723e2018-01-05 14:31:44 +0100457
458 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100459 cflags: [
460 "-Wall",
461 "-Wextra",
462 "-Wunused",
463 "-Werror",
464 ],
465 stl: "none",
466
467 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700468 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800469 ramdisk_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700470 vendor_ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900471 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200472 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100473
Ryan Prichard470b6662018-03-27 22:10:55 -0700474 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100475 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100476 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100477
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800478 // Opt out of native_coverage when opting out of system_shared_libs
479 native_coverage: false,
480
dimitry11da1dc2018-01-05 13:35:43 +0100481 sanitize: {
482 never: true,
483 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900484
485 apex_available: [
486 "//apex_available:platform",
487 "com.android.runtime",
488 ],
Yi Kong15a05a72020-09-22 00:56:13 +0800489
490 lto: {
491 never: true,
492 },
dimitry11da1dc2018-01-05 13:35:43 +0100493}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800494
495cc_test {
496 name: "linker-unit-tests",
497
498 cflags: [
499 "-g",
500 "-Wall",
501 "-Wextra",
502 "-Wunused",
503 "-Werror",
504 ],
505
506 // We need to access Bionic private headers in the linker.
507 include_dirs: ["bionic/libc"],
508
509 srcs: [
510 // Tests.
511 "linker_block_allocator_test.cpp",
512 "linker_config_test.cpp",
513 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100514 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800515 "linker_sleb128_test.cpp",
516 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800517 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800518
519 // Parts of the linker that we're testing.
520 "linker_block_allocator.cpp",
521 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800522 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100523 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800524 "linker_test_globals.cpp",
525 "linker_utils.cpp",
526 ],
527
528 static_libs: [
529 "libasync_safe",
530 "libbase",
531 "liblog",
532 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800533
534 arch: {
535 arm: {
536 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
537 },
538 arm64: {
539 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
540 },
541 },
542}
543
544cc_benchmark {
545 name: "linker-benchmarks",
546
547 srcs: [
548 "linker_gnu_hash_benchmark.cpp",
549 ],
550
551 arch: {
552 arm: {
553 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
554 },
555 arm64: {
556 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
557 },
558 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800559}