xref: /aosp_15_r20/bionic/linker/Android.bp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1package {
2    default_team: "trendy_team_native_tools_libraries",
3    default_applicable_licenses: ["bionic_linker_license"],
4}
5
6license {
7    name: "bionic_linker_license",
8    visibility: [":__subpackages__"],
9    license_kinds: [
10        "SPDX-license-identifier-BSD",
11    ],
12    license_text: [
13        "NOTICE",
14    ],
15}
16
17linker_common_flags = [
18    "-fno-stack-protector",
19    "-Wstrict-overflow=5",
20    "-fvisibility=hidden",
21    "-Wall",
22    "-Wextra",
23    "-Wunused",
24    "-Werror",
25]
26
27// ========================================================
28// linker_wrapper - Linux Bionic (on the host)
29// ========================================================
30
31// This is used for bionic on (host) Linux to bootstrap our linker embedded into
32// a binary.
33//
34// Host bionic binaries do not have a PT_INTERP section, instead this gets
35// embedded as the entry point, and the linker is embedded as ELF sections in
36// each binary. There's a linker script that sets all of that up (generated by
37// extract_linker), and defines the extern symbols used in this file.
38cc_object {
39    name: "linker_wrapper",
40    host_supported: true,
41    device_supported: false,
42    enabled: false,
43    target: {
44        linux_bionic: {
45            enabled: true,
46        },
47    },
48
49    cflags: linker_common_flags,
50
51    srcs: [
52        "linker_wrapper.cpp",
53    ],
54    arch: {
55        arm64: {
56            srcs: ["arch/arm64/linker_wrapper_begin.S"],
57        },
58        riscv64: {
59            srcs: ["arch/riscv64/linker_wrapper_begin.S"],
60        },
61        x86_64: {
62            srcs: ["arch/x86_64/linker_wrapper_begin.S"],
63        },
64    },
65
66    header_libs: ["libc_headers"],
67
68    // We need to access Bionic private headers in the linker.
69    include_dirs: ["bionic/libc"],
70}
71
72// ========================================================
73// linker default configuration
74// ========================================================
75
76// Configuration for the linker binary and any of its static libraries.
77cc_defaults {
78    name: "linker_defaults",
79    arch: {
80        arm: {
81            cflags: ["-D__work_around_b_24465209__"],
82        },
83        x86: {
84            cflags: ["-D__work_around_b_24465209__"],
85        },
86    },
87
88    cflags: linker_common_flags,
89    asflags: linker_common_flags,
90
91    product_variables: {
92        debuggable: {
93            cppflags: ["-DUSE_LD_CONFIG_FILE"],
94        },
95    },
96
97    cppflags: ["-Wold-style-cast"],
98
99    static_libs: [
100        "libziparchive",
101        "libbase",
102        "libz",
103
104        "libasync_safe",
105
106        "liblog_for_runtime_apex",
107    ],
108
109    // We need to access Bionic private headers in the linker.
110    include_dirs: ["bionic/libc"],
111
112    sanitize: {
113        // Supporting memtag_globals in the linker would be tricky,
114        // because it relocates itself very early.
115        memtag_globals: false,
116    },
117}
118
119// ========================================================
120// linker components
121// ========================================================
122
123// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
124// native bridge implementations).
125cc_defaults {
126    name: "linker_all_targets",
127    defaults: ["linux_bionic_supported"],
128    recovery_available: true,
129    vendor_ramdisk_available: true,
130    native_bridge_supported: true,
131}
132
133cc_library_static {
134    name: "liblinker_main",
135    defaults: [
136        "linker_defaults",
137        "linker_all_targets",
138    ],
139    srcs: ["linker_main.cpp"],
140
141    // Ensure that the compiler won't insert string function calls before ifuncs are resolved.
142    cflags: ["-ffreestanding"],
143    apex_available: [
144        "com.android.runtime",
145    ],
146}
147
148cc_library_static {
149    name: "liblinker_malloc",
150    defaults: [
151        "linker_defaults",
152        "linker_all_targets",
153    ],
154    srcs: ["linker_memory.cpp"],
155    apex_available: [
156        "com.android.runtime",
157    ],
158}
159
160cc_library_static {
161    name: "liblinker_debuggerd_stub",
162    defaults: [
163        "linker_defaults",
164        "linker_all_targets",
165    ],
166    srcs: ["linker_debuggerd_stub.cpp"],
167}
168
169// ========================================================
170// template for the linker binary
171// ========================================================
172
173filegroup {
174    name: "linker_sources",
175    srcs: [
176        "dlfcn.cpp",
177        "linker.cpp",
178        "linker_auxv.cpp",
179        "linker_block_allocator.cpp",
180        "linker_dlwarning.cpp",
181        "linker_cfi.cpp",
182        "linker_config.cpp",
183        "linker_debug.cpp",
184        "linker_gdb_support.cpp",
185        "linker_globals.cpp",
186        "linker_libc_support.c",
187        "linker_libcxx_support.cpp",
188        "linker_namespaces.cpp",
189        "linker_logger.cpp",
190        "linker_mapped_file_fragment.cpp",
191        "linker_note_gnu_property.cpp",
192        "linker_phdr.cpp",
193        "linker_phdr_16kib_compat.cpp",
194        "linker_relocate.cpp",
195        "linker_sdk_versions.cpp",
196        "linker_soinfo.cpp",
197        "linker_transparent_hugepage_support.cpp",
198        "linker_tls.cpp",
199        "linker_utils.cpp",
200        "rt.cpp",
201    ],
202}
203
204filegroup {
205    name: "linker_sources_arm",
206    srcs: [
207        "arch/arm/begin.S",
208        "arch/arm_neon/linker_gnu_hash_neon.cpp",
209    ],
210}
211
212filegroup {
213    name: "linker_sources_arm64",
214    srcs: [
215        "arch/arm64/begin.S",
216        "arch/arm64/tlsdesc_resolver.S",
217        "arch/arm_neon/linker_gnu_hash_neon.cpp",
218    ],
219}
220
221filegroup {
222    name: "linker_sources_riscv64",
223    srcs: [
224        "arch/riscv64/begin.S",
225        "arch/riscv64/tlsdesc_resolver.S",
226    ],
227}
228
229filegroup {
230    name: "linker_sources_x86",
231    srcs: [
232        "arch/x86/begin.S",
233    ],
234}
235
236filegroup {
237    name: "linker_sources_x86_64",
238    srcs: [
239        "arch/x86_64/begin.S",
240    ],
241}
242
243cc_defaults {
244    name: "linker_version_script_overlay",
245    arch: {
246        arm: {
247            version_script: "linker.arm.map",
248        },
249        arm64: {
250            version_script: "linker.generic.map",
251        },
252        riscv64: {
253            version_script: "linker.generic.map",
254        },
255        x86: {
256            version_script: "linker.generic.map",
257        },
258        x86_64: {
259            version_script: "linker.generic.map",
260        },
261    },
262}
263
264// A template for the linker binary. May be inherited by native bridge implementations.
265cc_defaults {
266    name: "linker_bin_template",
267    defaults: [
268        "linker_defaults",
269        "keep_symbols",
270    ],
271
272    srcs: [":linker_sources"],
273
274    arch: {
275        arm: {
276            srcs: [":linker_sources_arm"],
277        },
278        arm64: {
279            srcs: [":linker_sources_arm64"],
280        },
281        riscv64: {
282            srcs: [":linker_sources_riscv64"],
283        },
284        x86: {
285            srcs: [":linker_sources_x86"],
286        },
287        x86_64: {
288            srcs: [":linker_sources_x86_64"],
289        },
290    },
291
292    static_executable: true,
293
294    // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
295    // static_executable. The dynamic linker is actually a shared object linked with static
296    // libraries.
297    ldflags: [
298        "-shared",
299        "-Wl,-Bsymbolic",
300        "-Wl,--exclude-libs,ALL",
301        "-Wl,-soname,ld-android.so",
302        // When the linker applies its own IRELATIVE relocations, it will only read DT_REL[A] and
303        // DT_JMPREL, not DT_ANDROID_REL[A], which can also theoretically contain IRELATIVE
304        // relocations. lld has been taught to not store them there as a bug workaround (see
305        // https://llvm.org/pr86751) but the workaround could be removed at some point in the
306        // future. So we explicitly prevent it from doing so by disabling DT_ANDROID_REL[A] when
307        // linking the linker (DT_RELR cannot encode IRELATIVE relocations).
308        "-Wl,--pack-dyn-relocs=relr",
309    ],
310
311    // We link libc++_static manually because otherwise the build system will
312    // automatically add libdl to the list of static libraries.
313    stl: "none",
314
315    // We don't want crtbegin.o (because we have our own arch/*/begin.o),
316    // so unset it just for this module.
317    nocrt: true,
318
319    // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
320    // looking up symbols in the linker by mistake.
321    prefix_symbols: "__dl_",
322
323    sanitize: {
324        hwaddress: false,
325        memtag_stack: false,
326    },
327
328    static_libs: [
329        "liblinker_main",
330        "liblinker_malloc",
331
332        // We use a version of libc++ built without exceptions,
333        // because accessing EH globals uses ELF TLS,
334        // which is not supported in the loader.
335        "libc++_static_noexcept",
336
337        "libc_nomalloc",
338        "libc_dynamic_dispatch",
339        "libm",
340        "libunwind",
341    ],
342
343    system_shared_libs: [],
344
345    // Opt out of native_coverage when opting out of system_shared_libs
346    native_coverage: false,
347}
348
349// ========================================================
350// linker[_asan][64] binary
351// ========================================================
352
353cc_binary {
354    name: "linker",
355    defaults: [
356        "linker_bin_template",
357        "linux_bionic_supported",
358        "linker_version_script_overlay",
359    ],
360
361    srcs: [
362        "linker_translate_path.cpp",
363    ],
364
365    symlinks: ["linker_asan"],
366    arch: {
367        arm64: {
368            symlinks: ["linker_hwasan"],
369        },
370    },
371    multilib: {
372        lib64: {
373            suffix: "64",
374        },
375    },
376
377    compile_multilib: "both",
378
379    recovery_available: true,
380    vendor_ramdisk_available: true,
381    apex_available: [
382        "//apex_available:platform",
383        "com.android.runtime",
384    ],
385
386    target: {
387        android: {
388            srcs: [
389                "linker_debuggerd_android.cpp",
390            ],
391            static_libs: [
392                "libc++demangle_noexcept",
393                "libdebuggerd_handler_fallback",
394            ],
395        },
396        linux_bionic: {
397            static_libs: [
398                "liblinker_debuggerd_stub",
399            ],
400        },
401    },
402
403    afdo: true,
404}
405
406// ========================================================
407// assorted modules
408// ========================================================
409
410sh_binary {
411    name: "ldd",
412    src: "ldd.sh",
413}
414
415// Used to generate binaries that can be backed by transparent hugepages.
416cc_defaults {
417    name: "linker_hugepage_aligned",
418    arch: {
419        arm64: {
420            ldflags: ["-z max-page-size=0x200000"],
421        },
422        x86_64: {
423            ldflags: ["-z max-page-size=0x200000"],
424        },
425    },
426}
427
428cc_library {
429    srcs: ["ld_android.cpp"],
430    cflags: [
431        "-Wall",
432        "-Wextra",
433        "-Wunused",
434        "-Werror",
435    ],
436    stl: "none",
437
438    name: "ld-android",
439    defaults: [
440        "linux_bionic_supported",
441        "linker_version_script_overlay",
442    ],
443    ramdisk_available: true,
444    vendor_ramdisk_available: true,
445    recovery_available: true,
446    native_bridge_supported: true,
447
448    nocrt: true,
449    system_shared_libs: [],
450    header_libs: ["libc_headers"],
451
452    // Opt out of native_coverage when opting out of system_shared_libs
453    native_coverage: false,
454
455    sanitize: {
456        never: true,
457    },
458
459    apex_available: [
460        "//apex_available:platform",
461        "com.android.runtime",
462    ],
463}
464
465cc_test {
466    name: "linker-unit-tests",
467    test_suites: ["device-tests"],
468
469    cflags: [
470        "-g",
471        "-Wall",
472        "-Wextra",
473        "-Wunused",
474        "-Werror",
475    ],
476
477    // We need to access Bionic private headers in the linker.
478    include_dirs: ["bionic/libc"],
479
480    srcs: [
481        // Tests.
482        "linker_block_allocator_test.cpp",
483        "linker_config_test.cpp",
484        "linked_list_test.cpp",
485        "linker_note_gnu_property_test.cpp",
486        "linker_sleb128_test.cpp",
487        "linker_utils_test.cpp",
488        "linker_gnu_hash_test.cpp",
489        "linker_crt_pad_segment_test.cpp",
490
491        // Parts of the linker that we're testing.
492        ":elf_note_sources",
493        "linker_block_allocator.cpp",
494        "linker_config.cpp",
495        "linker_debug.cpp",
496        "linker_note_gnu_property.cpp",
497        "linker_test_globals.cpp",
498        "linker_utils.cpp",
499        "linker_phdr.cpp",
500        "linker_mapped_file_fragment.cpp",
501        "linker_sdk_versions.cpp",
502        "linker_dlwarning.cpp",
503        "linker_phdr_16kib_compat.cpp"
504    ],
505
506    static_libs: [
507        "libasync_safe",
508        "libbase",
509        "liblog_for_runtime_apex",
510        "libprocinfo", // For procinfo::MappedFileSize()
511    ],
512
513    data_libs: [
514        "crt_pad_segment_disabled",
515        "crt_pad_segment_enabled",
516        "no_crt_pad_segment",
517    ],
518
519    arch: {
520        arm: {
521            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
522        },
523        arm64: {
524            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
525        },
526    },
527}
528
529cc_benchmark {
530    name: "linker-benchmarks",
531
532    srcs: [
533        "linker_gnu_hash_benchmark.cpp",
534    ],
535
536    arch: {
537        arm: {
538            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
539        },
540        arm64: {
541            srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
542        },
543    },
544}
545