blob: 08b2c7b07ba4603cbcec75d8050f9a5959ece801 [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: {
42 x86_64: {
43 srcs: ["arch/x86_64/begin.S"],
44 },
45 },
46
47 prefix_symbols: "__dlwrap_",
48
Martin Stjernholm82d84bc2020-04-06 20:32:09 +010049 header_libs: ["libc_headers"],
50
Dan Willemsen7ccc50d2017-09-18 21:28:14 -070051 // We need to access Bionic private headers in the linker.
52 include_dirs: ["bionic/libc"],
53}
54
Ryan Prichard80e40f02019-10-31 19:54:46 -070055// ========================================================
56// linker default configuration
57// ========================================================
58
59// Configuration for the linker binary and any of its static libraries.
60cc_defaults {
61 name: "linker_defaults",
62 arch: {
63 arm: {
64 cflags: ["-D__work_around_b_24465209__"],
65 },
66 x86: {
67 cflags: ["-D__work_around_b_24465209__"],
68 },
69 },
70
71 cflags: [
72 "-fno-stack-protector",
73 "-Wstrict-overflow=5",
74 "-fvisibility=hidden",
75 "-Wall",
76 "-Wextra",
77 "-Wunused",
78 "-Werror",
79 ],
80
81 // TODO: split out the asflags.
82 asflags: [
83 "-fno-stack-protector",
84 "-Wstrict-overflow=5",
85 "-fvisibility=hidden",
86 "-Wall",
87 "-Wextra",
88 "-Wunused",
89 "-Werror",
90 ],
91
92 product_variables: {
93 debuggable: {
94 cppflags: ["-DUSE_LD_CONFIG_FILE"],
95 },
96 },
97
98 cppflags: ["-Wold-style-cast"],
99
100 static_libs: [
101 "libziparchive",
102 "libbase",
103 "libz",
104
105 "libasync_safe",
106
107 "liblog",
108 ],
109
110 // We need to access Bionic private headers in the linker.
111 include_dirs: ["bionic/libc"],
112}
113
114// ========================================================
115// linker components
116// ========================================================
117
118// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
119// native bridge implementations).
120cc_defaults {
121 name: "linker_all_targets",
122 defaults: ["linux_bionic_supported"],
123 recovery_available: true,
124 native_bridge_supported: true,
125}
126
127cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700128 name: "liblinker_main",
129 defaults: ["linker_defaults", "linker_all_targets"],
130 srcs: ["linker_main.cpp"],
131
132 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
133 cflags: ["-ffreestanding"],
134}
135
136cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700137 name: "liblinker_malloc",
138 defaults: ["linker_defaults", "linker_all_targets"],
139 srcs: ["linker_memory.cpp"],
140}
141
142cc_library_static {
143 name: "liblinker_debuggerd_stub",
144 defaults: ["linker_defaults", "linker_all_targets"],
145 srcs: ["linker_debuggerd_stub.cpp"],
146}
147
148// ========================================================
149// template for the linker binary
150// ========================================================
151
dimitryb8b3a762018-09-25 12:31:11 +0200152filegroup {
153 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700154 srcs: [
155 "dlfcn.cpp",
156 "linker.cpp",
157 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700158 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700159 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800160 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800161 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700162 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700163 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700164 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800165 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700166 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700167 "linker_logger.cpp",
168 "linker_mapped_file_fragment.cpp",
169 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800170 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700171 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700172 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800173 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700174 "linker_utils.cpp",
175 "rt.cpp",
176 ],
dimitryb8b3a762018-09-25 12:31:11 +0200177}
Colin Cross97f0aef2016-07-14 16:05:46 -0700178
dimitryb8b3a762018-09-25 12:31:11 +0200179filegroup {
180 name: "linker_sources_arm",
181 srcs: [
182 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800183 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200184 ],
185}
186
187filegroup {
188 name: "linker_sources_arm64",
189 srcs: [
190 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800191 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800192 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200193 ],
194}
195
196filegroup {
197 name: "linker_sources_x86",
198 srcs: [
199 "arch/x86/begin.S",
200 ],
201}
202
203filegroup {
204 name: "linker_sources_x86_64",
205 srcs: [
206 "arch/x86_64/begin.S",
207 ],
208}
209
dimitryb8b3a762018-09-25 12:31:11 +0200210cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700211 name: "linker_version_script_overlay",
212 arch: {
213 arm: { version_script: "linker.arm.map" },
214 arm64: { version_script: "linker.generic.map" },
215 x86: { version_script: "linker.generic.map" },
216 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700217 },
218}
219
220// A template for the linker binary. May be inherited by native bridge implementations.
221cc_defaults {
222 name: "linker_bin_template",
223 defaults: ["linker_defaults"],
224
225 srcs: [":linker_sources"],
226
Colin Cross97f0aef2016-07-14 16:05:46 -0700227 arch: {
228 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700229 srcs: [":linker_sources_arm"],
230 static_libs: ["libunwind_llvm"],
231 },
232 arm64: {
233 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800234 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700235 },
236 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700237 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800238 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700239 },
240 x86_64: {
241 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800242 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700243 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700244 },
245
Ryan Prichard80e40f02019-10-31 19:54:46 -0700246 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
247 // static_executable. This dynamic linker is actually a shared object linked with static
248 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700249 ldflags: [
250 "-shared",
251 "-Wl,-Bsymbolic",
252 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100253 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700254 ],
255
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800256 // we are going to link libc++_static manually because
257 // when stl is not set to "none" build system adds libdl
258 // to the list of static libraries which needs to be
259 // avoided in the case of building loader.
260 stl: "none",
261
Colin Cross97f0aef2016-07-14 16:05:46 -0700262 // we don't want crtbegin.o (because we have begin.o), so unset it
263 // just for this module
264 nocrt: true,
265
dimitryb8b3a762018-09-25 12:31:11 +0200266 static_executable: true,
267
268 // Leave the symbols in the shared library so that stack unwinders can produce
269 // meaningful name resolution.
270 strip: {
271 keep_symbols: true,
272 },
273
274 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
275 // looking up symbols in the linker by mistake.
276 prefix_symbols: "__dl_",
277
278 sanitize: {
279 hwaddress: false,
280 },
dimitryb8b3a762018-09-25 12:31:11 +0200281
Colin Cross97f0aef2016-07-14 16:05:46 -0700282 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700283 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700284 "liblinker_malloc",
285
286 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700287 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700288 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800289 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700290 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700291
Ryan Prichardd9a41152019-10-14 16:58:53 -0700292 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
293 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
294 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
295 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
296 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
297 //
298 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
299 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
300 // linker stops linking against libgcc.a's arm32 unwinder.
301 whole_static_libs: ["libc_unwind_static"],
302
Ryan Prichard80e40f02019-10-31 19:54:46 -0700303 system_shared_libs: [],
304
305 // Opt out of native_coverage when opting out of system_shared_libs
306 native_coverage: false,
307}
308
309// ========================================================
310// linker[_asan][64] binary
311// ========================================================
312
313cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700314 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700315 defaults: [
316 "linker_bin_template",
317 "linux_bionic_supported",
318 "linker_version_script_overlay",
319 ],
320
Victor Khimenkod15229d2020-05-14 22:14:45 +0200321 srcs: [
322 "linker_translate_path.cpp",
323 ],
324
Colin Cross10fffb42016-12-08 09:57:35 -0800325 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700326 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700327 lib64: {
328 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800329 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700330 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800331
Ryan Prichard80e40f02019-10-31 19:54:46 -0700332 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700333
334 recovery_available: true,
335 apex_available: [
336 "//apex_available:platform",
337 "com.android.runtime",
338 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800339
Colin Cross97f0aef2016-07-14 16:05:46 -0700340 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800341 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700342 srcs: [
343 "linker_debuggerd_android.cpp",
344 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700345 static_libs: [
346 "libc++demangle",
347 "libdebuggerd_handler_fallback",
348 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800349 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700350 linux_bionic: {
351 static_libs: [
352 "liblinker_debuggerd_stub",
353 ],
354 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700355 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700356}
dimitry11da1dc2018-01-05 13:35:43 +0100357
Ryan Prichard80e40f02019-10-31 19:54:46 -0700358// ========================================================
359// assorted modules
360// ========================================================
361
Elliott Hughes90f96b92019-05-09 15:56:39 -0700362sh_binary {
363 name: "ldd",
364 src: "ldd",
365}
366
dimitry11da1dc2018-01-05 13:35:43 +0100367cc_library {
368 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
369 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
370 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
371 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
372 // we use this property to make sure libc.so has its own copy of the code from
373 // libgcc.a it uses.
374 //
375 // DO NOT REMOVE --exclude-libs!
376
Yi Kong7786a342018-08-31 19:01:56 -0700377 ldflags: [
378 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700379 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700380 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
381 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
382 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
383 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
384 ],
dimitry11da1dc2018-01-05 13:35:43 +0100385
386 // for x86, exclude libgcc_eh.a for the same reasons as above
387 arch: {
388 x86: {
389 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
390 },
391 x86_64: {
392 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
393 },
394 },
dimitry581723e2018-01-05 14:31:44 +0100395
396 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100397 cflags: [
398 "-Wall",
399 "-Wextra",
400 "-Wunused",
401 "-Werror",
402 ],
403 stl: "none",
404
405 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700406 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800407 ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900408 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200409 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100410
Ryan Prichard470b6662018-03-27 22:10:55 -0700411 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100412 system_shared_libs: [],
Martin Stjernholm82d84bc2020-04-06 20:32:09 +0100413 header_libs: ["libc_headers"],
dimitry11da1dc2018-01-05 13:35:43 +0100414
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800415 // Opt out of native_coverage when opting out of system_shared_libs
416 native_coverage: false,
417
dimitry11da1dc2018-01-05 13:35:43 +0100418 sanitize: {
419 never: true,
420 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900421
422 apex_available: [
423 "//apex_available:platform",
424 "com.android.runtime",
425 ],
dimitry11da1dc2018-01-05 13:35:43 +0100426}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800427
428cc_test {
429 name: "linker-unit-tests",
430
431 cflags: [
432 "-g",
433 "-Wall",
434 "-Wextra",
435 "-Wunused",
436 "-Werror",
437 ],
438
439 // We need to access Bionic private headers in the linker.
440 include_dirs: ["bionic/libc"],
441
442 srcs: [
443 // Tests.
444 "linker_block_allocator_test.cpp",
445 "linker_config_test.cpp",
446 "linked_list_test.cpp",
447 "linker_sleb128_test.cpp",
448 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800449 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800450
451 // Parts of the linker that we're testing.
452 "linker_block_allocator.cpp",
453 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800454 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800455 "linker_test_globals.cpp",
456 "linker_utils.cpp",
457 ],
458
459 static_libs: [
460 "libasync_safe",
461 "libbase",
462 "liblog",
463 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800464
465 arch: {
466 arm: {
467 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
468 },
469 arm64: {
470 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
471 },
472 },
473}
474
475cc_benchmark {
476 name: "linker-benchmarks",
477
478 srcs: [
479 "linker_gnu_hash_benchmark.cpp",
480 ],
481
482 arch: {
483 arm: {
484 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
485 },
486 arm64: {
487 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
488 },
489 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800490}