blob: acdf0942f278136ffd4a53abdb824b4693365f5f [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
49 // We need to access Bionic private headers in the linker.
50 include_dirs: ["bionic/libc"],
51}
52
Ryan Prichard80e40f02019-10-31 19:54:46 -070053// ========================================================
54// linker default configuration
55// ========================================================
56
57// Configuration for the linker binary and any of its static libraries.
58cc_defaults {
59 name: "linker_defaults",
60 arch: {
61 arm: {
62 cflags: ["-D__work_around_b_24465209__"],
63 },
64 x86: {
65 cflags: ["-D__work_around_b_24465209__"],
66 },
67 },
68
69 cflags: [
70 "-fno-stack-protector",
71 "-Wstrict-overflow=5",
72 "-fvisibility=hidden",
73 "-Wall",
74 "-Wextra",
75 "-Wunused",
76 "-Werror",
77 ],
78
79 // TODO: split out the asflags.
80 asflags: [
81 "-fno-stack-protector",
82 "-Wstrict-overflow=5",
83 "-fvisibility=hidden",
84 "-Wall",
85 "-Wextra",
86 "-Wunused",
87 "-Werror",
88 ],
89
90 product_variables: {
91 debuggable: {
92 cppflags: ["-DUSE_LD_CONFIG_FILE"],
93 },
94 },
95
96 cppflags: ["-Wold-style-cast"],
97
98 static_libs: [
99 "libziparchive",
100 "libbase",
101 "libz",
102
103 "libasync_safe",
104
105 "liblog",
106 ],
107
108 // We need to access Bionic private headers in the linker.
109 include_dirs: ["bionic/libc"],
110}
111
112// ========================================================
113// linker components
114// ========================================================
115
116// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
117// native bridge implementations).
118cc_defaults {
119 name: "linker_all_targets",
120 defaults: ["linux_bionic_supported"],
121 recovery_available: true,
122 native_bridge_supported: true,
123}
124
125cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700126 name: "liblinker_main",
127 defaults: ["linker_defaults", "linker_all_targets"],
128 srcs: ["linker_main.cpp"],
129
130 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
131 cflags: ["-ffreestanding"],
132}
133
134cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700135 name: "liblinker_malloc",
136 defaults: ["linker_defaults", "linker_all_targets"],
137 srcs: ["linker_memory.cpp"],
138}
139
140cc_library_static {
141 name: "liblinker_debuggerd_stub",
142 defaults: ["linker_defaults", "linker_all_targets"],
143 srcs: ["linker_debuggerd_stub.cpp"],
144}
145
146// ========================================================
147// template for the linker binary
148// ========================================================
149
dimitryb8b3a762018-09-25 12:31:11 +0200150filegroup {
151 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700152 srcs: [
153 "dlfcn.cpp",
154 "linker.cpp",
155 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700156 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700157 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800158 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800159 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700160 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700161 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700162 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800163 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700164 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700165 "linker_logger.cpp",
166 "linker_mapped_file_fragment.cpp",
167 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800168 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700169 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700170 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800171 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700172 "linker_utils.cpp",
173 "rt.cpp",
174 ],
dimitryb8b3a762018-09-25 12:31:11 +0200175}
Colin Cross97f0aef2016-07-14 16:05:46 -0700176
dimitryb8b3a762018-09-25 12:31:11 +0200177filegroup {
178 name: "linker_sources_arm",
179 srcs: [
180 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800181 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200182 ],
183}
184
185filegroup {
186 name: "linker_sources_arm64",
187 srcs: [
188 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800189 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800190 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200191 ],
192}
193
194filegroup {
195 name: "linker_sources_x86",
196 srcs: [
197 "arch/x86/begin.S",
198 ],
199}
200
201filegroup {
202 name: "linker_sources_x86_64",
203 srcs: [
204 "arch/x86_64/begin.S",
205 ],
206}
207
208filegroup {
209 name: "linker_sources_mips",
210 srcs: [
211 "arch/mips/begin.S",
212 "linker_mips.cpp",
213 ],
214}
215
216filegroup {
217 name: "linker_sources_mips64",
218 srcs: [
219 "arch/mips64/begin.S",
220 "linker_mips.cpp",
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" },
231 mips: { version_script: "linker.generic.map" },
232 mips64: { version_script: "linker.generic.map" },
233 },
234}
235
236// A template for the linker binary. May be inherited by native bridge implementations.
237cc_defaults {
238 name: "linker_bin_template",
239 defaults: ["linker_defaults"],
240
241 srcs: [":linker_sources"],
242
Colin Cross97f0aef2016-07-14 16:05:46 -0700243 arch: {
244 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700245 srcs: [":linker_sources_arm"],
246 static_libs: ["libunwind_llvm"],
247 },
248 arm64: {
249 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800250 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700251 },
252 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700253 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800254 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700255 },
256 x86_64: {
257 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800258 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700259 },
260 mips: {
261 srcs: [":linker_sources_mips"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800262 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700263 },
264 mips64: {
265 srcs: [":linker_sources_mips64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800266 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700267 },
268 },
269
Ryan Prichard80e40f02019-10-31 19:54:46 -0700270 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
271 // static_executable. This dynamic linker is actually a shared object linked with static
272 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700273 ldflags: [
274 "-shared",
275 "-Wl,-Bsymbolic",
276 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100277 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700278 ],
279
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800280 // we are going to link libc++_static manually because
281 // when stl is not set to "none" build system adds libdl
282 // to the list of static libraries which needs to be
283 // avoided in the case of building loader.
284 stl: "none",
285
Colin Cross97f0aef2016-07-14 16:05:46 -0700286 // we don't want crtbegin.o (because we have begin.o), so unset it
287 // just for this module
288 nocrt: true,
289
dimitryb8b3a762018-09-25 12:31:11 +0200290 static_executable: true,
291
292 // Leave the symbols in the shared library so that stack unwinders can produce
293 // meaningful name resolution.
294 strip: {
295 keep_symbols: true,
296 },
297
298 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
299 // looking up symbols in the linker by mistake.
300 prefix_symbols: "__dl_",
301
302 sanitize: {
303 hwaddress: false,
304 },
dimitryb8b3a762018-09-25 12:31:11 +0200305
Colin Cross97f0aef2016-07-14 16:05:46 -0700306 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700307 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700308 "liblinker_malloc",
309
310 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700311 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700312 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800313 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700314 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700315
Ryan Prichardd9a41152019-10-14 16:58:53 -0700316 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
317 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
318 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
319 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
320 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
321 //
322 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
323 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
324 // linker stops linking against libgcc.a's arm32 unwinder.
325 whole_static_libs: ["libc_unwind_static"],
326
Ryan Prichard80e40f02019-10-31 19:54:46 -0700327 system_shared_libs: [],
328
329 // Opt out of native_coverage when opting out of system_shared_libs
330 native_coverage: false,
331}
332
333// ========================================================
334// linker[_asan][64] binary
335// ========================================================
336
337cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700338 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700339 defaults: [
340 "linker_bin_template",
341 "linux_bionic_supported",
342 "linker_version_script_overlay",
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",
353 xom: false,
354
355 recovery_available: true,
356 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 ],
375 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700376 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700377}
dimitry11da1dc2018-01-05 13:35:43 +0100378
Ryan Prichard80e40f02019-10-31 19:54:46 -0700379// ========================================================
380// assorted modules
381// ========================================================
382
Elliott Hughes90f96b92019-05-09 15:56:39 -0700383sh_binary {
384 name: "ldd",
385 src: "ldd",
386}
387
dimitry11da1dc2018-01-05 13:35:43 +0100388cc_library {
389 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
390 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
391 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
392 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
393 // we use this property to make sure libc.so has its own copy of the code from
394 // libgcc.a it uses.
395 //
396 // DO NOT REMOVE --exclude-libs!
397
Yi Kong7786a342018-08-31 19:01:56 -0700398 ldflags: [
399 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700400 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700401 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
402 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
403 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
404 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
405 ],
dimitry11da1dc2018-01-05 13:35:43 +0100406
407 // for x86, exclude libgcc_eh.a for the same reasons as above
408 arch: {
409 x86: {
410 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
411 },
412 x86_64: {
413 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
414 },
415 },
dimitry581723e2018-01-05 14:31:44 +0100416
417 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100418 cflags: [
419 "-Wall",
420 "-Wextra",
421 "-Wunused",
422 "-Werror",
423 ],
424 stl: "none",
425
426 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700427 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Jiyong Park5603c6e2018-04-27 21:53:11 +0900428 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200429 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100430
Ryan Prichard470b6662018-03-27 22:10:55 -0700431 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100432 system_shared_libs: [],
433
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800434 // Opt out of native_coverage when opting out of system_shared_libs
435 native_coverage: false,
436
dimitry11da1dc2018-01-05 13:35:43 +0100437 sanitize: {
438 never: true,
439 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900440
441 apex_available: [
442 "//apex_available:platform",
443 "com.android.runtime",
444 ],
dimitry11da1dc2018-01-05 13:35:43 +0100445}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800446
447cc_test {
448 name: "linker-unit-tests",
449
450 cflags: [
451 "-g",
452 "-Wall",
453 "-Wextra",
454 "-Wunused",
455 "-Werror",
456 ],
457
458 // We need to access Bionic private headers in the linker.
459 include_dirs: ["bionic/libc"],
460
461 srcs: [
462 // Tests.
463 "linker_block_allocator_test.cpp",
464 "linker_config_test.cpp",
465 "linked_list_test.cpp",
466 "linker_sleb128_test.cpp",
467 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800468 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800469
470 // Parts of the linker that we're testing.
471 "linker_block_allocator.cpp",
472 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800473 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800474 "linker_test_globals.cpp",
475 "linker_utils.cpp",
476 ],
477
478 static_libs: [
479 "libasync_safe",
480 "libbase",
481 "liblog",
482 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800483
484 arch: {
485 arm: {
486 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
487 },
488 arm64: {
489 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
490 },
491 },
492}
493
494cc_benchmark {
495 name: "linker-benchmarks",
496
497 srcs: [
498 "linker_gnu_hash_benchmark.cpp",
499 ],
500
501 arch: {
502 arm: {
503 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
504 },
505 arm64: {
506 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
507 },
508 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800509}