Make libdl.so be loaded after libc.so

Make sure that libdl is always after libc on the command line.
Simplifies the logic to always support system_shared_libs for
sdk and vndk builds.

For backwards compatibility without updating lots of Android.bp
files, allow libdl to be listed in shared_libs as long as it
is also in system_shared_libs or libc is not in system_shared_libs.

Remove all the places that libdl is added as a dependency, since
it will always be present unless explicitly removed now.

Bug: 62815515
Test: m -j checkbuild
Change-Id: I0233178ffea87a2f0b82190746022476304a68e2
This commit is contained in:
Colin Cross 2017-08-18 16:52:25 -07:00 committed by Justin Yun
parent 0b062130dc
commit ef88ae2369
3 changed files with 25 additions and 38 deletions

View file

@ -48,8 +48,8 @@ type BaseLinkerProperties struct {
No_default_compiler_flags *bool No_default_compiler_flags *bool
// list of system libraries that will be dynamically linked to // list of system libraries that will be dynamically linked to
// shared library and executable modules. If unset, generally defaults to libc // shared library and executable modules. If unset, generally defaults to libc,
// and libm. Set to [] to prevent linking against libc and libm. // libm, and libdl. Set to [] to prevent linking against the defaults.
System_shared_libs []string System_shared_libs []string
// allow the module to contain undefined symbols. By default, // allow the module to contain undefined symbols. By default,
@ -153,34 +153,32 @@ func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
} }
if !ctx.static() { if !ctx.static() {
// libdl should always appear after libc in dt_needed list - see below systemSharedLibs := linker.Properties.System_shared_libs
// the only exception is when libc is not in linker.Properties.System_shared_libs if systemSharedLibs == nil {
// such as for libc module itself systemSharedLibs = []string{"libc", "libm", "libdl"}
if inList("libc", linker.Properties.System_shared_libs) {
_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
} }
if linker.Properties.System_shared_libs != nil { if inList("libdl", deps.SharedLibs) {
if !inList("libdl", linker.Properties.System_shared_libs) && // If system_shared_libs has libc but not libdl, make sure shared_libs does not
inList("libc", linker.Properties.System_shared_libs) { // have libdl to avoid loading libdl before libc.
linker.Properties.System_shared_libs = append(linker.Properties.System_shared_libs, if inList("libc", systemSharedLibs) {
"libdl") if !inList("libdl", systemSharedLibs) {
ctx.PropertyErrorf("shared_libs",
"libdl must be in system_shared_libs, not shared_libs")
}
_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
} }
deps.LateSharedLibs = append(deps.LateSharedLibs,
linker.Properties.System_shared_libs...)
} else if !ctx.sdk() && !ctx.vndk() {
deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
} }
}
if ctx.sdk() { // If libc and libdl are both in system_shared_libs make sure libd comes after libc
deps.SharedLibs = append(deps.SharedLibs, // to avoid loading libdl before libc.
"libc", if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
"libm", indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
"libdl", ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
) }
}
if ctx.vndk() { deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
} else if ctx.sdk() || ctx.vndk() {
deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl") deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
} }
} }

View file

@ -264,9 +264,6 @@ func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
if Bool(sanitize.Properties.Sanitize.Address) { if Bool(sanitize.Properties.Sanitize.Address) {
deps.StaticLibs = append(deps.StaticLibs, asanLibs...) deps.StaticLibs = append(deps.StaticLibs, asanLibs...)
} }
if Bool(sanitize.Properties.Sanitize.Address) || Bool(sanitize.Properties.Sanitize.Thread) {
deps.SharedLibs = append(deps.SharedLibs, "libdl")
}
} }
return deps return deps

View file

@ -107,8 +107,6 @@ func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
} }
if ctx.staticBinary() { if ctx.staticBinary() {
deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl") deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
} else {
deps.SharedLibs = append(deps.SharedLibs, "libdl")
} }
} }
case "": case "":
@ -118,15 +116,9 @@ func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
// The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
// its own includes. The includes are handled in CCBase.Flags(). // its own includes. The includes are handled in CCBase.Flags().
deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...) deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
case "ndk_libc++_shared": case "ndk_libc++_shared", "ndk_libstlport_shared":
deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl,
"libdl")
case "ndk_libc++_static":
deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
deps.SharedLibs = append(deps.SharedLibs, "libdl")
case "ndk_libstlport_shared":
deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl) deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
case "ndk_libstlport_static", "ndk_libgnustl_static": case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl) deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
default: default:
panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl)) panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))