Fix dlopen of main executable by absolute path
This CL adds initialization of inode for the main executable
which enables linker to resolve the correct soinfo when
application calls dlopen with absolute path to the
main executable.
Bug: http://b/28420266
Change-Id: I102e07bde454bd44c6e46075e3faeeb5092830d8
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 748d0ca..56df1a6 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -805,15 +805,12 @@
ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
// Get the name of this executable.
- char executable_path[PATH_MAX];
- rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
- ASSERT_NE(rc, -1);
- executable_path[rc] = '\0';
+ const std::string& executable_path = get_executable_path();
// The filename should be that of this executable.
char dli_realpath[PATH_MAX];
ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
- ASSERT_STREQ(executable_path, dli_realpath);
+ ASSERT_STREQ(executable_path.c_str(), dli_realpath);
// The symbol name should be the symbol we looked up.
ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
@@ -838,6 +835,22 @@
ASSERT_EQ(0, dlclose(self));
}
+TEST(dlfcn, dlopen_executable_by_absolute_path) {
+ void* handle1 = dlopen(nullptr, RTLD_NOW);
+ ASSERT_TRUE(handle1 != nullptr) << dlerror();
+
+ void* handle2 = dlopen(get_executable_path().c_str(), RTLD_NOW);
+ ASSERT_TRUE(handle2 != nullptr) << dlerror();
+
+#if defined(__BIONIC__)
+ ASSERT_EQ(handle1, handle2);
+#else
+ GTEST_LOG_(INFO) << "Skipping ASSERT_EQ(handle1, handle2) for glibc: "
+ "it loads a separate copy of the main executable "
+ "on dlopen by absolute path.";
+#endif
+}
+
#if defined(__LP64__)
#define PATH_TO_SYSTEM_LIB "/system/lib64/"
#else