platform_bionic/linker/Android.bp
Christopher R. Palmer 1d53e3197d linker: Add support for dynamic SHIM libraries
Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Tue Nov 3 16:44:44 2015 -0500

    linker: Add support for dynamic "shim" libs

    Add a new environment variable

    LD_SHIM_LIBS

    that is a colon (":") separated list of vertical bar ("|") separated pairs.
    The pairs are the name for a soinfo reference (executable or shared library)
    followed by the name of the shim library to load.  For example:

    LD_SHIM_LIBS=rmt_storage|libshim_ioprio.so:/system/lib/libicuuv.so|libshim_icu53.so

    will instruct the linker to load the dynamic library libshim_ioprio.so
    whenver rmt_storage is executed [*] and will load libshim_icu53.so whenever
    any executable or other shared library links against /system/lib/libicuuv.so.

    There are no restrictions against circular references.  In this example,
    libshim_icu53.so can link against libicuuv.so which provides a simple and
    convenient means of adding compatibility symbols.

    [*] Note that the absolute path is not available to the linker and therefore
    using the name of executables does depend on the invocation and therefore
    should only be used if absolutely necessary.  That is, running
    /system/bin/rmt_storage would not load any shim libs in this example because
    it does not match the name of the invocation of the command.

    If you have trouble determining the sonames being loaded, you can also set
    the environment variable LD_DEBUG=1 which will cause additional information
    to be logged to help trace the detection of the shim libs.

    Change-Id: I0ef80fa466167f7bcb7dac90842bef1c3cf879b6

Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Sun Nov 15 14:26:32 2015 -0500

    linker: Fix the fact that shim libs do not properly call constructors

    Change-Id: I34333e13443a154e675b853fa41442351bc4243a

Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Tue Dec 1 07:10:36 2015 -0500

    linker: Don't try to walk the g_active_shim_libs when doing dlsym

    This is a bug in the original shim_lib implementation which was
    doing the shim lib resolution both when loading the libraries
    and when doing the dynamic symbol resolution.

    Change-Id: Ib2df0498cf551b3bbd37d7c351410b9908eb1795

Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Sun Nov 29 08:28:10 2015 -0500

    linker: Reset the active shim libs each time we do a dlopen

    We use the active libs to avoid recursively trying to load the
    same library:

    A -> shimlibs add B -> depends on A -> shimlibs add B -> ...

    However, when we repeatedly dlopen the same library we need
    to reset the active shim libs to avoid failing to add B the
    second time we dlopen A.

    Change-Id: I27580e3d6a53858e8bca025d6c85f981cffbea06

Author: Danny Baumann <dannybaumann@web.de>
Date:   Fri Dec 11 10:29:16 2015 +0100

    Make shim lib load failure non-fatal.

    Instead, print an appropriate warning message. Aborting symbol
    resolution on shim lib load failure leads to weird symbol lookup
    failures, because symbols in libraries referenced after the one loading
    the shim won't be loaded anymore without a log message stating why that
    happened.

    Change-Id: Ic3ad7095ddae7ea1039cb6a18603d5cde8a16143

Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Sat Dec 12 06:10:09 2015 -0500

    bionic: Do not allow LD_SHIM_LIBS for setuid executables

    That's really not safe...

    Change-Id: If79af951830966fc21812cd0f60a8998a752a941

Author: Christopher R. Palmer <crpalmer@gmail.com>
Date:   Sun Feb 14 11:38:44 2016 -0500

    bionic: linker: Load shim libs *before* the self-linked libs

    By loading them earlier, this allows us to override a symbol in
    a library that is being directly linked.

    I believe this explains why some people have had problems shimming
    one lib but when the changet he shim to be against a different
    lib it magically works.

    It also makes it possible to override some symbols that were
    nearly impossible to override before this change.  For example, it is
    pretty much impossible to override a symbol in libutils without
    this change because it's loaded almost everywhere so no matter
    where you try to place the shimming, it will be too late and
    the other symbol will have priority.

    In particularly, this is necessary to be able to correctly
    shim the VectorImpl symbols for dlx.

    Change-Id: I461ca416bc288e28035352da00fde5f34f8d9ffa

Author: Chirayu Desai <chirayudesai1@gmail.com>
Date:   Thu Aug 25 19:02:41 2016 +0530

    linker: Update find_library call for shimlibs

    commits 0cdef7e7f3
    "Respect caller DT_RUNPATH in dlopen()."
    and 42d5fcb9f4
    "Introducing linker namespaces"
    added new arguments to find_library, add them here.

    Change-Id: I8f35a45b00d14f8b2ce01a0a96d2dc7759be04a6

Author: Chippa-a <vusal1372@gmail.com>
Date:   Sat Aug 27 14:56:30 2016 +0200

    linker: Update LD_SHIM_LIBS parser function

     * Upgrade the code using the same changes as
        42d5fcb9f4
        bda20e78f0

    Change-Id: Ic8be0871945bd9feccd0f94a6770f3cc78a70a0f

Author: Danny Baumann <dannybaumann@web.de>
Date:   Wed Sep 7 16:54:06 2016 +0200

    Inject shim libs as if they were DT_NEEDED.

    The previous separate approach had one flaw: If the shim lib requires
    another lib that's already loaded, find_library_internal() would return
    the previously loaded copy, but the later load action would fail as the
    ELF reader map of the initial loading round was already discarded and
    thus a new ElfReader instance for the soinfo instance was created, which
    didn't know about the previous reading/loading state.

    Change-Id: Ib224dbd35d114197097e3dee14a077cc9130fedb

Author: jrior001 <jriordan001@gmail.com>
Date:   Fri Oct 7 19:36:51 2016 -0400

    linker: load shims prior to DT_NEEDED check

    This allows shims to override existing symbols, not just
    inject new symbols.

    Change-Id: Ib9216bcc651d8d38999c593babb94d76dc1dbc95

Author: Adrian DC <radian.dc@gmail.com>
Date: Sat, 8 Apr 2017 22:40:01 +0200

     * Adapt to latest AOSP Oreo bionic linker changes
     * Additional header to avoid unused function

    Change-Id: Ib9216bcc651d8d38999c593babb94d76dc1dbc95

Author: Paul Keith <javelinanddart@gmail.com>
Date:   Thu Feb 15 21:57:33 2018 +0100

    linker: Move shims to TARGET_LD_SHIM_LIBS

    * To reduce security exposure, let's set this at compile time,
      and block off all the code unless the board flag is set

    Change-Id: Ieec5f5d9e0f39a798fd48eae037ecffe9502474c

Author: Nich <nctrenco@gmail.com>
Date:   Fri Jun 8 09:48:17 2018 +0800

    linker: Provide soinfo path of the shimmed binary

    This is a forward port of part of the original change that was missed out
    since the initial port of the shim logic to O.

    Change-Id: I1f7ff98472cfef5cb2d2bcb303082784898cd0c6

Author: Nich <nctrenco@gmail.com>
Date:   Tue Jun 5 13:36:43 2018 +0800

    linker: Remove unused find_libraries declaration

    commit "Inject shim libs as if they were DT_NEEDED." removed references
    to the forward declaration.

    Change-Id: I5f1aaa3a96f2af3edef07d4ea4e204b586424631

Author: Nich <nctrenco@gmail.com>
Date:   Sun Jun 10 00:45:51 2018 +0800

    linker: Make shim reference path absolute

    This way, we can filter out non-existent binaries, and ensure we get
    its absolute path before matching with get_realpath(). This for one
    allows the use of symlinks in TARGET_LD_SHIM_LIBS.

    Change-Id: I823815271b3257965534b6b87d8ea36ffb68bc08

Author: Nich <nctrenco@gmail.com>
Date:   Fri Jun 15 03:59:05 2018 +0800

    linker: Ensure active matching pairs

    Change-Id: I54c666b4560dbfb40839b0bf9132a7fd8d3ed2dd

Author: Nich <nctrenco@gmail.com>
Date:   Thu Jun 21 01:58:10 2018 +0800

    linker: Don't involve shim in for_each_dt_needed

    for_each_dt_needed may have other usages that shouldn't involve the
    shim, for example, in the unloading of soinfos.

    Change-Id: Id38de183d90c3f707767bdca032a5ea2bc82fde8

Author: Jiyong Park <jiyong@google.com>
Date:   Fri Jan 25 18:18:01 2019 +0900

    Call realpath(3) only when the path is accessible for read

    Suppress the SELinux denial log spam by not calling realpath(3) when the
    path does not exist or is not accessible for read, and then not auditing
    access(2) failure.

    Change-Id: I729ecb8ea0bb581069eb849bae7cd28e6ab636cc

Change-Id: Ic3ad7095ddae7ea1039cb6a18603d5cde8a16152
Signed-off-by: Wang Han <416810799@qq.com>
2024-09-07 23:54:33 +02:00

627 lines
16 KiB
Text

// ========================================================
// linker_wrapper - Linux Bionic (on the host)
// ========================================================
// This is used for bionic on (host) Linux to bootstrap our linker embedded into
// a binary.
//
// Host bionic binaries do not have a PT_INTERP section, instead this gets
// embedded as the entry point, and the linker is embedded as ELF sections in
// each binary. There's a linker script that sets all of that up (generated by
// extract_linker), and defines the extern symbols used in this file.
package {
default_team: "trendy_team_native_tools_libraries",
default_applicable_licenses: ["bionic_linker_license"],
}
license {
name: "bionic_linker_license",
visibility: [":__subpackages__"],
license_kinds: [
"SPDX-license-identifier-BSD",
],
license_text: [
"NOTICE",
],
}
cc_object {
name: "linker_wrapper",
host_supported: true,
device_supported: false,
enabled: false,
target: {
linux_bionic: {
enabled: true,
},
},
cflags: [
"-fno-stack-protector",
"-Wstrict-overflow=5",
"-fvisibility=hidden",
"-Wall",
"-Wextra",
"-Wno-unused",
"-Werror",
],
srcs: [
"linker_wrapper.cpp",
],
arch: {
arm64: {
srcs: ["arch/arm64/linker_wrapper_begin.S"],
},
riscv64: {
srcs: ["arch/riscv64/linker_wrapper_begin.S"],
},
x86_64: {
srcs: ["arch/x86_64/linker_wrapper_begin.S"],
},
},
header_libs: ["libc_headers"],
// We need to access Bionic private headers in the linker.
include_dirs: ["bionic/libc"],
}
// ========================================================
// linker default configuration
// ========================================================
// Configuration for the linker binary and any of its static libraries.
cc_defaults {
name: "linker_defaults",
defaults: [
"shim_libs_defaults",
],
arch: {
arm: {
cflags: ["-D__work_around_b_24465209__"],
},
x86: {
cflags: ["-D__work_around_b_24465209__"],
},
},
cflags: [
"-fno-stack-protector",
"-Wstrict-overflow=5",
"-fvisibility=hidden",
"-Wall",
"-Wextra",
"-Wunused",
"-Werror",
],
// TODO: split out the asflags.
asflags: [
"-fno-stack-protector",
"-Wstrict-overflow=5",
"-fvisibility=hidden",
"-Wall",
"-Wextra",
"-Wunused",
"-Werror",
],
product_variables: {
debuggable: {
cppflags: ["-DUSE_LD_CONFIG_FILE"],
},
},
cppflags: ["-Wold-style-cast"],
static_libs: [
"libziparchive",
"libbase",
"libz",
"libasync_safe",
"liblog_for_runtime_apex",
],
// We need to access Bionic private headers in the linker.
include_dirs: ["bionic/libc"],
}
// ========================================================
// linker components
// ========================================================
// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and
// native bridge implementations).
cc_defaults {
name: "linker_all_targets",
defaults: ["linux_bionic_supported"],
recovery_available: true,
vendor_ramdisk_available: true,
native_bridge_supported: true,
}
cc_library_static {
name: "liblinker_main",
defaults: [
"linker_defaults",
"linker_all_targets",
],
srcs: ["linker_main.cpp"],
// Ensure that the compiler won't insert string function calls before ifuncs are resolved.
cflags: ["-ffreestanding"],
apex_available: [
"com.android.runtime",
],
}
cc_library_static {
name: "liblinker_malloc",
defaults: [
"linker_defaults",
"linker_all_targets",
],
srcs: ["linker_memory.cpp"],
apex_available: [
"com.android.runtime",
],
}
cc_library_static {
name: "liblinker_debuggerd_stub",
defaults: [
"linker_defaults",
"linker_all_targets",
],
srcs: ["linker_debuggerd_stub.cpp"],
}
// ========================================================
// template for the linker binary
// ========================================================
filegroup {
name: "linker_sources",
srcs: [
"dlfcn.cpp",
"linker.cpp",
"linker_auxv.cpp",
"linker_block_allocator.cpp",
"linker_dlwarning.cpp",
"linker_cfi.cpp",
"linker_config.cpp",
"linker_debug.cpp",
"linker_gdb_support.cpp",
"linker_globals.cpp",
"linker_libc_support.c",
"linker_libcxx_support.cpp",
"linker_namespaces.cpp",
"linker_logger.cpp",
"linker_mapped_file_fragment.cpp",
"linker_note_gnu_property.cpp",
"linker_phdr.cpp",
"linker_relocate.cpp",
"linker_sdk_versions.cpp",
"linker_soinfo.cpp",
"linker_transparent_hugepage_support.cpp",
"linker_tls.cpp",
"linker_utils.cpp",
"rt.cpp",
],
}
filegroup {
name: "linker_sources_arm",
srcs: [
"arch/arm/begin.S",
"arch/arm_neon/linker_gnu_hash_neon.cpp",
],
}
filegroup {
name: "linker_sources_arm64",
srcs: [
"arch/arm64/begin.S",
"arch/arm64/tlsdesc_resolver.S",
"arch/arm_neon/linker_gnu_hash_neon.cpp",
],
}
filegroup {
name: "linker_sources_riscv64",
srcs: [
"arch/riscv64/begin.S",
"arch/riscv64/tlsdesc_resolver.S",
],
}
filegroup {
name: "linker_sources_x86",
srcs: [
"arch/x86/begin.S",
],
}
filegroup {
name: "linker_sources_x86_64",
srcs: [
"arch/x86_64/begin.S",
],
}
cc_defaults {
name: "linker_version_script_overlay",
arch: {
arm: {
version_script: "linker.arm.map",
},
arm64: {
version_script: "linker.generic.map",
},
riscv64: {
version_script: "linker.generic.map",
},
x86: {
version_script: "linker.generic.map",
},
x86_64: {
version_script: "linker.generic.map",
},
},
}
// A template for the linker binary. May be inherited by native bridge implementations.
cc_defaults {
name: "linker_bin_template",
defaults: ["linker_defaults"],
srcs: [":linker_sources"],
arch: {
arm: {
srcs: [":linker_sources_arm"],
// Arm 32 bit does not produce complete exidx unwind information
// so keep the .debug_frame which is relatively small and does
// include needed unwind information.
// See b/242162222 for details.
strip: {
keep_symbols_and_debug_frame: true,
},
},
arm64: {
srcs: [":linker_sources_arm64"],
// Leave the symbols in the shared library so that stack unwinders can produce
// meaningful name resolution.
strip: {
keep_symbols: true,
},
},
riscv64: {
srcs: [":linker_sources_riscv64"],
// Leave the symbols in the shared library so that stack unwinders can produce
// meaningful name resolution.
strip: {
keep_symbols: true,
},
},
x86: {
srcs: [":linker_sources_x86"],
// Leave the symbols in the shared library so that stack unwinders can produce
// meaningful name resolution.
strip: {
keep_symbols: true,
},
},
x86_64: {
srcs: [":linker_sources_x86_64"],
// Leave the symbols in the shared library so that stack unwinders can produce
// meaningful name resolution.
strip: {
keep_symbols: true,
},
},
},
// -shared is used to overwrite the -Bstatic and -static flags triggered by enabling
// static_executable. This dynamic linker is actually a shared object linked with static
// libraries.
ldflags: [
"-shared",
"-Wl,-Bsymbolic",
"-Wl,--exclude-libs,ALL",
"-Wl,-soname,ld-android.so",
// When the linker applies its own IRELATIVE relocations, it will only read DT_REL[A] and
// DT_JMPREL, not DT_ANDROID_REL[A], which can also theoretically contain IRELATIVE
// relocations. lld has been taught to not store them there as a bug workaround (see
// https://llvm.org/pr86751) but the workaround could be removed at some point in the
// future. So we explicitly prevent it from doing so by disabling DT_ANDROID_REL[A] when
// linking the linker (DT_RELR cannot encode IRELATIVE relocations).
"-Wl,--pack-dyn-relocs=relr",
],
// we are going to link libc++_static manually because
// when stl is not set to "none" build system adds libdl
// to the list of static libraries which needs to be
// avoided in the case of building loader.
stl: "none",
// we don't want crtbegin.o (because we have begin.o), so unset it
// just for this module
nocrt: true,
static_executable: true,
// Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb
// looking up symbols in the linker by mistake.
prefix_symbols: "__dl_",
sanitize: {
hwaddress: false,
},
static_libs: [
"liblinker_main",
"liblinker_malloc",
// Use a version of libc++ built without exceptions, because accessing EH globals uses
// ELF TLS, which is not supported in the loader.
"libc++_static_noexcept",
"libc_nomalloc",
"libc_dynamic_dispatch",
"libm",
"libunwind",
],
// Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a
// definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak
// reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition
// of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a
// non-relative dynamic relocation in the linker binary, which complicates linker startup.
//
// This line should be unnecessary because the linker's dependency on libunwind_llvm.a should
// override libgcc.a, but this line provides a simpler guarantee. It can be removed once the
// linker stops linking against libgcc.a's arm32 unwinder.
whole_static_libs: ["libc_unwind_static"],
system_shared_libs: [],
// Opt out of native_coverage when opting out of system_shared_libs
native_coverage: false,
}
// ========================================================
// linker[_asan][64] binary
// ========================================================
cc_binary {
name: "linker",
defaults: [
"linker_bin_template",
"linux_bionic_supported",
"linker_version_script_overlay",
],
srcs: [
"linker_translate_path.cpp",
],
symlinks: ["linker_asan"],
arch: {
arm64: {
symlinks: ["linker_hwasan"],
},
},
multilib: {
lib64: {
suffix: "64",
},
},
compile_multilib: "both",
recovery_available: true,
vendor_ramdisk_available: true,
apex_available: [
"//apex_available:platform",
"com.android.runtime",
],
target: {
android: {
srcs: [
"linker_debuggerd_android.cpp",
],
static_libs: [
"libc++demangle_noexcept",
"libdebuggerd_handler_fallback",
],
},
linux_bionic: {
static_libs: [
"liblinker_debuggerd_stub",
],
},
},
afdo: true,
// FIXME: Workaround compat issue with obfuscation libraries.
// http://b/352456802
lto_O0: true,
}
// ========================================================
// assorted modules
// ========================================================
sh_binary {
name: "ldd",
src: "ldd.sh",
}
// Used to generate binaries that can be backed by transparent hugepages.
cc_defaults {
name: "linker_hugepage_aligned",
arch: {
arm64: {
ldflags: ["-z max-page-size=0x200000"],
},
x86_64: {
ldflags: ["-z max-page-size=0x200000"],
},
},
}
cc_library {
// NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from
// libgcc.a are made static to ld-android.so. This in turn ensures that libraries that
// a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so
// to provide those symbols, but will instead pull them from libgcc.a. Specifically,
// we use this property to make sure libc.so has its own copy of the code from
// libgcc.a it uses.
//
// DO NOT REMOVE --exclude-libs!
ldflags: [
"-Wl,--exclude-libs=libgcc.a",
"-Wl,--exclude-libs=libgcc_stripped.a",
"-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
"-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
"-Wl,--exclude-libs=libclang_rt.builtins-riscv64-android.a",
"-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
"-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
],
// for x86, exclude libgcc_eh.a for the same reasons as above
arch: {
x86: {
ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
},
x86_64: {
ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
},
},
srcs: ["ld_android.cpp"],
cflags: [
"-Wall",
"-Wextra",
"-Wunused",
"-Werror",
],
stl: "none",
name: "ld-android",
defaults: [
"linux_bionic_supported",
"linker_version_script_overlay",
],
ramdisk_available: true,
vendor_ramdisk_available: true,
recovery_available: true,
native_bridge_supported: true,
nocrt: true,
system_shared_libs: [],
header_libs: ["libc_headers"],
// Opt out of native_coverage when opting out of system_shared_libs
native_coverage: false,
sanitize: {
never: true,
},
apex_available: [
"//apex_available:platform",
"com.android.runtime",
],
}
cc_test {
name: "linker-unit-tests",
test_suites: ["device-tests"],
cflags: [
"-g",
"-Wall",
"-Wextra",
"-Wunused",
"-Werror",
],
// We need to access Bionic private headers in the linker.
include_dirs: ["bionic/libc"],
srcs: [
// Tests.
"linker_block_allocator_test.cpp",
"linker_config_test.cpp",
"linked_list_test.cpp",
"linker_note_gnu_property_test.cpp",
"linker_sleb128_test.cpp",
"linker_utils_test.cpp",
"linker_gnu_hash_test.cpp",
"linker_crt_pad_segment_test.cpp",
// Parts of the linker that we're testing.
":elf_note_sources",
"linker_block_allocator.cpp",
"linker_config.cpp",
"linker_debug.cpp",
"linker_note_gnu_property.cpp",
"linker_test_globals.cpp",
"linker_utils.cpp",
"linker_phdr.cpp",
"linker_mapped_file_fragment.cpp",
"linker_sdk_versions.cpp",
"linker_dlwarning.cpp",
],
static_libs: [
"libasync_safe",
"libbase",
"liblog_for_runtime_apex",
"libprocinfo", // For procinfo::MappedFileSize()
],
data_libs: [
"crt_pad_segment_disabled",
"crt_pad_segment_enabled",
"no_crt_pad_segment",
],
arch: {
arm: {
srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
},
arm64: {
srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
},
},
}
cc_benchmark {
name: "linker-benchmarks",
srcs: [
"linker_gnu_hash_benchmark.cpp",
],
arch: {
arm: {
srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
},
arm64: {
srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"],
},
},
}