blob: 4be080b1ebdc420d92c5f4d5e2796c0f15c189e5 [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
dimitryb8b3a762018-09-25 12:31:11 +0200208cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700209 name: "linker_version_script_overlay",
210 arch: {
211 arm: { version_script: "linker.arm.map" },
212 arm64: { version_script: "linker.generic.map" },
213 x86: { version_script: "linker.generic.map" },
214 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700215 },
216}
217
218// A template for the linker binary. May be inherited by native bridge implementations.
219cc_defaults {
220 name: "linker_bin_template",
221 defaults: ["linker_defaults"],
222
223 srcs: [":linker_sources"],
224
Colin Cross97f0aef2016-07-14 16:05:46 -0700225 arch: {
226 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700227 srcs: [":linker_sources_arm"],
228 static_libs: ["libunwind_llvm"],
229 },
230 arm64: {
231 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800232 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700233 },
234 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700235 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800236 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700237 },
238 x86_64: {
239 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800240 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700241 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700242 },
243
Ryan Prichard80e40f02019-10-31 19:54:46 -0700244 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
245 // static_executable. This dynamic linker is actually a shared object linked with static
246 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700247 ldflags: [
248 "-shared",
249 "-Wl,-Bsymbolic",
250 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100251 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700252 ],
253
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800254 // we are going to link libc++_static manually because
255 // when stl is not set to "none" build system adds libdl
256 // to the list of static libraries which needs to be
257 // avoided in the case of building loader.
258 stl: "none",
259
Colin Cross97f0aef2016-07-14 16:05:46 -0700260 // we don't want crtbegin.o (because we have begin.o), so unset it
261 // just for this module
262 nocrt: true,
263
dimitryb8b3a762018-09-25 12:31:11 +0200264 static_executable: true,
265
266 // Leave the symbols in the shared library so that stack unwinders can produce
267 // meaningful name resolution.
268 strip: {
269 keep_symbols: true,
270 },
271
272 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
273 // looking up symbols in the linker by mistake.
274 prefix_symbols: "__dl_",
275
276 sanitize: {
277 hwaddress: false,
278 },
dimitryb8b3a762018-09-25 12:31:11 +0200279
Colin Cross97f0aef2016-07-14 16:05:46 -0700280 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700281 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700282 "liblinker_malloc",
283
284 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700285 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700286 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800287 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700288 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700289
Ryan Prichardd9a41152019-10-14 16:58:53 -0700290 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
291 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
292 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
293 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
294 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
295 //
296 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
297 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
298 // linker stops linking against libgcc.a's arm32 unwinder.
299 whole_static_libs: ["libc_unwind_static"],
300
Ryan Prichard80e40f02019-10-31 19:54:46 -0700301 system_shared_libs: [],
302
303 // Opt out of native_coverage when opting out of system_shared_libs
304 native_coverage: false,
305}
306
307// ========================================================
308// linker[_asan][64] binary
309// ========================================================
310
311cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700312 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700313 defaults: [
314 "linker_bin_template",
315 "linux_bionic_supported",
316 "linker_version_script_overlay",
317 ],
318
Colin Cross10fffb42016-12-08 09:57:35 -0800319 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700320 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700321 lib64: {
322 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800323 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700324 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800325
Ryan Prichard80e40f02019-10-31 19:54:46 -0700326 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700327
328 recovery_available: true,
329 apex_available: [
330 "//apex_available:platform",
331 "com.android.runtime",
332 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800333
Colin Cross97f0aef2016-07-14 16:05:46 -0700334 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800335 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700336 srcs: [
337 "linker_debuggerd_android.cpp",
338 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700339 static_libs: [
340 "libc++demangle",
341 "libdebuggerd_handler_fallback",
342 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800343 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700344 linux_bionic: {
345 static_libs: [
346 "liblinker_debuggerd_stub",
347 ],
348 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700349 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700350}
dimitry11da1dc2018-01-05 13:35:43 +0100351
Ryan Prichard80e40f02019-10-31 19:54:46 -0700352// ========================================================
353// assorted modules
354// ========================================================
355
Elliott Hughes90f96b92019-05-09 15:56:39 -0700356sh_binary {
357 name: "ldd",
358 src: "ldd",
359}
360
dimitry11da1dc2018-01-05 13:35:43 +0100361cc_library {
362 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
363 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
364 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
365 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
366 // we use this property to make sure libc.so has its own copy of the code from
367 // libgcc.a it uses.
368 //
369 // DO NOT REMOVE --exclude-libs!
370
Yi Kong7786a342018-08-31 19:01:56 -0700371 ldflags: [
372 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700373 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700374 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
375 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
376 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
377 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
378 ],
dimitry11da1dc2018-01-05 13:35:43 +0100379
380 // for x86, exclude libgcc_eh.a for the same reasons as above
381 arch: {
382 x86: {
383 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
384 },
385 x86_64: {
386 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
387 },
388 },
dimitry581723e2018-01-05 14:31:44 +0100389
390 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100391 cflags: [
392 "-Wall",
393 "-Wextra",
394 "-Wunused",
395 "-Werror",
396 ],
397 stl: "none",
398
399 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700400 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800401 ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900402 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200403 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100404
Ryan Prichard470b6662018-03-27 22:10:55 -0700405 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100406 system_shared_libs: [],
407
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800408 // Opt out of native_coverage when opting out of system_shared_libs
409 native_coverage: false,
410
dimitry11da1dc2018-01-05 13:35:43 +0100411 sanitize: {
412 never: true,
413 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900414
415 apex_available: [
416 "//apex_available:platform",
417 "com.android.runtime",
418 ],
dimitry11da1dc2018-01-05 13:35:43 +0100419}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800420
421cc_test {
422 name: "linker-unit-tests",
423
424 cflags: [
425 "-g",
426 "-Wall",
427 "-Wextra",
428 "-Wunused",
429 "-Werror",
430 ],
431
432 // We need to access Bionic private headers in the linker.
433 include_dirs: ["bionic/libc"],
434
435 srcs: [
436 // Tests.
437 "linker_block_allocator_test.cpp",
438 "linker_config_test.cpp",
439 "linked_list_test.cpp",
440 "linker_sleb128_test.cpp",
441 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800442 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800443
444 // Parts of the linker that we're testing.
445 "linker_block_allocator.cpp",
446 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800447 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800448 "linker_test_globals.cpp",
449 "linker_utils.cpp",
450 ],
451
452 static_libs: [
453 "libasync_safe",
454 "libbase",
455 "liblog",
456 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800457
458 arch: {
459 arm: {
460 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
461 },
462 arm64: {
463 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
464 },
465 },
466}
467
468cc_benchmark {
469 name: "linker-benchmarks",
470
471 srcs: [
472 "linker_gnu_hash_benchmark.cpp",
473 ],
474
475 arch: {
476 arm: {
477 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
478 },
479 arm64: {
480 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
481 },
482 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800483}