blob: dbefcf61644de3a041bd0913b4ecf15789ae262c [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: {
Colin Crossa0a591a2021-06-11 12:46:45 -070058 srcs: ["arch/arm64/linker_wrapper_begin.S"],
Jiyong Park3b47d602020-09-18 16:15:53 +090059 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070060 x86_64: {
Colin Crossa0a591a2021-06-11 12:46:45 -070061 srcs: ["arch/x86_64/linker_wrapper_begin.S"],
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070062 },
63 },
64
Martin Stjernholm82d84bc2020-04-06 20:32:09 +010065 header_libs: ["libc_headers"],
66
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070067 // We need to access Bionic private headers in the linker.
68 include_dirs: ["bionic/libc"],
69}
70
Ryan Prichard80e40f02019-10-31 19:54:46 -070071// ========================================================
72// linker default configuration
73// ========================================================
74
75// Configuration for the linker binary and any of its static libraries.
76cc_defaults {
77 name: "linker_defaults",
78 arch: {
79 arm: {
80 cflags: ["-D__work_around_b_24465209__"],
81 },
82 x86: {
83 cflags: ["-D__work_around_b_24465209__"],
84 },
85 },
86
87 cflags: [
88 "-fno-stack-protector",
89 "-Wstrict-overflow=5",
90 "-fvisibility=hidden",
91 "-Wall",
92 "-Wextra",
93 "-Wunused",
94 "-Werror",
95 ],
96
97 // TODO: split out the asflags.
98 asflags: [
99 "-fno-stack-protector",
100 "-Wstrict-overflow=5",
101 "-fvisibility=hidden",
102 "-Wall",
103 "-Wextra",
104 "-Wunused",
105 "-Werror",
106 ],
107
108 product_variables: {
109 debuggable: {
110 cppflags: ["-DUSE_LD_CONFIG_FILE"],
111 },
112 },
113
114 cppflags: ["-Wold-style-cast"],
115
116 static_libs: [
117 "libziparchive",
118 "libbase",
119 "libz",
120
121 "libasync_safe",
122
123 "liblog",
124 ],
125
126 // We need to access Bionic private headers in the linker.
127 include_dirs: ["bionic/libc"],
128}
129
130// ========================================================
131// linker components
132// ========================================================
133
134// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
135// native bridge implementations).
136cc_defaults {
137 name: "linker_all_targets",
138 defaults: ["linux_bionic_supported"],
139 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700140 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700141 native_bridge_supported: true,
142}
143
144cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700145 name: "liblinker_main",
146 defaults: ["linker_defaults", "linker_all_targets"],
147 srcs: ["linker_main.cpp"],
148
149 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
150 cflags: ["-ffreestanding"],
151}
152
153cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700154 name: "liblinker_malloc",
155 defaults: ["linker_defaults", "linker_all_targets"],
156 srcs: ["linker_memory.cpp"],
157}
158
159cc_library_static {
160 name: "liblinker_debuggerd_stub",
161 defaults: ["linker_defaults", "linker_all_targets"],
162 srcs: ["linker_debuggerd_stub.cpp"],
163}
164
165// ========================================================
166// template for the linker binary
167// ========================================================
168
dimitryb8b3a762018-09-25 12:31:11 +0200169filegroup {
170 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700171 srcs: [
172 "dlfcn.cpp",
173 "linker.cpp",
174 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700175 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700176 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800177 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800178 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700179 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700180 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700181 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800182 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700183 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700184 "linker_logger.cpp",
185 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100186 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700187 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800188 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700189 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700190 "linker_soinfo.cpp",
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -0700191 "linker_transparent_hugepage_support.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800192 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700193 "linker_utils.cpp",
194 "rt.cpp",
195 ],
dimitryb8b3a762018-09-25 12:31:11 +0200196}
Colin Cross97f0aef2016-07-14 16:05:46 -0700197
dimitryb8b3a762018-09-25 12:31:11 +0200198filegroup {
199 name: "linker_sources_arm",
200 srcs: [
201 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800202 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200203 ],
204}
205
206filegroup {
207 name: "linker_sources_arm64",
208 srcs: [
209 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800210 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800211 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200212 ],
213}
214
215filegroup {
216 name: "linker_sources_x86",
217 srcs: [
218 "arch/x86/begin.S",
219 ],
220}
221
222filegroup {
223 name: "linker_sources_x86_64",
224 srcs: [
225 "arch/x86_64/begin.S",
226 ],
227}
228
dimitryb8b3a762018-09-25 12:31:11 +0200229cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700230 name: "linker_version_script_overlay",
231 arch: {
232 arm: { version_script: "linker.arm.map" },
233 arm64: { version_script: "linker.generic.map" },
234 x86: { version_script: "linker.generic.map" },
235 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700236 },
237}
238
239// A template for the linker binary. May be inherited by native bridge implementations.
240cc_defaults {
241 name: "linker_bin_template",
242 defaults: ["linker_defaults"],
243
244 srcs: [":linker_sources"],
245
Colin Cross97f0aef2016-07-14 16:05:46 -0700246 arch: {
247 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700248 srcs: [":linker_sources_arm"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700249 },
250 arm64: {
251 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700252 },
253 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700254 srcs: [":linker_sources_x86"],
255 },
256 x86_64: {
257 srcs: [":linker_sources_x86_64"],
258 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700259 },
260
Ryan Prichard80e40f02019-10-31 19:54:46 -0700261 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
262 // static_executable. This dynamic linker is actually a shared object linked with static
263 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700264 ldflags: [
265 "-shared",
266 "-Wl,-Bsymbolic",
267 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100268 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700269 ],
270
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800271 // we are going to link libc++_static manually because
272 // when stl is not set to "none" build system adds libdl
273 // to the list of static libraries which needs to be
274 // avoided in the case of building loader.
275 stl: "none",
276
Colin Cross97f0aef2016-07-14 16:05:46 -0700277 // we don't want crtbegin.o (because we have begin.o), so unset it
278 // just for this module
279 nocrt: true,
280
dimitryb8b3a762018-09-25 12:31:11 +0200281 static_executable: true,
282
283 // Leave the symbols in the shared library so that stack unwinders can produce
284 // meaningful name resolution.
285 strip: {
286 keep_symbols: true,
287 },
288
289 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
290 // looking up symbols in the linker by mistake.
291 prefix_symbols: "__dl_",
292
293 sanitize: {
294 hwaddress: false,
295 },
dimitryb8b3a762018-09-25 12:31:11 +0200296
Colin Cross97f0aef2016-07-14 16:05:46 -0700297 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700298 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700299 "liblinker_malloc",
300
301 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700302 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700303 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800304 "libm",
Ryan Prichardcdf71752020-12-16 03:37:22 -0800305 "libunwind",
Colin Cross97f0aef2016-07-14 16:05:46 -0700306 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700307
Ryan Prichardd9a41152019-10-14 16:58:53 -0700308 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
309 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
310 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
311 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
312 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
313 //
314 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
315 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
316 // linker stops linking against libgcc.a's arm32 unwinder.
317 whole_static_libs: ["libc_unwind_static"],
318
Ryan Prichard80e40f02019-10-31 19:54:46 -0700319 system_shared_libs: [],
320
321 // Opt out of native_coverage when opting out of system_shared_libs
322 native_coverage: false,
323}
324
325// ========================================================
326// linker[_asan][64] binary
327// ========================================================
328
329cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700330 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700331 defaults: [
332 "linker_bin_template",
333 "linux_bionic_supported",
334 "linker_version_script_overlay",
335 ],
336
Victor Khimenkod15229d2020-05-14 22:14:45 +0200337 srcs: [
338 "linker_translate_path.cpp",
339 ],
340
Colin Cross10fffb42016-12-08 09:57:35 -0800341 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700342 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700343 lib64: {
344 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800345 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700346 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800347
Ryan Prichard80e40f02019-10-31 19:54:46 -0700348 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700349
350 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700351 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700352 apex_available: [
353 "//apex_available:platform",
354 "com.android.runtime",
355 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800356
Colin Cross97f0aef2016-07-14 16:05:46 -0700357 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800358 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700359 srcs: [
360 "linker_debuggerd_android.cpp",
361 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700362 static_libs: [
363 "libc++demangle",
364 "libdebuggerd_handler_fallback",
365 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800366 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700367 linux_bionic: {
368 static_libs: [
369 "liblinker_debuggerd_stub",
370 ],
Yi Kong6f6daaa2020-12-10 01:27:44 +0800371 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700372 },
Yi Konga7e363f2020-10-01 04:10:01 +0800373
Yi Konge20a1d92022-01-25 03:19:58 +0800374 afdo: true,
Colin Cross97f0aef2016-07-14 16:05:46 -0700375}
dimitry11da1dc2018-01-05 13:35:43 +0100376
Ryan Prichard80e40f02019-10-31 19:54:46 -0700377// ========================================================
378// assorted modules
379// ========================================================
380
Elliott Hughes90f96b92019-05-09 15:56:39 -0700381sh_binary {
382 name: "ldd",
Rupert Shuttlewortha7e29a82021-02-18 13:35:22 +0000383 src: "ldd.sh",
Elliott Hughes90f96b92019-05-09 15:56:39 -0700384}
385
Collin Fijalkovichc9521e02021-04-29 11:31:50 -0700386// Used to generate binaries that can be backed by transparent hugepages.
387cc_defaults {
388 name: "linker_hugepage_aligned",
389 arch: {
390 arm64: {
391 ldflags: ["-z max-page-size=0x200000"],
392 },
393 x86_64: {
394 ldflags: ["-z max-page-size=0x200000"],
395 },
396 },
397}
398
dimitry11da1dc2018-01-05 13:35:43 +0100399cc_library {
400 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
401 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
402 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
403 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
404 // we use this property to make sure libc.so has its own copy of the code from
405 // libgcc.a it uses.
406 //
407 // DO NOT REMOVE --exclude-libs!
408
Yi Kong7786a342018-08-31 19:01:56 -0700409 ldflags: [
410 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700411 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700412 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
413 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
Ryan Prichardef147872021-01-28 14:06:52 -0800414 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
Yi Kong7786a342018-08-31 19:01:56 -0700415 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
416 ],
dimitry11da1dc2018-01-05 13:35:43 +0100417
418 // for x86, exclude libgcc_eh.a for the same reasons as above
419 arch: {
420 x86: {
421 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
422 },
423 x86_64: {
424 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
425 },
426 },
dimitry581723e2018-01-05 14:31:44 +0100427
428 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100429 cflags: [
430 "-Wall",
431 "-Wextra",
432 "-Wunused",
433 "-Werror",
434 ],
435 stl: "none",
436
437 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700438 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800439 ramdisk_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700440 vendor_ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900441 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200442 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100443
Ryan Prichard470b6662018-03-27 22:10:55 -0700444 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100445 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100446 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100447
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800448 // Opt out of native_coverage when opting out of system_shared_libs
449 native_coverage: false,
450
dimitry11da1dc2018-01-05 13:35:43 +0100451 sanitize: {
452 never: true,
453 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900454
455 apex_available: [
456 "//apex_available:platform",
457 "com.android.runtime",
458 ],
Yi Kong15a05a72020-09-22 00:56:13 +0800459
460 lto: {
461 never: true,
462 },
dimitry11da1dc2018-01-05 13:35:43 +0100463}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800464
465cc_test {
466 name: "linker-unit-tests",
Colin Cross0cc60af2021-09-30 14:04:39 -0700467 test_suites: ["device-tests"],
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800468
469 cflags: [
470 "-g",
471 "-Wall",
472 "-Wextra",
473 "-Wunused",
474 "-Werror",
475 ],
476
477 // We need to access Bionic private headers in the linker.
478 include_dirs: ["bionic/libc"],
479
480 srcs: [
481 // Tests.
482 "linker_block_allocator_test.cpp",
483 "linker_config_test.cpp",
484 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100485 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800486 "linker_sleb128_test.cpp",
487 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800488 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800489
490 // Parts of the linker that we're testing.
491 "linker_block_allocator.cpp",
492 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800493 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100494 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800495 "linker_test_globals.cpp",
496 "linker_utils.cpp",
497 ],
498
499 static_libs: [
500 "libasync_safe",
501 "libbase",
502 "liblog",
503 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800504
505 arch: {
506 arm: {
507 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
508 },
509 arm64: {
510 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
511 },
512 },
513}
514
515cc_benchmark {
516 name: "linker-benchmarks",
517
518 srcs: [
519 "linker_gnu_hash_benchmark.cpp",
520 ],
521
522 arch: {
523 arm: {
524 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
525 },
526 arm64: {
527 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
528 },
529 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800530}