• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// ========================================================
2// linker_wrapper - Linux Bionic (on the host)
3// ========================================================
4
5// 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.
12package {
13    default_team: "trendy_team_native_tools_libraries",
14    default_applicable_licenses: ["bionic_linker_license"],
15}
16
17license {
18    name: "bionic_linker_license",
19    visibility: [":__subpackages__"],
20    license_kinds: [
21        "SPDX-license-identifier-BSD",
22    ],
23    license_text: [
24        "NOTICE",
25    ],
26}
27
28cc_object {
29    name: "linker_wrapper",
30    host_supported: true,
31    device_supported: false,
32    enabled: false,
33    target: {
34        linux_bionic: {
35            enabled: true,
36        },
37    },
38
39    cflags: [
40        "-fno-stack-protector",
41        "-Wstrict-overflow=5",
42        "-fvisibility=hidden",
43        "-Wall",
44        "-Wextra",
45        "-Wno-unused",
46        "-Werror",
47    ],
48
49    srcs: [
50        "linker_wrapper.cpp",
51    ],
52    arch: {
53        arm64: {
54            srcs: ["arch/arm64/linker_wrapper_begin.S"],
55        },
56        riscv64: {
57            srcs: ["arch/riscv64/linker_wrapper_begin.S"],
58        },
59        x86_64: {
60            srcs: ["arch/x86_64/linker_wrapper_begin.S"],
61        },
62    },
63
64    header_libs: ["libc_headers"],
65
66    // We need to access Bionic private headers in the linker.
67    include_dirs: ["bionic/libc"],
68}
69
70// ========================================================
71// linker default configuration
72// ========================================================
73
74// Configuration for the linker binary and any of its static libraries.
75cc_defaults {
76    name: "linker_defaults",
77    arch: {
78        arm: {
79            cflags: ["-D__work_around_b_24465209__"],
80        },
81        x86: {
82            cflags: ["-D__work_around_b_24465209__"],
83        },
84    },
85
86    cflags: [
87        "-fno-stack-protector",
88        "-Wstrict-overflow=5",
89        "-fvisibility=hidden",
90        "-Wall",
91        "-Wextra",
92        "-Wunused",
93        "-Werror",
94    ],
95
96    // TODO: split out the asflags.
97    asflags: [
98        "-fno-stack-protector",
99        "-Wstrict-overflow=5",
100        "-fvisibility=hidden",
101        "-Wall",
102        "-Wextra",
103        "-Wunused",
104        "-Werror",
105    ],
106
107    product_variables: {
108        debuggable: {
109            cppflags: ["-DUSE_LD_CONFIG_FILE"],
110        },
111    },
112
113    cppflags: ["-Wold-style-cast"],
114
115    static_libs: [
116        "libziparchive",
117        "libbase",
118        "libz",
119
120        "libasync_safe",
121
122        "liblog_for_runtime_apex",
123    ],
124
125    // We need to access Bionic private headers in the linker.
126    include_dirs: ["bionic/libc"],
127}
128
129// ========================================================
130// linker components
131// ========================================================
132
133// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
134// native bridge implementations).
135cc_defaults {
136    name: "linker_all_targets",
137    defaults: ["linux_bionic_supported"],
138    recovery_available: true,
139    vendor_ramdisk_available: true,
140    native_bridge_supported: true,
141}
142
143cc_library_static {
144    name: "liblinker_main",
145    defaults: [
146        "linker_defaults",
147        "linker_all_targets",
148    ],
149    srcs: ["linker_main.cpp"],
150
151    // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
152    cflags: ["-ffreestanding"],
153    apex_available: [
154        "com.android.runtime",
155    ],
156}
157
158cc_library_static {
159    name: "liblinker_malloc",
160    defaults: [
161        "linker_defaults",
162        "linker_all_targets",
163    ],
164    srcs: ["linker_memory.cpp"],
165    apex_available: [
166        "com.android.runtime",
167    ],
168}
169
170cc_library_static {
171    name: "liblinker_debuggerd_stub",
172    defaults: [
173        "linker_defaults",
174        "linker_all_targets",
175    ],
176    srcs: ["linker_debuggerd_stub.cpp"],
177}
178
179// ========================================================
180// template for the linker binary
181// ========================================================
182
183filegroup {
184    name: "linker_sources",
185    srcs: [
186        "dlfcn.cpp",
187        "linker.cpp",
188        "linker_auxv.cpp",
189        "linker_block_allocator.cpp",
190        "linker_dlwarning.cpp",
191        "linker_cfi.cpp",
192        "linker_config.cpp",
193        "linker_debug.cpp",
194        "linker_gdb_support.cpp",
195        "linker_globals.cpp",
196        "linker_libc_support.c",
197        "linker_libcxx_support.cpp",
198        "linker_namespaces.cpp",
199        "linker_logger.cpp",
200        "linker_mapped_file_fragment.cpp",
201        "linker_note_gnu_property.cpp",
202        "linker_phdr.cpp",
203        "linker_relocate.cpp",
204        "linker_sdk_versions.cpp",
205        "linker_soinfo.cpp",
206        "linker_transparent_hugepage_support.cpp",
207        "linker_tls.cpp",
208        "linker_utils.cpp",
209        "rt.cpp",
210    ],
211}
212
213filegroup {
214    name: "linker_sources_arm",
215    srcs: [
216        "arch/arm/begin.S",
217        "arch/arm_neon/linker_gnu_hash_neon.cpp",
218    ],
219}
220
221filegroup {
222    name: "linker_sources_arm64",
223    srcs: [
224        "arch/arm64/begin.S",
225        "arch/arm64/tlsdesc_resolver.S",
226        "arch/arm_neon/linker_gnu_hash_neon.cpp",
227    ],
228}
229
230filegroup {
231    name: "linker_sources_riscv64",
232    srcs: [
233        "arch/riscv64/begin.S",
234        "arch/riscv64/tlsdesc_resolver.S",
235    ],
236}
237
238filegroup {
239    name: "linker_sources_x86",
240    srcs: [
241        "arch/x86/begin.S",
242    ],
243}
244
245filegroup {
246    name: "linker_sources_x86_64",
247    srcs: [
248        "arch/x86_64/begin.S",
249    ],
250}
251
252cc_defaults {
253    name: "linker_version_script_overlay",
254    arch: {
255        arm: {
256            version_script: "linker.arm.map",
257        },
258        arm64: {
259            version_script: "linker.generic.map",
260        },
261        riscv64: {
262            version_script: "linker.generic.map",
263        },
264        x86: {
265            version_script: "linker.generic.map",
266        },
267        x86_64: {
268            version_script: "linker.generic.map",
269        },
270    },
271}
272
273// A template for the linker binary. May be inherited by native bridge implementations.
274cc_defaults {
275    name: "linker_bin_template",
276    defaults: ["linker_defaults"],
277
278    srcs: [":linker_sources"],
279
280    arch: {
281        arm: {
282            srcs: [":linker_sources_arm"],
283
284            // Arm 32 bit does not produce complete exidx unwind information
285            // so keep the .debug_frame which is relatively small and does
286            // include needed unwind information.
287            // See b/242162222 for details.
288            strip: {
289                keep_symbols_and_debug_frame: true,
290            },
291        },
292        arm64: {
293            srcs: [":linker_sources_arm64"],
294
295            // Leave the symbols in the shared library so that stack unwinders can produce
296            // meaningful name resolution.
297            strip: {
298                keep_symbols: true,
299            },
300        },
301        riscv64: {
302            srcs: [":linker_sources_riscv64"],
303
304            // Leave the symbols in the shared library so that stack unwinders can produce
305            // meaningful name resolution.
306            strip: {
307                keep_symbols: true,
308            },
309        },
310        x86: {
311            srcs: [":linker_sources_x86"],
312
313            // Leave the symbols in the shared library so that stack unwinders can produce
314            // meaningful name resolution.
315            strip: {
316                keep_symbols: true,
317            },
318        },
319        x86_64: {
320            srcs: [":linker_sources_x86_64"],
321
322            // Leave the symbols in the shared library so that stack unwinders can produce
323            // meaningful name resolution.
324            strip: {
325                keep_symbols: true,
326            },
327        },
328    },
329
330    // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
331    // static_executable. This dynamic linker is actually a shared object linked with static
332    // libraries.
333    ldflags: [
334        "-shared",
335        "-Wl,-Bsymbolic",
336        "-Wl,--exclude-libs,ALL",
337        "-Wl,-soname,ld-android.so",
338        // When the linker applies its own IRELATIVE relocations, it will only read DT_REL[A] and
339        // DT_JMPREL, not DT_ANDROID_REL[A], which can also theoretically contain IRELATIVE
340        // relocations. lld has been taught to not store them there as a bug workaround (see
341        // https://llvm.org/pr86751) but the workaround could be removed at some point in the
342        // future. So we explicitly prevent it from doing so by disabling DT_ANDROID_REL[A] when
343        // linking the linker (DT_RELR cannot encode IRELATIVE relocations).
344        "-Wl,--pack-dyn-relocs=relr",
345    ],
346
347    // we are going to link libc++_static manually because
348    // when stl is not set to "none" build system adds libdl
349    // to the list of static libraries which needs to be
350    // avoided in the case of building loader.
351    stl: "none",
352
353    // we don't want crtbegin.o (because we have begin.o), so unset it
354    // just for this module
355    nocrt: true,
356
357    static_executable: true,
358
359    // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
360    // looking up symbols in the linker by mistake.
361    prefix_symbols: "__dl_",
362
363    sanitize: {
364        hwaddress: false,
365    },
366
367    static_libs: [
368        "liblinker_main",
369        "liblinker_malloc",
370
371        // Use a version of libc++ built without exceptions, because accessing EH globals uses
372        // ELF TLS, which is not supported in the loader.
373        "libc++_static_noexcept",
374        "libc_nomalloc",
375        "libc_dynamic_dispatch",
376        "libm",
377        "libunwind",
378    ],
379
380    // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
381    // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
382    // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
383    // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
384    // non-relative dynamic relocation in the linker binary, which complicates linker startup.
385    //
386    // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
387    // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
388    // linker stops linking against libgcc.a's arm32 unwinder.
389    whole_static_libs: ["libc_unwind_static"],
390
391    system_shared_libs: [],
392
393    // Opt out of native_coverage when opting out of system_shared_libs
394    native_coverage: false,
395}
396
397// ========================================================
398// linker[_asan][64] binary
399// ========================================================
400
401cc_binary {
402    name: "linker",
403    defaults: [
404        "linker_bin_template",
405        "linux_bionic_supported",
406        "linker_version_script_overlay",
407    ],
408
409    srcs: [
410        "linker_translate_path.cpp",
411    ],
412
413    symlinks: ["linker_asan"],
414    arch: {
415        arm64: {
416            symlinks: ["linker_hwasan"],
417        },
418    },
419    multilib: {
420        lib64: {
421            suffix: "64",
422        },
423    },
424
425    compile_multilib: "both",
426
427    recovery_available: true,
428    vendor_ramdisk_available: true,
429    apex_available: [
430        "//apex_available:platform",
431        "com.android.runtime",
432    ],
433
434    target: {
435        android: {
436            srcs: [
437                "linker_debuggerd_android.cpp",
438            ],
439            static_libs: [
440                "libc++demangle_noexcept",
441                "libdebuggerd_handler_fallback",
442            ],
443        },
444        linux_bionic: {
445            static_libs: [
446                "liblinker_debuggerd_stub",
447            ],
448        },
449    },
450
451    afdo: true,
452
453    // FIXME: Workaround compat issue with obfuscation libraries.
454    // http://b/352456802
455    lto_O0: true,
456}
457
458// ========================================================
459// assorted modules
460// ========================================================
461
462sh_binary {
463    name: "ldd",
464    src: "ldd.sh",
465}
466
467// Used to generate binaries that can be backed by transparent hugepages.
468cc_defaults {
469    name: "linker_hugepage_aligned",
470    arch: {
471        arm64: {
472            ldflags: ["-z max-page-size=0x200000"],
473        },
474        x86_64: {
475            ldflags: ["-z max-page-size=0x200000"],
476        },
477    },
478}
479
480cc_library {
481    // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
482    // libgcc.a are made static to ld-android.so.  This in turn ensures that libraries that
483    // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
484    // to provide those symbols, but will instead pull them from libgcc.a.  Specifically,
485    // we use this property to make sure libc.so has its own copy of the code from
486    // libgcc.a it uses.
487    //
488    // DO NOT REMOVE --exclude-libs!
489
490    ldflags: [
491        "-Wl,--exclude-libs=libgcc.a",
492        "-Wl,--exclude-libs=libgcc_stripped.a",
493        "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
494        "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
495        "-Wl,--exclude-libs=libclang_rt.builtins-riscv64-android.a",
496        "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
497        "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
498    ],
499
500    // for x86, exclude libgcc_eh.a for the same reasons as above
501    arch: {
502        x86: {
503            ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
504        },
505        x86_64: {
506            ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
507        },
508    },
509
510    srcs: ["ld_android.cpp"],
511    cflags: [
512        "-Wall",
513        "-Wextra",
514        "-Wunused",
515        "-Werror",
516    ],
517    stl: "none",
518
519    name: "ld-android",
520    defaults: [
521        "linux_bionic_supported",
522        "linker_version_script_overlay",
523    ],
524    ramdisk_available: true,
525    vendor_ramdisk_available: true,
526    recovery_available: true,
527    native_bridge_supported: true,
528
529    nocrt: true,
530    system_shared_libs: [],
531    header_libs: ["libc_headers"],
532
533    // Opt out of native_coverage when opting out of system_shared_libs
534    native_coverage: false,
535
536    sanitize: {
537        never: true,
538    },
539
540    apex_available: [
541        "//apex_available:platform",
542        "com.android.runtime",
543    ],
544}
545
546cc_test {
547    name: "linker-unit-tests",
548    test_suites: ["device-tests"],
549
550    cflags: [
551        "-g",
552        "-Wall",
553        "-Wextra",
554        "-Wunused",
555        "-Werror",
556    ],
557
558    // We need to access Bionic private headers in the linker.
559    include_dirs: ["bionic/libc"],
560
561    srcs: [
562        // Tests.
563        "linker_block_allocator_test.cpp",
564        "linker_config_test.cpp",
565        "linked_list_test.cpp",
566        "linker_note_gnu_property_test.cpp",
567        "linker_sleb128_test.cpp",
568        "linker_utils_test.cpp",
569        "linker_gnu_hash_test.cpp",
570        "linker_crt_pad_segment_test.cpp",
571
572        // Parts of the linker that we're testing.
573        ":elf_note_sources",
574        "linker_block_allocator.cpp",
575        "linker_config.cpp",
576        "linker_debug.cpp",
577        "linker_note_gnu_property.cpp",
578        "linker_test_globals.cpp",
579        "linker_utils.cpp",
580        "linker_phdr.cpp",
581        "linker_mapped_file_fragment.cpp",
582        "linker_sdk_versions.cpp",
583        "linker_dlwarning.cpp",
584    ],
585
586    static_libs: [
587        "libasync_safe",
588        "libbase",
589        "liblog_for_runtime_apex",
590        "libprocinfo", // For procinfo::MappedFileSize()
591    ],
592
593    data_libs: [
594        "crt_pad_segment_disabled",
595        "crt_pad_segment_enabled",
596        "no_crt_pad_segment",
597    ],
598
599    arch: {
600        arm: {
601            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
602        },
603        arm64: {
604            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
605        },
606    },
607}
608
609cc_benchmark {
610    name: "linker-benchmarks",
611
612    srcs: [
613        "linker_gnu_hash_benchmark.cpp",
614    ],
615
616    arch: {
617        arm: {
618            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
619        },
620        arm64: {
621            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
622        },
623    },
624}
625