blob: 8e810d2fe4ee160756f327df2f98f52abd2b1342 [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",
168 "linker_sdk_versions.cpp",
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700169 "linker_soinfo.cpp",
Ryan Prichard45d13492019-01-03 02:51:30 -0800170 "linker_tls.cpp",
Colin Cross97f0aef2016-07-14 16:05:46 -0700171 "linker_utils.cpp",
172 "rt.cpp",
173 ],
dimitryb8b3a762018-09-25 12:31:11 +0200174}
Colin Cross97f0aef2016-07-14 16:05:46 -0700175
dimitryb8b3a762018-09-25 12:31:11 +0200176filegroup {
177 name: "linker_sources_arm",
178 srcs: [
179 "arch/arm/begin.S",
dimitryb8b3a762018-09-25 12:31:11 +0200180 ],
181}
182
183filegroup {
184 name: "linker_sources_arm64",
185 srcs: [
186 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800187 "arch/arm64/tlsdesc_resolver.S",
dimitryb8b3a762018-09-25 12:31:11 +0200188 ],
189}
190
191filegroup {
192 name: "linker_sources_x86",
193 srcs: [
194 "arch/x86/begin.S",
195 ],
196}
197
198filegroup {
199 name: "linker_sources_x86_64",
200 srcs: [
201 "arch/x86_64/begin.S",
202 ],
203}
204
205filegroup {
206 name: "linker_sources_mips",
207 srcs: [
208 "arch/mips/begin.S",
209 "linker_mips.cpp",
210 ],
211}
212
213filegroup {
214 name: "linker_sources_mips64",
215 srcs: [
216 "arch/mips64/begin.S",
217 "linker_mips.cpp",
218 ],
219}
220
dimitryb8b3a762018-09-25 12:31:11 +0200221cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700222 name: "linker_version_script_overlay",
223 arch: {
224 arm: { version_script: "linker.arm.map" },
225 arm64: { version_script: "linker.generic.map" },
226 x86: { version_script: "linker.generic.map" },
227 x86_64: { version_script: "linker.generic.map" },
228 mips: { version_script: "linker.generic.map" },
229 mips64: { version_script: "linker.generic.map" },
230 },
231}
232
233// A template for the linker binary. May be inherited by native bridge implementations.
234cc_defaults {
235 name: "linker_bin_template",
236 defaults: ["linker_defaults"],
237
238 srcs: [":linker_sources"],
239
Colin Cross97f0aef2016-07-14 16:05:46 -0700240 arch: {
241 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700242 srcs: [":linker_sources_arm"],
243 static_libs: ["libunwind_llvm"],
244 },
245 arm64: {
246 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700247 },
248 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700249 srcs: [":linker_sources_x86"],
250 },
251 x86_64: {
252 srcs: [":linker_sources_x86_64"],
253 },
254 mips: {
255 srcs: [":linker_sources_mips"],
256 },
257 mips64: {
258 srcs: [":linker_sources_mips64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700259 },
260 },
261
Ryan Prichard80e40f02019-10-31 19:54:46 -0700262 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
263 // static_executable. This dynamic linker is actually a shared object linked with static
264 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700265 ldflags: [
266 "-shared",
267 "-Wl,-Bsymbolic",
268 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100269 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700270 ],
271
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800272 // we are going to link libc++_static manually because
273 // when stl is not set to "none" build system adds libdl
274 // to the list of static libraries which needs to be
275 // avoided in the case of building loader.
276 stl: "none",
277
Colin Cross97f0aef2016-07-14 16:05:46 -0700278 // we don't want crtbegin.o (because we have begin.o), so unset it
279 // just for this module
280 nocrt: true,
281
dimitryb8b3a762018-09-25 12:31:11 +0200282 static_executable: true,
283
284 // Leave the symbols in the shared library so that stack unwinders can produce
285 // meaningful name resolution.
286 strip: {
287 keep_symbols: true,
288 },
289
290 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
291 // looking up symbols in the linker by mistake.
292 prefix_symbols: "__dl_",
293
294 sanitize: {
295 hwaddress: false,
296 },
dimitryb8b3a762018-09-25 12:31:11 +0200297
Colin Cross97f0aef2016-07-14 16:05:46 -0700298 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700299 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700300 "liblinker_malloc",
301
302 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700303 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700304 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800305 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700306 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700307
Ryan Prichardd9a41152019-10-14 16:58:53 -0700308 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
309 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
310 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
311 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
312 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
313 //
314 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
315 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
316 // linker stops linking against libgcc.a's arm32 unwinder.
317 whole_static_libs: ["libc_unwind_static"],
318
Ryan Prichard80e40f02019-10-31 19:54:46 -0700319 system_shared_libs: [],
320
321 // Opt out of native_coverage when opting out of system_shared_libs
322 native_coverage: false,
323}
324
325// ========================================================
326// linker[_asan][64] binary
327// ========================================================
328
329cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700330 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700331 defaults: [
332 "linker_bin_template",
333 "linux_bionic_supported",
334 "linker_version_script_overlay",
335 ],
336
Colin Cross10fffb42016-12-08 09:57:35 -0800337 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700338 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700339 lib64: {
340 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800341 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700342 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800343
Ryan Prichard80e40f02019-10-31 19:54:46 -0700344 compile_multilib: "both",
345 xom: false,
346
347 recovery_available: true,
348 apex_available: [
349 "//apex_available:platform",
350 "com.android.runtime",
351 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800352
Colin Cross97f0aef2016-07-14 16:05:46 -0700353 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800354 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700355 srcs: [
356 "linker_debuggerd_android.cpp",
357 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700358 static_libs: [
359 "libc++demangle",
360 "libdebuggerd_handler_fallback",
361 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800362 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700363 linux_bionic: {
364 static_libs: [
365 "liblinker_debuggerd_stub",
366 ],
367 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700368 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700369}
dimitry11da1dc2018-01-05 13:35:43 +0100370
Ryan Prichard80e40f02019-10-31 19:54:46 -0700371// ========================================================
372// assorted modules
373// ========================================================
374
Elliott Hughes90f96b92019-05-09 15:56:39 -0700375sh_binary {
376 name: "ldd",
377 src: "ldd",
378}
379
dimitry11da1dc2018-01-05 13:35:43 +0100380cc_library {
381 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
382 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
383 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
384 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
385 // we use this property to make sure libc.so has its own copy of the code from
386 // libgcc.a it uses.
387 //
388 // DO NOT REMOVE --exclude-libs!
389
Yi Kong7786a342018-08-31 19:01:56 -0700390 ldflags: [
391 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700392 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700393 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
394 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
395 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
396 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
397 ],
dimitry11da1dc2018-01-05 13:35:43 +0100398
399 // for x86, exclude libgcc_eh.a for the same reasons as above
400 arch: {
401 x86: {
402 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
403 },
404 x86_64: {
405 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
406 },
407 },
dimitry581723e2018-01-05 14:31:44 +0100408
409 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100410 cflags: [
411 "-Wall",
412 "-Wextra",
413 "-Wunused",
414 "-Werror",
415 ],
416 stl: "none",
417
418 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700419 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Jiyong Park5603c6e2018-04-27 21:53:11 +0900420 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200421 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100422
Ryan Prichard470b6662018-03-27 22:10:55 -0700423 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100424 system_shared_libs: [],
425
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800426 // Opt out of native_coverage when opting out of system_shared_libs
427 native_coverage: false,
428
dimitry11da1dc2018-01-05 13:35:43 +0100429 sanitize: {
430 never: true,
431 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900432
433 apex_available: [
434 "//apex_available:platform",
435 "com.android.runtime",
436 ],
dimitry11da1dc2018-01-05 13:35:43 +0100437}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800438
439cc_test {
440 name: "linker-unit-tests",
441
442 cflags: [
443 "-g",
444 "-Wall",
445 "-Wextra",
446 "-Wunused",
447 "-Werror",
448 ],
449
450 // We need to access Bionic private headers in the linker.
451 include_dirs: ["bionic/libc"],
452
453 srcs: [
454 // Tests.
455 "linker_block_allocator_test.cpp",
456 "linker_config_test.cpp",
457 "linked_list_test.cpp",
458 "linker_sleb128_test.cpp",
459 "linker_utils_test.cpp",
460
461 // Parts of the linker that we're testing.
462 "linker_block_allocator.cpp",
463 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800464 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800465 "linker_test_globals.cpp",
466 "linker_utils.cpp",
467 ],
468
469 static_libs: [
470 "libasync_safe",
471 "libbase",
472 "liblog",
473 ],
474}