[llvm] [ORC] Add automatic shared library resolver for unresolved symbols. (PR #148410)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 7 22:03:51 PDT 2025
================
@@ -0,0 +1,485 @@
+//===- LibraryResolver.h - Automatic Dynamic Library Symbol Resolution -*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides support for automatically searching symbols across
+// dynamic libraries that have not yet been loaded.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_DYNAMICLOADER_H
+#define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_DYNAMICLOADER_H
+
+#include "llvm/ADT/FunctionExtras.h"
+#include "llvm/ExecutionEngine/Orc/Shared/SymbolFilter.h"
+#include "llvm/ExecutionEngine/Orc/TargetProcess/LibraryScanner.h"
+#include "llvm/Support/Path.h"
+
+#include <atomic>
+#include <shared_mutex>
+#include <unordered_map>
+
+namespace llvm {
+namespace orc {
+
+/// Manages library metadata and state for symbol resolution.
+///
+/// Tracks libraries by load state and kind (user/system), and stores
+/// associated Bloom filters and hash maps to speed up symbol lookups.
+/// Thread-safe for concurrent access.
+class LibraryManager {
+public:
+ enum class State : uint8_t { Unloaded = 0, Loaded = 1, Queried = 2 };
+
+ class LibraryInfo {
+ public:
+ LibraryInfo(const LibraryInfo &) = delete;
+ LibraryInfo &operator=(const LibraryInfo &) = delete;
+
+ LibraryInfo(std::string filePath, State s, PathType k,
+ std::optional<BloomFilter> filter = std::nullopt)
+ : filePath(std::move(filePath)), state(s), kind(k),
+ filter(std::move(filter)) {}
+
+ StringRef getBasePath() const { return sys::path::parent_path(filePath); }
+ StringRef getFileName() const { return sys::path::filename(filePath); }
+
+ std::string getFullPath() const { return filePath; }
+
+ bool setFilter(BloomFilter F) {
+ std::lock_guard<std::shared_mutex> lock(mutex);
+ if (filter)
+ return false;
+ filter.emplace(std::move(F));
+ return true;
+ }
+
+ bool ensureFilterBuilt(const BloomFilterBuilder &FB,
+ const std::vector<std::string> &symbols) {
+ std::lock_guard<std::shared_mutex> lock(mutex);
+ if (filter)
+ return false;
+ filter.emplace(FB.build(symbols));
+ return true;
+ }
+
+ bool mayContain(StringRef symbol) const {
+ assert(hasFilter());
+ std::shared_lock<std::shared_mutex> lock(mutex);
+ return filter->mayContain(symbol);
+ }
+
+ bool hasFilter() const {
+ std::shared_lock<std::shared_mutex> lock(mutex);
+ return filter.has_value();
+ }
+
+ State getState() const { return state.load(); }
+ PathType getKind() const { return kind; }
+
+ void setState(State s) { state.store(s); }
+
+ bool operator==(const LibraryInfo &other) const {
+ return filePath == other.filePath;
+ }
+
+ private:
+ std::string filePath;
+ std::atomic<State> state;
+ PathType kind;
+ std::optional<BloomFilter> filter;
+ mutable std::shared_mutex mutex;
+ };
+
+ class FilteredView {
+ public:
+ using Map = StringMap<std::shared_ptr<LibraryInfo>>;
+ using Iterator = typename Map::const_iterator;
+ class FilterIterator {
+ public:
+ FilterIterator(Iterator _it, Iterator _end, State s, PathType k)
+ : it(_it), end(_end), state(s), kind(k) {
+ advance();
+ }
+
+ bool operator!=(const FilterIterator &other) const {
+ return it != other.it;
+ }
+ const std::shared_ptr<LibraryInfo> &operator*() const {
+ return it->second;
+ }
+
+ FilterIterator &operator++() {
+ ++it;
+ advance();
+ return *this;
+ }
+
+ private:
+ void advance() {
+ while (it != end) {
+ const auto &lib = it->second;
+ if (lib->getState() == state && lib->getKind() == kind)
+ break;
+ ++it;
+ }
----------------
SahilPatidar wrote:
Like this :
```cpp
for (; it != end; ++it)
if (it->second->getState() == state && it->second->getKind() == kind)
break;
```
https://github.com/llvm/llvm-project/pull/148410
More information about the llvm-commits
mailing list