blob: e67fca8e3e2e8e695f90734babc463588217d219 [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",
dimitryb8b3a762018-09-25 12:31:11 +0200181 ],
182}
183
184filegroup {
185 name: "linker_sources_arm64",
186 srcs: [
187 "arch/arm64/begin.S",
Ryan Prichardffaae702019-01-23 17:47:10 -0800188 "arch/arm64/tlsdesc_resolver.S",
dimitryb8b3a762018-09-25 12:31:11 +0200189 ],
190}
191
192filegroup {
193 name: "linker_sources_x86",
194 srcs: [
195 "arch/x86/begin.S",
196 ],
197}
198
199filegroup {
200 name: "linker_sources_x86_64",
201 srcs: [
202 "arch/x86_64/begin.S",
203 ],
204}
205
206filegroup {
207 name: "linker_sources_mips",
208 srcs: [
209 "arch/mips/begin.S",
210 "linker_mips.cpp",
211 ],
212}
213
214filegroup {
215 name: "linker_sources_mips64",
216 srcs: [
217 "arch/mips64/begin.S",
218 "linker_mips.cpp",
219 ],
220}
221
dimitryb8b3a762018-09-25 12:31:11 +0200222cc_defaults {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700223 name: "linker_version_script_overlay",
224 arch: {
225 arm: { version_script: "linker.arm.map" },
226 arm64: { version_script: "linker.generic.map" },
227 x86: { version_script: "linker.generic.map" },
228 x86_64: { version_script: "linker.generic.map" },
229 mips: { version_script: "linker.generic.map" },
230 mips64: { version_script: "linker.generic.map" },
231 },
232}
233
234// A template for the linker binary. May be inherited by native bridge implementations.
235cc_defaults {
236 name: "linker_bin_template",
237 defaults: ["linker_defaults"],
238
239 srcs: [":linker_sources"],
240
Colin Cross97f0aef2016-07-14 16:05:46 -0700241 arch: {
242 arm: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700243 srcs: [":linker_sources_arm"],
244 static_libs: ["libunwind_llvm"],
245 },
246 arm64: {
247 srcs: [":linker_sources_arm64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700248 },
249 x86: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700250 srcs: [":linker_sources_x86"],
251 },
252 x86_64: {
253 srcs: [":linker_sources_x86_64"],
254 },
255 mips: {
256 srcs: [":linker_sources_mips"],
257 },
258 mips64: {
259 srcs: [":linker_sources_mips64"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700260 },
261 },
262
Ryan Prichard80e40f02019-10-31 19:54:46 -0700263 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
264 // static_executable. This dynamic linker is actually a shared object linked with static
265 // libraries.
Colin Cross97f0aef2016-07-14 16:05:46 -0700266 ldflags: [
267 "-shared",
268 "-Wl,-Bsymbolic",
269 "-Wl,--exclude-libs,ALL",
dimitry8e8c2c02018-01-04 12:08:32 +0100270 "-Wl,-soname,ld-android.so",
Colin Cross97f0aef2016-07-14 16:05:46 -0700271 ],
272
Dimitry Ivanovfc0d4802016-12-06 11:10:09 -0800273 // we are going to link libc++_static manually because
274 // when stl is not set to "none" build system adds libdl
275 // to the list of static libraries which needs to be
276 // avoided in the case of building loader.
277 stl: "none",
278
Colin Cross97f0aef2016-07-14 16:05:46 -0700279 // we don't want crtbegin.o (because we have begin.o), so unset it
280 // just for this module
281 nocrt: true,
282
dimitryb8b3a762018-09-25 12:31:11 +0200283 static_executable: true,
284
285 // Leave the symbols in the shared library so that stack unwinders can produce
286 // meaningful name resolution.
287 strip: {
288 keep_symbols: true,
289 },
290
291 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
292 // looking up symbols in the linker by mistake.
293 prefix_symbols: "__dl_",
294
295 sanitize: {
296 hwaddress: false,
297 },
dimitryb8b3a762018-09-25 12:31:11 +0200298
Colin Cross97f0aef2016-07-14 16:05:46 -0700299 static_libs: [
Ryan Prichard249757b2019-11-01 17:18:28 -0700300 "liblinker_main",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700301 "liblinker_malloc",
302
303 "libc++_static",
Colin Cross97f0aef2016-07-14 16:05:46 -0700304 "libc_nomalloc",
Ryan Prichard249757b2019-11-01 17:18:28 -0700305 "libc_dynamic_dispatch",
Dimitry Ivanov451909d2017-01-26 16:52:59 -0800306 "libm",
Colin Cross97f0aef2016-07-14 16:05:46 -0700307 ],
Colin Cross97f0aef2016-07-14 16:05:46 -0700308
Ryan Prichardd9a41152019-10-14 16:58:53 -0700309 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
310 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
311 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
312 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
313 // non-relative dynamic relocation in the linker binary, which complicates linker startup.
314 //
315 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
316 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
317 // linker stops linking against libgcc.a's arm32 unwinder.
318 whole_static_libs: ["libc_unwind_static"],
319
Ryan Prichard80e40f02019-10-31 19:54:46 -0700320 system_shared_libs: [],
321
322 // Opt out of native_coverage when opting out of system_shared_libs
323 native_coverage: false,
324}
325
326// ========================================================
327// linker[_asan][64] binary
328// ========================================================
329
330cc_binary {
Colin Cross97f0aef2016-07-14 16:05:46 -0700331 name: "linker",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700332 defaults: [
333 "linker_bin_template",
334 "linux_bionic_supported",
335 "linker_version_script_overlay",
336 ],
337
Colin Cross10fffb42016-12-08 09:57:35 -0800338 symlinks: ["linker_asan"],
Colin Cross97f0aef2016-07-14 16:05:46 -0700339 multilib: {
Colin Cross97f0aef2016-07-14 16:05:46 -0700340 lib64: {
341 suffix: "64",
Colin Cross10fffb42016-12-08 09:57:35 -0800342 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700343 },
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800344
Ryan Prichard80e40f02019-10-31 19:54:46 -0700345 compile_multilib: "both",
346 xom: false,
347
348 recovery_available: true,
349 apex_available: [
350 "//apex_available:platform",
351 "com.android.runtime",
352 ],
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800353
Colin Cross97f0aef2016-07-14 16:05:46 -0700354 target: {
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800355 android: {
Ryan Prichard80e40f02019-10-31 19:54:46 -0700356 srcs: [
357 "linker_debuggerd_android.cpp",
358 ],
Dan Albert4ea19212019-07-23 13:31:14 -0700359 static_libs: [
360 "libc++demangle",
361 "libdebuggerd_handler_fallback",
362 ],
Dan Willemsen7ec52b12016-11-28 17:02:25 -0800363 },
Ryan Prichard80e40f02019-10-31 19:54:46 -0700364 linux_bionic: {
365 static_libs: [
366 "liblinker_debuggerd_stub",
367 ],
368 }
Colin Cross97f0aef2016-07-14 16:05:46 -0700369 },
Colin Cross97f0aef2016-07-14 16:05:46 -0700370}
dimitry11da1dc2018-01-05 13:35:43 +0100371
Ryan Prichard80e40f02019-10-31 19:54:46 -0700372// ========================================================
373// assorted modules
374// ========================================================
375
Elliott Hughes90f96b92019-05-09 15:56:39 -0700376sh_binary {
377 name: "ldd",
378 src: "ldd",
379}
380
dimitry11da1dc2018-01-05 13:35:43 +0100381cc_library {
382 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
383 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
384 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
385 // to provide those symbols, but will instead pull them from libgcc.a. Specifically,
386 // we use this property to make sure libc.so has its own copy of the code from
387 // libgcc.a it uses.
388 //
389 // DO NOT REMOVE --exclude-libs!
390
Yi Kong7786a342018-08-31 19:01:56 -0700391 ldflags: [
392 "-Wl,--exclude-libs=libgcc.a",
Yi Kong7ac2afb2019-05-06 16:23:10 -0700393 "-Wl,--exclude-libs=libgcc_stripped.a",
Yi Kong7786a342018-08-31 19:01:56 -0700394 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
395 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
396 "-Wl,--exclude-libs=libclang_rt.builtins-x86-android.a",
397 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
398 ],
dimitry11da1dc2018-01-05 13:35:43 +0100399
400 // for x86, exclude libgcc_eh.a for the same reasons as above
401 arch: {
402 x86: {
403 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
404 },
405 x86_64: {
406 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
407 },
408 },
dimitry581723e2018-01-05 14:31:44 +0100409
410 srcs: ["ld_android.cpp"],
dimitry11da1dc2018-01-05 13:35:43 +0100411 cflags: [
412 "-Wall",
413 "-Wextra",
414 "-Wunused",
415 "-Werror",
416 ],
417 stl: "none",
418
419 name: "ld-android",
Ryan Prichard80e40f02019-10-31 19:54:46 -0700420 defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
Jiyong Park5603c6e2018-04-27 21:53:11 +0900421 recovery_available: true,
dimitry7f048802019-05-03 15:57:34 +0200422 native_bridge_supported: true,
dimitry11da1dc2018-01-05 13:35:43 +0100423
Ryan Prichard470b6662018-03-27 22:10:55 -0700424 nocrt: true,
dimitry11da1dc2018-01-05 13:35:43 +0100425 system_shared_libs: [],
426
Pirama Arumuga Nainarfcd35382019-02-14 13:48:01 -0800427 // Opt out of native_coverage when opting out of system_shared_libs
428 native_coverage: false,
429
dimitry11da1dc2018-01-05 13:35:43 +0100430 sanitize: {
431 never: true,
432 },
Jiyong Parke87e0dc2019-10-02 17:09:33 +0900433
434 apex_available: [
435 "//apex_available:platform",
436 "com.android.runtime",
437 ],
dimitry11da1dc2018-01-05 13:35:43 +0100438}
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800439
440cc_test {
441 name: "linker-unit-tests",
442
443 cflags: [
444 "-g",
445 "-Wall",
446 "-Wextra",
447 "-Wunused",
448 "-Werror",
449 ],
450
451 // We need to access Bionic private headers in the linker.
452 include_dirs: ["bionic/libc"],
453
454 srcs: [
455 // Tests.
456 "linker_block_allocator_test.cpp",
457 "linker_config_test.cpp",
458 "linked_list_test.cpp",
459 "linker_sleb128_test.cpp",
460 "linker_utils_test.cpp",
461
462 // Parts of the linker that we're testing.
463 "linker_block_allocator.cpp",
464 "linker_config.cpp",
Ryan Prichard551565e2019-12-23 16:03:14 -0800465 "linker_debug.cpp",
Elliott Hughes15a2b7b2019-02-15 13:48:38 -0800466 "linker_test_globals.cpp",
467 "linker_utils.cpp",
468 ],
469
470 static_libs: [
471 "libasync_safe",
472 "libbase",
473 "liblog",
474 ],
475}