blob: 582a9e41e971ebd0dd7d50c6b5e09dc37f5a6348 [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",
LuK1337fd5fa532020-10-15 17:19:52 +020060 defaults: [
61 "process_sdk_version_overrides_defaults",
62 "shim_libs_defaults",
63 ],
Ryan Prichard80e40f02019-10-31 19:54:46 -070064 arch: {
65 arm: {
66 cflags: ["-D__work_around_b_24465209__"],
67 },
68 x86: {
69 cflags: ["-D__work_around_b_24465209__"],
70 },
71 },
72
73 cflags: [
74 "-fno-stack-protector",
75 "-Wstrict-overflow=5",
76 "-fvisibility=hidden",
77 "-Wall",
78 "-Wextra",
79 "-Wunused",
80 "-Werror",
81 ],
82
83 // TODO: split out the asflags.
84 asflags: [
85 "-fno-stack-protector",
86 "-Wstrict-overflow=5",
87 "-fvisibility=hidden",
88 "-Wall",
89 "-Wextra",
90 "-Wunused",
91 "-Werror",
92 ],
93
94 product_variables: {
95 debuggable: {
96 cppflags: ["-DUSE_LD_CONFIG_FILE"],
97 },
98 },
99
100 cppflags: ["-Wold-style-cast"],
101
102 static_libs: [
103 "libziparchive",
104 "libbase",
105 "libz",
106
107 "libasync_safe",
108
109 "liblog",
110 ],
111
112 // We need to access Bionic private headers in the linker.
113 include_dirs: ["bionic/libc"],
114}
115
116// ========================================================
117// linker components
118// ========================================================
119
120// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
121// native bridge implementations).
122cc_defaults {
123 name: "linker_all_targets",
124 defaults: ["linux_bionic_supported"],
125 recovery_available: true,
126 native_bridge_supported: true,
127}
128
129cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700130 name: "liblinker_main",
131 defaults: ["linker_defaults", "linker_all_targets"],
132 srcs: ["linker_main.cpp"],
133
134 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
135 cflags: ["-ffreestanding"],
136}
137
138cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700139 name: "liblinker_malloc",
140 defaults: ["linker_defaults", "linker_all_targets"],
141 srcs: ["linker_memory.cpp"],
142}
143
144cc_library_static {
145 name: "liblinker_debuggerd_stub",
146 defaults: ["linker_defaults", "linker_all_targets"],
147 srcs: ["linker_debuggerd_stub.cpp"],
148}
149
150// ========================================================
151// template for the linker binary
152// ========================================================
153
dimitryb8b3a762018-09-25 12:31:11 +0200154filegroup {
155 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700156 srcs: [
157 "dlfcn.cpp",
158 "linker.cpp",
159 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700160 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700161 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800162 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800163 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700164 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700165 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700166 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800167 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700168 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700169 "linker_logger.cpp",
170 "linker_mapped_file_fragment.cpp",
171 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800172 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700173 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700174 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800175 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700176 "linker_utils.cpp",
177 "rt.cpp",
178 ],
dimitryb8b3a762018-09-25 12:31:11 +0200179}
Colin Cross97f0aef2016-07-14 16:05:46 -0700180
dimitryb8b3a762018-09-25 12:31:11 +0200181filegroup {
182 name: "linker_sources_arm",
183 srcs: [
184 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800185 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200186 ],
187}
188
189filegroup {
190 name: "linker_sources_arm64",
191 srcs: [
192 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800193 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800194 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200195 ],
196}
197
198filegroup {
199 name: "linker_sources_x86",
200 srcs: [
201 "arch/x86/begin.S",
202 ],
203}
204
205filegroup {
206 name: "linker_sources_x86_64",
207 srcs: [
208 "arch/x86_64/begin.S",
209 ],
210}
211
dimitryb8b3a762018-09-25 12:31:11 +0200212cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700213 name: "linker_version_script_overlay",
214 arch: {
215 arm: { version_script: "linker.arm.map" },
216 arm64: { version_script: "linker.generic.map" },
217 x86: { version_script: "linker.generic.map" },
218 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700219 },
220}
221
222// A template for the linker binary. May be inherited by native bridge implementations.
223cc_defaults {
224 name: "linker_bin_template",
225 defaults: ["linker_defaults"],
226
227 srcs: [":linker_sources"],
228
Colin Cross97f0aef2016-07-14 16:05:46 -0700229 arch: {
230 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700231 srcs: [":linker_sources_arm"],
232 static_libs: ["libunwind_llvm"],
233 },
234 arm64: {
235 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800236 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700237 },
238 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700239 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800240 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700241 },
242 x86_64: {
243 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800244 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700245 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700246 },
247
Ryan Prichard80e40f02019-10-31 19:54:46 -0700248 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
249 // static_executable. This dynamic linker is actually a shared object linked with static
250 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700251 ldflags: [
252 "-shared",
253 "-Wl,-Bsymbolic",
254 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100255 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700256 ],
257
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800258 // we are going to link libc++_static manually because
259 // when stl is not set to "none" build system adds libdl
260 // to the list of static libraries which needs to be
261 // avoided in the case of building loader.
262 stl: "none",
263
Colin Cross97f0aef2016-07-14 16:05:46 -0700264 // we don't want crtbegin.o (because we have begin.o), so unset it
265 // just for this module
266 nocrt: true,
267
dimitryb8b3a762018-09-25 12:31:11 +0200268 static_executable: true,
269
270 // Leave the symbols in the shared library so that stack unwinders can produce
271 // meaningful name resolution.
272 strip: {
273 keep_symbols: true,
274 },
275
276 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
277 // looking up symbols in the linker by mistake.
278 prefix_symbols: "__dl_",
279
280 sanitize: {
281 hwaddress: false,
282 },
dimitryb8b3a762018-09-25 12:31:11 +0200283
Colin Cross97f0aef2016-07-14 16:05:46 -0700284 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700285 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700286 "liblinker_malloc",
287
288 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700289 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700290 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800291 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700292 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700293
Ryan Prichardd9a41152019-10-14 16:58:53 -0700294 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
295 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
296 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
297 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
298 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
299 //
300 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
301 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
302 // linker stops linking against libgcc.a's arm32 unwinder.
303 whole_static_libs: ["libc_unwind_static"],
304
Ryan Prichard80e40f02019-10-31 19:54:46 -0700305 system_shared_libs: [],
306
307 // Opt out of native_coverage when opting out of system_shared_libs
308 native_coverage: false,
309}
310
311// ========================================================
312// linker[_asan][64] binary
313// ========================================================
314
315cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700316 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700317 defaults: [
318 "linker_bin_template",
319 "linux_bionic_supported",
320 "linker_version_script_overlay",
321 ],
322
Victor Khimenkoeb6c7ab2020-05-14 22:14:45 +0200323 srcs: [
324 "linker_translate_path.cpp",
325 ],
326
Colin Cross10fffb42016-12-08 09:57:35 -0800327 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700328 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700329 lib64: {
330 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800331 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700332 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800333
Ryan Prichard80e40f02019-10-31 19:54:46 -0700334 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700335
336 recovery_available: true,
337 apex_available: [
338 "//apex_available:platform",
339 "com.android.runtime",
340 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800341
Colin Cross97f0aef2016-07-14 16:05:46 -0700342 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800343 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700344 srcs: [
345 "linker_debuggerd_android.cpp",
346 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700347 static_libs: [
348 "libc++demangle",
349 "libdebuggerd_handler_fallback",
350 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800351 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700352 linux_bionic: {
353 static_libs: [
354 "liblinker_debuggerd_stub",
355 ],
356 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700357 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700358}
dimitry11da1dc2018-01-05 13:35:43 +0100359
Ryan Prichard80e40f02019-10-31 19:54:46 -0700360// ========================================================
361// assorted modules
362// ========================================================
363
Elliott Hughes90f96b92019-05-09 15:56:39 -0700364sh_binary {
365 name: "ldd",
366 src: "ldd",
367}
368
dimitry11da1dc2018-01-05 13:35:43 +0100369cc_library {
370 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
371 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
372 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
373 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
374 // we use this property to make sure libc.so has its own copy of the code from
375 // libgcc.a it uses.
376 //
377 // DO NOT REMOVE --exclude-libs!
378
Yi Kong7786a342018-08-31 19:01:56 -0700379 ldflags: [
380 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700381 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700382 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
383 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
384 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
385 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
386 ],
dimitry11da1dc2018-01-05 13:35:43 +0100387
388 // for x86, exclude libgcc_eh.a for the same reasons as above
389 arch: {
390 x86: {
391 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
392 },
393 x86_64: {
394 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
395 },
396 },
dimitry581723e2018-01-05 14:31:44 +0100397
398 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100399 cflags: [
400 "-Wall",
401 "-Wextra",
402 "-Wunused",
403 "-Werror",
404 ],
405 stl: "none",
406
407 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700408 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800409 ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900410 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200411 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100412
Ryan Prichard470b6662018-03-27 22:10:55 -0700413 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100414 system_shared_libs: [],
415
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800416 // Opt out of native_coverage when opting out of system_shared_libs
417 native_coverage: false,
418
dimitry11da1dc2018-01-05 13:35:43 +0100419 sanitize: {
420 never: true,
421 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900422
423 apex_available: [
424 "//apex_available:platform",
425 "com.android.runtime",
426 ],
dimitry11da1dc2018-01-05 13:35:43 +0100427}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800428
429cc_test {
430 name: "linker-unit-tests",
431
432 cflags: [
433 "-g",
434 "-Wall",
435 "-Wextra",
436 "-Wunused",
437 "-Werror",
438 ],
439
440 // We need to access Bionic private headers in the linker.
441 include_dirs: ["bionic/libc"],
442
443 srcs: [
444 // Tests.
445 "linker_block_allocator_test.cpp",
446 "linker_config_test.cpp",
447 "linked_list_test.cpp",
448 "linker_sleb128_test.cpp",
449 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800450 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800451
452 // Parts of the linker that we're testing.
453 "linker_block_allocator.cpp",
454 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800455 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800456 "linker_test_globals.cpp",
457 "linker_utils.cpp",
458 ],
459
460 static_libs: [
461 "libasync_safe",
462 "libbase",
463 "liblog",
464 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800465
466 arch: {
467 arm: {
468 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
469 },
470 arm64: {
471 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
472 },
473 },
474}
475
476cc_benchmark {
477 name: "linker-benchmarks",
478
479 srcs: [
480 "linker_gnu_hash_benchmark.cpp",
481 ],
482
483 arch: {
484 arm: {
485 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
486 },
487 arm64: {
488 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
489 },
490 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800491}