blob: d5e7367f4f25b657732d8f444c419c2cb6c05cbe [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",
73 arch: {
74 arm: {
75 cflags: ["-D__work_around_b_24465209__"],
76 },
77 x86: {
78 cflags: ["-D__work_around_b_24465209__"],
79 },
80 },
81
82 cflags: [
83 "-fno-stack-protector",
84 "-Wstrict-overflow=5",
85 "-fvisibility=hidden",
86 "-Wall",
87 "-Wextra",
88 "-Wunused",
89 "-Werror",
90 ],
91
92 // TODO: split out the asflags.
93 asflags: [
94 "-fno-stack-protector",
95 "-Wstrict-overflow=5",
96 "-fvisibility=hidden",
97 "-Wall",
98 "-Wextra",
99 "-Wunused",
100 "-Werror",
101 ],
102
103 product_variables: {
104 debuggable: {
105 cppflags: ["-DUSE_LD_CONFIG_FILE"],
106 },
107 },
108
109 cppflags: ["-Wold-style-cast"],
110
111 static_libs: [
112 "libziparchive",
113 "libbase",
114 "libz",
115
116 "libasync_safe",
117
118 "liblog",
119 ],
120
121 // We need to access Bionic private headers in the linker.
122 include_dirs: ["bionic/libc"],
123}
124
125// ========================================================
126// linker components
127// ========================================================
128
129// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
130// native bridge implementations).
131cc_defaults {
132 name: "linker_all_targets",
133 defaults: ["linux_bionic_supported"],
134 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700135 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700136 native_bridge_supported: true,
137}
138
139cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700140 name: "liblinker_main",
141 defaults: ["linker_defaults", "linker_all_targets"],
142 srcs: ["linker_main.cpp"],
143
144 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
145 cflags: ["-ffreestanding"],
146}
147
148cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700149 name: "liblinker_malloc",
150 defaults: ["linker_defaults", "linker_all_targets"],
151 srcs: ["linker_memory.cpp"],
152}
153
154cc_library_static {
155 name: "liblinker_debuggerd_stub",
156 defaults: ["linker_defaults", "linker_all_targets"],
157 srcs: ["linker_debuggerd_stub.cpp"],
158}
159
160// ========================================================
161// template for the linker binary
162// ========================================================
163
dimitryb8b3a762018-09-25 12:31:11 +0200164filegroup {
165 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700166 srcs: [
167 "dlfcn.cpp",
168 "linker.cpp",
169 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700170 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700171 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800172 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800173 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700174 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700175 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700176 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800177 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700178 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700179 "linker_logger.cpp",
180 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100181 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700182 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800183 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700184 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700185 "linker_soinfo.cpp",
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -0700186 "linker_transparent_hugepage_support.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800187 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700188 "linker_utils.cpp",
189 "rt.cpp",
190 ],
dimitryb8b3a762018-09-25 12:31:11 +0200191}
Colin Cross97f0aef2016-07-14 16:05:46 -0700192
dimitryb8b3a762018-09-25 12:31:11 +0200193filegroup {
194 name: "linker_sources_arm",
195 srcs: [
196 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800197 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200198 ],
199}
200
201filegroup {
202 name: "linker_sources_arm64",
203 srcs: [
204 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800205 "arch/arm64/tlsdesc_resolver.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_x86",
212 srcs: [
213 "arch/x86/begin.S",
214 ],
215}
216
217filegroup {
218 name: "linker_sources_x86_64",
219 srcs: [
220 "arch/x86_64/begin.S",
221 ],
222}
223
dimitryb8b3a762018-09-25 12:31:11 +0200224cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700225 name: "linker_version_script_overlay",
226 arch: {
227 arm: { version_script: "linker.arm.map" },
228 arm64: { version_script: "linker.generic.map" },
229 x86: { version_script: "linker.generic.map" },
230 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700231 },
232}
233
234// A template for the linker binary. May be inherited by native bridge implementations.
235cc_defaults {
236 name: "linker_bin_template",
237 defaults: ["linker_defaults"],
238
239 srcs: [":linker_sources"],
240
Colin Cross97f0aef2016-07-14 16:05:46 -0700241 arch: {
242 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700243 srcs: [":linker_sources_arm"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700244 },
245 arm64: {
246 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700247 },
248 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700249 srcs: [":linker_sources_x86"],
250 },
251 x86_64: {
252 srcs: [":linker_sources_x86_64"],
253 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700254 },
255
Ryan Prichard80e40f02019-10-31 19:54:46 -0700256 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
257 // static_executable. This dynamic linker is actually a shared object linked with static
258 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700259 ldflags: [
260 "-shared",
261 "-Wl,-Bsymbolic",
262 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100263 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700264 ],
265
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800266 // we are going to link libc++_static manually because
267 // when stl is not set to "none" build system adds libdl
268 // to the list of static libraries which needs to be
269 // avoided in the case of building loader.
270 stl: "none",
271
Colin Cross97f0aef2016-07-14 16:05:46 -0700272 // we don't want crtbegin.o (because we have begin.o), so unset it
273 // just for this module
274 nocrt: true,
275
dimitryb8b3a762018-09-25 12:31:11 +0200276 static_executable: true,
277
278 // Leave the symbols in the shared library so that stack unwinders can produce
279 // meaningful name resolution.
280 strip: {
281 keep_symbols: true,
282 },
283
284 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
285 // looking up symbols in the linker by mistake.
286 prefix_symbols: "__dl_",
287
288 sanitize: {
289 hwaddress: false,
290 },
dimitryb8b3a762018-09-25 12:31:11 +0200291
Colin Cross97f0aef2016-07-14 16:05:46 -0700292 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700293 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700294 "liblinker_malloc",
295
296 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700297 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700298 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800299 "libm",
Ryan Prichardcdf71752020-12-16 03:37:22 -0800300 "libunwind",
Colin Cross97f0aef2016-07-14 16:05:46 -0700301 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700302
Ryan Prichardd9a41152019-10-14 16:58:53 -0700303 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
304 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
305 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
306 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
307 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
308 //
309 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
310 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
311 // linker stops linking against libgcc.a's arm32 unwinder.
312 whole_static_libs: ["libc_unwind_static"],
313
Ryan Prichard80e40f02019-10-31 19:54:46 -0700314 system_shared_libs: [],
315
316 // Opt out of native_coverage when opting out of system_shared_libs
317 native_coverage: false,
318}
319
320// ========================================================
321// linker[_asan][64] binary
322// ========================================================
323
324cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700325 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700326 defaults: [
327 "linker_bin_template",
328 "linux_bionic_supported",
329 "linker_version_script_overlay",
330 ],
331
Victor Khimenkod15229d2020-05-14 22:14:45 +0200332 srcs: [
333 "linker_translate_path.cpp",
334 ],
335
Colin Cross10fffb42016-12-08 09:57:35 -0800336 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700337 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700338 lib64: {
339 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800340 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700341 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800342
Ryan Prichard80e40f02019-10-31 19:54:46 -0700343 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700344
345 recovery_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700346 vendor_ramdisk_available: true,
Ryan Prichard80e40f02019-10-31 19:54:46 -0700347 apex_available: [
348 "//apex_available:platform",
349 "com.android.runtime",
350 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800351
Colin Cross97f0aef2016-07-14 16:05:46 -0700352 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800353 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700354 srcs: [
355 "linker_debuggerd_android.cpp",
356 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700357 static_libs: [
358 "libc++demangle",
359 "libdebuggerd_handler_fallback",
360 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800361 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700362 linux_bionic: {
363 static_libs: [
364 "liblinker_debuggerd_stub",
365 ],
Yi Kong6f6daaa2020-12-10 01:27:44 +0800366 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700367 },
Yi Konga7e363f2020-10-01 04:10:01 +0800368
Yi Konge20a1d92022-01-25 03:19:58 +0800369 afdo: true,
Colin Cross97f0aef2016-07-14 16:05:46 -0700370}
dimitry11da1dc2018-01-05 13:35:43 +0100371
Ryan Prichard80e40f02019-10-31 19:54:46 -0700372// ========================================================
373// assorted modules
374// ========================================================
375
Elliott Hughes90f96b92019-05-09 15:56:39 -0700376sh_binary {
377 name: "ldd",
Rupert Shuttlewortha7e29a82021-02-18 13:35:22 +0000378 src: "ldd.sh",
Elliott Hughes90f96b92019-05-09 15:56:39 -0700379}
380
Collin Fijalkovichc9521e02021-04-29 11:31:50 -0700381// Used to generate binaries that can be backed by transparent hugepages.
382cc_defaults {
383 name: "linker_hugepage_aligned",
384 arch: {
385 arm64: {
386 ldflags: ["-z max-page-size=0x200000"],
387 },
388 x86_64: {
389 ldflags: ["-z max-page-size=0x200000"],
390 },
391 },
392}
393
dimitry11da1dc2018-01-05 13:35:43 +0100394cc_library {
395 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
396 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
397 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
398 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
399 // we use this property to make sure libc.so has its own copy of the code from
400 // libgcc.a it uses.
401 //
402 // DO NOT REMOVE --exclude-libs!
403
Yi Kong7786a342018-08-31 19:01:56 -0700404 ldflags: [
405 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700406 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700407 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
408 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
Ryan Prichardef147872021-01-28 14:06:52 -0800409 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
Yi Kong7786a342018-08-31 19:01:56 -0700410 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
411 ],
dimitry11da1dc2018-01-05 13:35:43 +0100412
413 // for x86, exclude libgcc_eh.a for the same reasons as above
414 arch: {
415 x86: {
416 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
417 },
418 x86_64: {
419 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
420 },
421 },
dimitry581723e2018-01-05 14:31:44 +0100422
423 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100424 cflags: [
425 "-Wall",
426 "-Wextra",
427 "-Wunused",
428 "-Werror",
429 ],
430 stl: "none",
431
432 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700433 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800434 ramdisk_available: true,
Yifan Hongb04490d2020-10-21 18:36:36 -0700435 vendor_ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900436 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200437 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100438
Ryan Prichard470b6662018-03-27 22:10:55 -0700439 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100440 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100441 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100442
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800443 // Opt out of native_coverage when opting out of system_shared_libs
444 native_coverage: false,
445
dimitry11da1dc2018-01-05 13:35:43 +0100446 sanitize: {
447 never: true,
448 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900449
450 apex_available: [
451 "//apex_available:platform",
452 "com.android.runtime",
453 ],
Yi Kong15a05a72020-09-22 00:56:13 +0800454
455 lto: {
456 never: true,
457 },
dimitry11da1dc2018-01-05 13:35:43 +0100458}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800459
460cc_test {
461 name: "linker-unit-tests",
Colin Cross0cc60af2021-09-30 14:04:39 -0700462 test_suites: ["device-tests"],
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800463
464 cflags: [
465 "-g",
466 "-Wall",
467 "-Wextra",
468 "-Wunused",
469 "-Werror",
470 ],
471
472 // We need to access Bionic private headers in the linker.
473 include_dirs: ["bionic/libc"],
474
475 srcs: [
476 // Tests.
477 "linker_block_allocator_test.cpp",
478 "linker_config_test.cpp",
479 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100480 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800481 "linker_sleb128_test.cpp",
482 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800483 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800484
485 // Parts of the linker that we're testing.
486 "linker_block_allocator.cpp",
487 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800488 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100489 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800490 "linker_test_globals.cpp",
491 "linker_utils.cpp",
492 ],
493
494 static_libs: [
495 "libasync_safe",
496 "libbase",
497 "liblog",
498 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800499
500 arch: {
501 arm: {
502 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
503 },
504 arm64: {
505 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
506 },
507 },
508}
509
510cc_benchmark {
511 name: "linker-benchmarks",
512
513 srcs: [
514 "linker_gnu_hash_benchmark.cpp",
515 ],
516
517 arch: {
518 arm: {
519 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
520 },
521 arm64: {
522 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
523 },
524 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800525}