blob: 6c59cffdd8ee112c32447cc41e0f163730df0f1f [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.
12cc_object {
13 name: "linker_wrapper",
14 host_supported: true,
15 device_supported: false,
16 target: {
Elliott Hughesd50a1de2018-02-05 17:30:57 -080017 linux_bionic: {
18 enabled: true,
19 },
20 linux_glibc: {
21 enabled: false,
22 },
23 darwin: {
24 enabled: false,
25 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070026 },
27
28 cflags: [
29 "-fno-stack-protector",
30 "-Wstrict-overflow=5",
31 "-fvisibility=hidden",
32 "-Wall",
33 "-Wextra",
34 "-Wno-unused",
35 "-Werror",
36 ],
37
38 srcs: [
39 "linker_wrapper.cpp",
40 ],
41 arch: {
Jiyong Park3b47d602020-09-18 16:15:53 +090042 arm64: {
43 srcs: ["arch/arm64/begin.S"],
44 },
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070045 x86_64: {
46 srcs: ["arch/x86_64/begin.S"],
47 },
48 },
49
50 prefix_symbols: "__dlwrap_",
51
Martin Stjernholm82d84bc2020-04-06 20:32:09 +010052 header_libs: ["libc_headers"],
53
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070054 // We need to access Bionic private headers in the linker.
55 include_dirs: ["bionic/libc"],
56}
57
Ryan Prichard80e40f02019-10-31 19:54:46 -070058// ========================================================
59// linker default configuration
60// ========================================================
61
62// Configuration for the linker binary and any of its static libraries.
63cc_defaults {
64 name: "linker_defaults",
65 arch: {
66 arm: {
67 cflags: ["-D__work_around_b_24465209__"],
68 },
69 x86: {
70 cflags: ["-D__work_around_b_24465209__"],
71 },
72 },
73
74 cflags: [
75 "-fno-stack-protector",
76 "-Wstrict-overflow=5",
77 "-fvisibility=hidden",
78 "-Wall",
79 "-Wextra",
80 "-Wunused",
81 "-Werror",
82 ],
83
84 // TODO: split out the asflags.
85 asflags: [
86 "-fno-stack-protector",
87 "-Wstrict-overflow=5",
88 "-fvisibility=hidden",
89 "-Wall",
90 "-Wextra",
91 "-Wunused",
92 "-Werror",
93 ],
94
95 product_variables: {
96 debuggable: {
97 cppflags: ["-DUSE_LD_CONFIG_FILE"],
98 },
99 },
100
101 cppflags: ["-Wold-style-cast"],
102
103 static_libs: [
104 "libziparchive",
105 "libbase",
106 "libz",
107
108 "libasync_safe",
109
110 "liblog",
111 ],
112
113 // We need to access Bionic private headers in the linker.
114 include_dirs: ["bionic/libc"],
115}
116
117// ========================================================
118// linker components
119// ========================================================
120
121// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
122// native bridge implementations).
123cc_defaults {
124 name: "linker_all_targets",
125 defaults: ["linux_bionic_supported"],
126 recovery_available: true,
127 native_bridge_supported: true,
128}
129
130cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700131 name: "liblinker_main",
132 defaults: ["linker_defaults", "linker_all_targets"],
133 srcs: ["linker_main.cpp"],
134
135 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
136 cflags: ["-ffreestanding"],
137}
138
139cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700140 name: "liblinker_malloc",
141 defaults: ["linker_defaults", "linker_all_targets"],
142 srcs: ["linker_memory.cpp"],
143}
144
145cc_library_static {
146 name: "liblinker_debuggerd_stub",
147 defaults: ["linker_defaults", "linker_all_targets"],
148 srcs: ["linker_debuggerd_stub.cpp"],
149}
150
151// ========================================================
152// template for the linker binary
153// ========================================================
154
dimitryb8b3a762018-09-25 12:31:11 +0200155filegroup {
156 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700157 srcs: [
158 "dlfcn.cpp",
159 "linker.cpp",
160 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700161 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700162 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800163 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800164 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700165 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700166 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700167 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800168 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700169 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700170 "linker_logger.cpp",
171 "linker_mapped_file_fragment.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100172 "linker_note_gnu_property.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700173 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800174 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700175 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700176 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800177 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700178 "linker_utils.cpp",
179 "rt.cpp",
180 ],
dimitryb8b3a762018-09-25 12:31:11 +0200181}
Colin Cross97f0aef2016-07-14 16:05:46 -0700182
dimitryb8b3a762018-09-25 12:31:11 +0200183filegroup {
184 name: "linker_sources_arm",
185 srcs: [
186 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800187 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200188 ],
189}
190
191filegroup {
192 name: "linker_sources_arm64",
193 srcs: [
194 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800195 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800196 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200197 ],
198}
199
200filegroup {
201 name: "linker_sources_x86",
202 srcs: [
203 "arch/x86/begin.S",
204 ],
205}
206
207filegroup {
208 name: "linker_sources_x86_64",
209 srcs: [
210 "arch/x86_64/begin.S",
211 ],
212}
213
dimitryb8b3a762018-09-25 12:31:11 +0200214cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700215 name: "linker_version_script_overlay",
216 arch: {
217 arm: { version_script: "linker.arm.map" },
218 arm64: { version_script: "linker.generic.map" },
219 x86: { version_script: "linker.generic.map" },
220 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700221 },
222}
223
224// A template for the linker binary. May be inherited by native bridge implementations.
225cc_defaults {
226 name: "linker_bin_template",
227 defaults: ["linker_defaults"],
228
229 srcs: [":linker_sources"],
230
Colin Cross97f0aef2016-07-14 16:05:46 -0700231 arch: {
232 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700233 srcs: [":linker_sources_arm"],
234 static_libs: ["libunwind_llvm"],
235 },
236 arm64: {
237 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800238 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700239 },
240 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700241 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800242 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700243 },
244 x86_64: {
245 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800246 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700247 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700248 },
249
Ryan Prichard80e40f02019-10-31 19:54:46 -0700250 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
251 // static_executable. This dynamic linker is actually a shared object linked with static
252 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700253 ldflags: [
254 "-shared",
255 "-Wl,-Bsymbolic",
256 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100257 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700258 ],
259
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800260 // we are going to link libc++_static manually because
261 // when stl is not set to "none" build system adds libdl
262 // to the list of static libraries which needs to be
263 // avoided in the case of building loader.
264 stl: "none",
265
Colin Cross97f0aef2016-07-14 16:05:46 -0700266 // we don't want crtbegin.o (because we have begin.o), so unset it
267 // just for this module
268 nocrt: true,
269
dimitryb8b3a762018-09-25 12:31:11 +0200270 static_executable: true,
271
272 // Leave the symbols in the shared library so that stack unwinders can produce
273 // meaningful name resolution.
274 strip: {
275 keep_symbols: true,
276 },
277
278 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
279 // looking up symbols in the linker by mistake.
280 prefix_symbols: "__dl_",
281
282 sanitize: {
283 hwaddress: false,
284 },
dimitryb8b3a762018-09-25 12:31:11 +0200285
Colin Cross97f0aef2016-07-14 16:05:46 -0700286 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700287 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700288 "liblinker_malloc",
289
290 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700291 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700292 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800293 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700294 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700295
Ryan Prichardd9a41152019-10-14 16:58:53 -0700296 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
297 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
298 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
299 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
300 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
301 //
302 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
303 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
304 // linker stops linking against libgcc.a's arm32 unwinder.
305 whole_static_libs: ["libc_unwind_static"],
306
Ryan Prichard80e40f02019-10-31 19:54:46 -0700307 system_shared_libs: [],
308
309 // Opt out of native_coverage when opting out of system_shared_libs
310 native_coverage: false,
311}
312
313// ========================================================
314// linker[_asan][64] binary
315// ========================================================
316
317cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700318 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700319 defaults: [
320 "linker_bin_template",
321 "linux_bionic_supported",
322 "linker_version_script_overlay",
323 ],
324
Victor Khimenkod15229d2020-05-14 22:14:45 +0200325 srcs: [
326 "linker_translate_path.cpp",
327 ],
328
Colin Cross10fffb42016-12-08 09:57:35 -0800329 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700330 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700331 lib64: {
332 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800333 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700334 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800335
Ryan Prichard80e40f02019-10-31 19:54:46 -0700336 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700337
338 recovery_available: true,
339 apex_available: [
340 "//apex_available:platform",
341 "com.android.runtime",
342 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800343
Colin Cross97f0aef2016-07-14 16:05:46 -0700344 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800345 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700346 srcs: [
347 "linker_debuggerd_android.cpp",
348 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700349 static_libs: [
350 "libc++demangle",
351 "libdebuggerd_handler_fallback",
352 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800353 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700354 linux_bionic: {
355 static_libs: [
356 "liblinker_debuggerd_stub",
357 ],
358 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700359 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700360}
dimitry11da1dc2018-01-05 13:35:43 +0100361
Ryan Prichard80e40f02019-10-31 19:54:46 -0700362// ========================================================
363// assorted modules
364// ========================================================
365
Elliott Hughes90f96b92019-05-09 15:56:39 -0700366sh_binary {
367 name: "ldd",
368 src: "ldd",
369}
370
dimitry11da1dc2018-01-05 13:35:43 +0100371cc_library {
372 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
373 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
374 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
375 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
376 // we use this property to make sure libc.so has its own copy of the code from
377 // libgcc.a it uses.
378 //
379 // DO NOT REMOVE --exclude-libs!
380
Yi Kong7786a342018-08-31 19:01:56 -0700381 ldflags: [
382 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700383 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700384 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
385 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
386 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
387 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
388 ],
dimitry11da1dc2018-01-05 13:35:43 +0100389
390 // for x86, exclude libgcc_eh.a for the same reasons as above
391 arch: {
392 x86: {
393 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
394 },
395 x86_64: {
396 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
397 },
398 },
dimitry581723e2018-01-05 14:31:44 +0100399
400 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100401 cflags: [
402 "-Wall",
403 "-Wextra",
404 "-Wunused",
405 "-Werror",
406 ],
407 stl: "none",
408
409 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700410 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800411 ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900412 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200413 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100414
Ryan Prichard470b6662018-03-27 22:10:55 -0700415 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100416 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100417 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100418
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800419 // Opt out of native_coverage when opting out of system_shared_libs
420 native_coverage: false,
421
dimitry11da1dc2018-01-05 13:35:43 +0100422 sanitize: {
423 never: true,
424 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900425
426 apex_available: [
427 "//apex_available:platform",
428 "com.android.runtime",
429 ],
dimitry11da1dc2018-01-05 13:35:43 +0100430}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800431
432cc_test {
433 name: "linker-unit-tests",
434
435 cflags: [
436 "-g",
437 "-Wall",
438 "-Wextra",
439 "-Wunused",
440 "-Werror",
441 ],
442
443 // We need to access Bionic private headers in the linker.
444 include_dirs: ["bionic/libc"],
445
446 srcs: [
447 // Tests.
448 "linker_block_allocator_test.cpp",
449 "linker_config_test.cpp",
450 "linked_list_test.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100451 "linker_note_gnu_property_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800452 "linker_sleb128_test.cpp",
453 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800454 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800455
456 // Parts of the linker that we're testing.
457 "linker_block_allocator.cpp",
458 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800459 "linker_debug.cpp",
Tamas Petz8d55d182020-02-24 14:15:25 +0100460 "linker_note_gnu_property.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800461 "linker_test_globals.cpp",
462 "linker_utils.cpp",
463 ],
464
465 static_libs: [
466 "libasync_safe",
467 "libbase",
468 "liblog",
469 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800470
471 arch: {
472 arm: {
473 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
474 },
475 arm64: {
476 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
477 },
478 },
479}
480
481cc_benchmark {
482 name: "linker-benchmarks",
483
484 srcs: [
485 "linker_gnu_hash_benchmark.cpp",
486 ],
487
488 arch: {
489 arm: {
490 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
491 },
492 arm64: {
493 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
494 },
495 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800496}