From b49fe1bde729e66e0c7766c2ac4a5ef1b426e5f7 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Fri, 11 Oct 2019 15:03:34 -0700 Subject: [PATCH] Stop linking libdl.a into static bins libdl.a has a no-op dlopen, which breaks static libraries that need a real dlopen. Instead of automatically linking libdl.a into static executables, make it optional. Until recently, the libunwind_llvm.a unwinder, used on arm32, needed the no-op dladdr, but it's now built using -D_LIBUNWIND_USE_DLADDR=0. The HWASan run-time uses dlsym and dladdr, so add a libdl dependency for HWASan-built static binaries. We could also remove the dependency from libclang_rt.hwasan_static-*.a, but this is also easy to do. Bug: http://b/141485154 Test: bionic unit tests, device boots, verify that static and dynamic executables can throw/catch an exception Test: verify that a static executable using dlopen doesn't link (unless it adds an explicit dependency on libdl) Change-Id: Ic52c3f336b671b4ed335e99c94a64dfe8614b618 --- cc/binary.go | 2 +- cc/cc_test.go | 2 +- cc/sanitize.go | 4 +++- cc/stl.go | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cc/binary.go b/cc/binary.go index 9f18d6c8a..b27142cf7 100644 --- a/cc/binary.go +++ b/cc/binary.go @@ -158,7 +158,7 @@ func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { if binary.static() { if ctx.selectedStl() == "libc++_static" { - deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") + deps.StaticLibs = append(deps.StaticLibs, "libm", "libc") } // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, diff --git a/cc/cc_test.go b/cc/cc_test.go index 0649c2d72..56078d948 100644 --- a/cc/cc_test.go +++ b/cc/cc_test.go @@ -2239,7 +2239,7 @@ func TestStaticExecutable(t *testing.T) { variant := "android_arm64_armv8-a_core" binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld") libFlags := binModuleRule.Args["libFlags"] - systemStaticLibs := []string{"libc.a", "libm.a", "libdl.a"} + systemStaticLibs := []string{"libc.a", "libm.a"} for _, lib := range systemStaticLibs { if !strings.Contains(libFlags, lib) { t.Errorf("Static lib %q was not found in %q", lib, libFlags) diff --git a/cc/sanitize.go b/cc/sanitize.go index 5172fc8da..3f0f0f498 100644 --- a/cc/sanitize.go +++ b/cc/sanitize.go @@ -848,12 +848,14 @@ func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { // Determine the runtime library required runtimeLibrary := "" + var extraStaticDeps []string toolchain := c.toolchain(mctx) if Bool(c.sanitize.Properties.Sanitize.Address) { runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain) } else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) { if c.staticBinary() { runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain) + extraStaticDeps = []string{"libdl"} } else { runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain) } @@ -887,7 +889,7 @@ func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) { mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ {Mutator: "link", Variation: "static"}, {Mutator: "image", Variation: c.imageVariation()}, - }...), staticDepTag, runtimeLibrary) + }...), staticDepTag, append([]string{runtimeLibrary}, extraStaticDeps...)...) } else if !c.static() && !c.header() { // dynamic executable and shared libs get shared runtime libs mctx.AddFarVariationDependencies(append(mctx.Target().Variations(), []blueprint.Variation{ diff --git a/cc/stl.go b/cc/stl.go index aa34240a0..f9273e1a5 100644 --- a/cc/stl.go +++ b/cc/stl.go @@ -175,7 +175,7 @@ func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps { deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm") } if ctx.staticBinary() { - deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") + deps.StaticLibs = append(deps.StaticLibs, "libm", "libc") } } case "":