EPCDynamicLibrarySearchGenerator.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
  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. #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
  9. namespace llvm {
  10. namespace orc {
  11. Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
  12. EPCDynamicLibrarySearchGenerator::Load(ExecutionSession &ES,
  13. const char *LibraryPath,
  14. SymbolPredicate Allow) {
  15. auto Handle = ES.getExecutorProcessControl().loadDylib(LibraryPath);
  16. if (!Handle)
  17. return Handle.takeError();
  18. return std::make_unique<EPCDynamicLibrarySearchGenerator>(ES, *Handle,
  19. std::move(Allow));
  20. }
  21. Error EPCDynamicLibrarySearchGenerator::tryToGenerate(
  22. LookupState &LS, LookupKind K, JITDylib &JD,
  23. JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
  24. if (Symbols.empty())
  25. return Error::success();
  26. SymbolLookupSet LookupSymbols;
  27. for (auto &KV : Symbols) {
  28. // Skip symbols that don't match the filter.
  29. if (Allow && !Allow(KV.first))
  30. continue;
  31. LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
  32. }
  33. SymbolMap NewSymbols;
  34. ExecutorProcessControl::LookupRequest Request(H, LookupSymbols);
  35. auto Result = EPC.lookupSymbols(Request);
  36. if (!Result)
  37. return Result.takeError();
  38. assert(Result->size() == 1 && "Results for more than one library returned");
  39. assert(Result->front().size() == LookupSymbols.size() &&
  40. "Result has incorrect number of elements");
  41. auto ResultI = Result->front().begin();
  42. for (auto &KV : LookupSymbols) {
  43. if (*ResultI)
  44. NewSymbols[KV.first] =
  45. JITEvaluatedSymbol(ResultI->getValue(), JITSymbolFlags::Exported);
  46. ++ResultI;
  47. }
  48. // If there were no resolved symbols bail out.
  49. if (NewSymbols.empty())
  50. return Error::success();
  51. // Define resolved symbols.
  52. return JD.define(absoluteSymbols(std::move(NewSymbols)));
  53. }
  54. } // end namespace orc
  55. } // end namespace llvm