blob: 3190870ae6714aabdbead4f740223c91683f0007 [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",
Jiyong Park8dba7fe2020-03-06 14:03:35 +0900101 "libdl", // libbase uses dlsym
Ryan Prichard80e40f02019-10-31 19:54:46 -0700102 "libz",
103
104 "libasync_safe",
105
106 "liblog",
107 ],
108
109 // We need to access Bionic private headers in the linker.
110 include_dirs: ["bionic/libc"],
111}
112
113// ========================================================
114// linker components
115// ========================================================
116
117// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
118// native bridge implementations).
119cc_defaults {
120 name: "linker_all_targets",
121 defaults: ["linux_bionic_supported"],
122 recovery_available: true,
123 native_bridge_supported: true,
124}
125
126cc_library_static {
Ryan Prichard249757b2019-11-01 17:18:28 -0700127 name: "liblinker_main",
128 defaults: ["linker_defaults", "linker_all_targets"],
129 srcs: ["linker_main.cpp"],
130
131 // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
132 cflags: ["-ffreestanding"],
133}
134
135cc_library_static {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700136 name: "liblinker_malloc",
137 defaults: ["linker_defaults", "linker_all_targets"],
138 srcs: ["linker_memory.cpp"],
139}
140
141cc_library_static {
142 name: "liblinker_debuggerd_stub",
143 defaults: ["linker_defaults", "linker_all_targets"],
144 srcs: ["linker_debuggerd_stub.cpp"],
145}
146
147// ========================================================
148// template for the linker binary
149// ========================================================
150
dimitryb8b3a762018-09-25 12:31:11 +0200151filegroup {
152 name: "linker_sources",
Colin Cross97f0aef2016-07-14 16:05:46 -0700153 srcs: [
154 "dlfcn.cpp",
155 "linker.cpp",
156 "linker_block_allocator.cpp",
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700157 "linker_dlwarning.cpp",
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700158 "linker_cfi.cpp",
Dimitry Ivanov4cabfaa2017-03-07 11:19:05 -0800159 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800160 "linker_debug.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700161 "linker_gdb_support.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700162 "linker_globals.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700163 "linker_libc_support.c",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800164 "linker_libcxx_support.cpp",
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700165 "linker_namespaces.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700166 "linker_logger.cpp",
167 "linker_mapped_file_fragment.cpp",
168 "linker_phdr.cpp",
Ryan Prichard339ecef2020-01-02 16:36:06 -0800169 "linker_relocate.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700170 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700171 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800172 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700173 "linker_utils.cpp",
174 "rt.cpp",
175 ],
dimitryb8b3a762018-09-25 12:31:11 +0200176}
Colin Cross97f0aef2016-07-14 16:05:46 -0700177
dimitryb8b3a762018-09-25 12:31:11 +0200178filegroup {
179 name: "linker_sources_arm",
180 srcs: [
181 "arch/arm/begin.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800182 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200183 ],
184}
185
186filegroup {
187 name: "linker_sources_arm64",
188 srcs: [
189 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800190 "arch/arm64/tlsdesc_resolver.S",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800191 "arch/arm_neon/linker_gnu_hash_neon.cpp",
dimitryb8b3a762018-09-25 12:31:11 +0200192 ],
193}
194
195filegroup {
196 name: "linker_sources_x86",
197 srcs: [
198 "arch/x86/begin.S",
199 ],
200}
201
202filegroup {
203 name: "linker_sources_x86_64",
204 srcs: [
205 "arch/x86_64/begin.S",
206 ],
207}
208
dimitryb8b3a762018-09-25 12:31:11 +0200209cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700210 name: "linker_version_script_overlay",
211 arch: {
212 arm: { version_script: "linker.arm.map" },
213 arm64: { version_script: "linker.generic.map" },
214 x86: { version_script: "linker.generic.map" },
215 x86_64: { version_script: "linker.generic.map" },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700216 },
217}
218
219// A template for the linker binary. May be inherited by native bridge implementations.
220cc_defaults {
221 name: "linker_bin_template",
222 defaults: ["linker_defaults"],
223
224 srcs: [":linker_sources"],
225
Colin Cross97f0aef2016-07-14 16:05:46 -0700226 arch: {
227 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700228 srcs: [":linker_sources_arm"],
229 static_libs: ["libunwind_llvm"],
230 },
231 arm64: {
232 srcs: [":linker_sources_arm64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800233 static_libs: ["libgcc_stripped"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700234 },
235 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700236 srcs: [":linker_sources_x86"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800237 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700238 },
239 x86_64: {
240 srcs: [":linker_sources_x86_64"],
Peter Collingbournef2b1e032019-12-10 17:41:16 -0800241 static_libs: ["libgcc_stripped"],
Ryan Prichard80e40f02019-10-31 19:54:46 -0700242 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700243 },
244
Ryan Prichard80e40f02019-10-31 19:54:46 -0700245 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
246 // static_executable. This dynamic linker is actually a shared object linked with static
247 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700248 ldflags: [
249 "-shared",
250 "-Wl,-Bsymbolic",
251 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100252 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700253 ],
254
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800255 // we are going to link libc++_static manually because
256 // when stl is not set to "none" build system adds libdl
257 // to the list of static libraries which needs to be
258 // avoided in the case of building loader.
259 stl: "none",
260
Colin Cross97f0aef2016-07-14 16:05:46 -0700261 // we don't want crtbegin.o (because we have begin.o), so unset it
262 // just for this module
263 nocrt: true,
264
dimitryb8b3a762018-09-25 12:31:11 +0200265 static_executable: true,
266
267 // Leave the symbols in the shared library so that stack unwinders can produce
268 // meaningful name resolution.
269 strip: {
270 keep_symbols: true,
271 },
272
273 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
274 // looking up symbols in the linker by mistake.
275 prefix_symbols: "__dl_",
276
277 sanitize: {
278 hwaddress: false,
279 },
dimitryb8b3a762018-09-25 12:31:11 +0200280
Colin Cross97f0aef2016-07-14 16:05:46 -0700281 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700282 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700283 "liblinker_malloc",
284
285 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700286 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700287 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800288 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700289 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700290
Ryan Prichardd9a41152019-10-14 16:58:53 -0700291 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
292 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
293 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
294 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
295 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
296 //
297 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
298 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
299 // linker stops linking against libgcc.a's arm32 unwinder.
300 whole_static_libs: ["libc_unwind_static"],
301
Ryan Prichard80e40f02019-10-31 19:54:46 -0700302 system_shared_libs: [],
303
304 // Opt out of native_coverage when opting out of system_shared_libs
305 native_coverage: false,
306}
307
308// ========================================================
309// linker[_asan][64] binary
310// ========================================================
311
312cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700313 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700314 defaults: [
315 "linker_bin_template",
316 "linux_bionic_supported",
317 "linker_version_script_overlay",
318 ],
319
Colin Cross10fffb42016-12-08 09:57:35 -0800320 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700321 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700322 lib64: {
323 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800324 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700325 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800326
Ryan Prichard80e40f02019-10-31 19:54:46 -0700327 compile_multilib: "both",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700328
329 recovery_available: true,
330 apex_available: [
331 "//apex_available:platform",
332 "com.android.runtime",
333 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800334
Colin Cross97f0aef2016-07-14 16:05:46 -0700335 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800336 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700337 srcs: [
338 "linker_debuggerd_android.cpp",
339 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700340 static_libs: [
341 "libc++demangle",
342 "libdebuggerd_handler_fallback",
343 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800344 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700345 linux_bionic: {
346 static_libs: [
347 "liblinker_debuggerd_stub",
348 ],
349 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700350 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700351}
dimitry11da1dc2018-01-05 13:35:43 +0100352
Ryan Prichard80e40f02019-10-31 19:54:46 -0700353// ========================================================
354// assorted modules
355// ========================================================
356
Elliott Hughes90f96b92019-05-09 15:56:39 -0700357sh_binary {
358 name: "ldd",
359 src: "ldd",
360}
361
dimitry11da1dc2018-01-05 13:35:43 +0100362cc_library {
363 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
364 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
365 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
366 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
367 // we use this property to make sure libc.so has its own copy of the code from
368 // libgcc.a it uses.
369 //
370 // DO NOT REMOVE --exclude-libs!
371
Yi Kong7786a342018-08-31 19:01:56 -0700372 ldflags: [
373 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700374 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700375 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
376 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
377 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
378 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
379 ],
dimitry11da1dc2018-01-05 13:35:43 +0100380
381 // for x86, exclude libgcc_eh.a for the same reasons as above
382 arch: {
383 x86: {
384 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
385 },
386 x86_64: {
387 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
388 },
389 },
dimitry581723e2018-01-05 14:31:44 +0100390
391 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100392 cflags: [
393 "-Wall",
394 "-Wextra",
395 "-Wunused",
396 "-Werror",
397 ],
398 stl: "none",
399
400 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700401 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Yifan Hong5a39cee2020-01-21 16:43:56 -0800402 ramdisk_available: true,
Jiyong Park5603c6e2018-04-27 21:53:11 +0900403 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200404 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100405
Ryan Prichard470b6662018-03-27 22:10:55 -0700406 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100407 system_shared_libs: [],
408
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800409 // Opt out of native_coverage when opting out of system_shared_libs
410 native_coverage: false,
411
dimitry11da1dc2018-01-05 13:35:43 +0100412 sanitize: {
413 never: true,
414 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900415
416 apex_available: [
417 "//apex_available:platform",
418 "com.android.runtime",
419 ],
dimitry11da1dc2018-01-05 13:35:43 +0100420}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800421
422cc_test {
423 name: "linker-unit-tests",
424
425 cflags: [
426 "-g",
427 "-Wall",
428 "-Wextra",
429 "-Wunused",
430 "-Werror",
431 ],
432
433 // We need to access Bionic private headers in the linker.
434 include_dirs: ["bionic/libc"],
435
436 srcs: [
437 // Tests.
438 "linker_block_allocator_test.cpp",
439 "linker_config_test.cpp",
440 "linked_list_test.cpp",
441 "linker_sleb128_test.cpp",
442 "linker_utils_test.cpp",
Ryan Prichard129f7a12019-12-23 16:45:47 -0800443 "linker_gnu_hash_test.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800444
445 // Parts of the linker that we're testing.
446 "linker_block_allocator.cpp",
447 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800448 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800449 "linker_test_globals.cpp",
450 "linker_utils.cpp",
451 ],
452
453 static_libs: [
454 "libasync_safe",
455 "libbase",
456 "liblog",
457 ],
Ryan Prichard129f7a12019-12-23 16:45:47 -0800458
459 arch: {
460 arm: {
461 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
462 },
463 arm64: {
464 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
465 },
466 },
467}
468
469cc_benchmark {
470 name: "linker-benchmarks",
471
472 srcs: [
473 "linker_gnu_hash_benchmark.cpp",
474 ],
475
476 arch: {
477 arm: {
478 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
479 },
480 arm64: {
481 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
482 },
483 },
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800484}