| 1 | //===------- LookupAndRecordAddrs.h - Symbol lookup support utility -------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h" |
| 10 | |
| 11 | #include <future> |
| 12 | |
| 13 | namespace llvm { |
| 14 | namespace orc { |
| 15 | |
| 16 | void lookupAndRecordAddrs( |
| 17 | unique_function<void(Error)> OnRecorded, ExecutionSession &ES, LookupKind K, |
| 18 | const JITDylibSearchOrder &SearchOrder, |
| 19 | std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs, |
| 20 | SymbolLookupFlags LookupFlags) { |
| 21 | |
| 22 | SymbolLookupSet Symbols; |
| 23 | for (auto &KV : Pairs) |
| 24 | Symbols.add(Name: KV.first, Flags: LookupFlags); |
| 25 | |
| 26 | ES.lookup( |
| 27 | K, SearchOrder, Symbols: std::move(Symbols), RequiredState: SymbolState::Ready, |
| 28 | NotifyComplete: [Pairs = std::move(Pairs), |
| 29 | OnRec = std::move(OnRecorded)](Expected<SymbolMap> Result) mutable { |
| 30 | if (!Result) |
| 31 | return OnRec(Result.takeError()); |
| 32 | for (auto &KV : Pairs) { |
| 33 | auto I = Result->find(Val: KV.first); |
| 34 | *KV.second = |
| 35 | I != Result->end() ? I->second.getAddress() : orc::ExecutorAddr(); |
| 36 | } |
| 37 | OnRec(Error::success()); |
| 38 | }, |
| 39 | RegisterDependencies: NoDependenciesToRegister); |
| 40 | } |
| 41 | |
| 42 | Error lookupAndRecordAddrs( |
| 43 | ExecutionSession &ES, LookupKind K, const JITDylibSearchOrder &SearchOrder, |
| 44 | std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs, |
| 45 | SymbolLookupFlags LookupFlags) { |
| 46 | |
| 47 | std::promise<MSVCPError> ResultP; |
| 48 | auto ResultF = ResultP.get_future(); |
| 49 | lookupAndRecordAddrs(OnRecorded: [&](Error Err) { ResultP.set_value(std::move(Err)); }, |
| 50 | ES, K, SearchOrder, Pairs: std::move(Pairs), LookupFlags); |
| 51 | return ResultF.get(); |
| 52 | } |
| 53 | |
| 54 | Error lookupAndRecordAddrs( |
| 55 | ExecutorProcessControl &EPC, tpctypes::DylibHandle H, |
| 56 | std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs, |
| 57 | SymbolLookupFlags LookupFlags) { |
| 58 | |
| 59 | SymbolLookupSet Symbols; |
| 60 | for (auto &KV : Pairs) |
| 61 | Symbols.add(Name: KV.first, Flags: LookupFlags); |
| 62 | |
| 63 | DylibManager::LookupRequest LR(H, Symbols); |
| 64 | auto Result = EPC.getDylibMgr().lookupSymbols(Request: LR); |
| 65 | if (!Result) |
| 66 | return Result.takeError(); |
| 67 | |
| 68 | if (Result->size() != 1) |
| 69 | return make_error<StringError>(Args: "Error in lookup result" , |
| 70 | Args: inconvertibleErrorCode()); |
| 71 | if (Result->front().size() != Pairs.size()) |
| 72 | return make_error<StringError>(Args: "Error in lookup result elements" , |
| 73 | Args: inconvertibleErrorCode()); |
| 74 | |
| 75 | for (unsigned I = 0; I != Pairs.size(); ++I) |
| 76 | *Pairs[I].second = Result->front()[I].getAddress(); |
| 77 | |
| 78 | return Error::success(); |
| 79 | } |
| 80 | |
| 81 | } // End namespace orc. |
| 82 | } // End namespace llvm. |
| 83 | |