[PATCH] D124815: [libclang] Fall back to getMainExecutable when dladdr fails

Ayke via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun May 29 04:41:24 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG75d12e49c729: [libclang] Fall back to getMainExecutable when dladdr fails (authored by aykevl).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124815/new/

https://reviews.llvm.org/D124815

Files:
  clang/tools/libclang/CIndexer.cpp


Index: clang/tools/libclang/CIndexer.cpp
===================================================================
--- clang/tools/libclang/CIndexer.cpp
+++ clang/tools/libclang/CIndexer.cpp
@@ -125,13 +125,23 @@
 #elif defined(_AIX)
   getClangResourcesPathImplAIX(LibClangPath);
 #else
-  // This silly cast below avoids a C++ warning.
   Dl_info info;
-  if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
-    llvm_unreachable("Call to dladdr() failed");
+  std::string Path;
+  // This silly cast below avoids a C++ warning.
+  if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) != 0) {
+    // We now have the CIndex directory, locate clang relative to it.
+    LibClangPath += info.dli_fname;
+  } else if (!(Path = llvm::sys::fs::getMainExecutable(nullptr, nullptr)).empty()) {
+    // If we can't get the path using dladdr, try to get the main executable
+    // path. This may be needed when we're statically linking libclang with
+    // musl libc, for example.
+    LibClangPath += Path;
+  } else {
+    // It's rather unlikely we end up here. But it could happen, so report an
+    // error instead of crashing.
+    llvm::report_fatal_error("Could not locate Clang resource path");
+  }
 
-  // We now have the CIndex directory, locate clang relative to it.
-  LibClangPath += info.dli_fname;
 #endif
 
   // Cache our result.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124815.432779.patch
Type: text/x-patch
Size: 1372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220529/fb04570a/attachment.bin>


More information about the cfe-commits mailing list