aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/lib/ExecutionEngine/Orc/LookupAndRecordAddrs.cpp
blob: 44cb78c773c9fb6bec5eccf370a058aea6c41457 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//===------- LookupAndRecordAddrs.h - Symbol lookup support utility -------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"

#include <future>

namespace llvm {
namespace orc {

void lookupAndRecordAddrs(
    unique_function<void(Error)> OnRecorded, ExecutionSession &ES, LookupKind K,
    const JITDylibSearchOrder &SearchOrder,
    std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs,
    SymbolLookupFlags LookupFlags) {

  SymbolLookupSet Symbols;
  for (auto &KV : Pairs)
    Symbols.add(KV.first, LookupFlags);

  ES.lookup(
      K, SearchOrder, Symbols, SymbolState::Ready,
      [Pairs = std::move(Pairs),
       OnRec = std::move(OnRecorded)](Expected<SymbolMap> Result) mutable {
        if (!Result)
          return OnRec(Result.takeError());
        for (auto &KV : Pairs) {
          auto I = Result->find(KV.first);
          KV.second->setValue((I != Result->end()) ? I->second.getAddress()
                                                   : 0);
        }
        OnRec(Error::success());
      },
      NoDependenciesToRegister);
}

Error lookupAndRecordAddrs(
    ExecutionSession &ES, LookupKind K, const JITDylibSearchOrder &SearchOrder,
    std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs,
    SymbolLookupFlags LookupFlags) {

  std::promise<MSVCPError> ResultP;
  auto ResultF = ResultP.get_future();
  lookupAndRecordAddrs([&](Error Err) { ResultP.set_value(std::move(Err)); },
                       ES, K, SearchOrder, Pairs, LookupFlags);
  return ResultF.get();
}

Error lookupAndRecordAddrs(
    ExecutorProcessControl &EPC, tpctypes::DylibHandle H,
    std::vector<std::pair<SymbolStringPtr, ExecutorAddr *>> Pairs,
    SymbolLookupFlags LookupFlags) {

  SymbolLookupSet Symbols;
  for (auto &KV : Pairs)
    Symbols.add(KV.first, LookupFlags);

  ExecutorProcessControl::LookupRequest LR(H, Symbols);
  auto Result = EPC.lookupSymbols(LR);
  if (!Result)
    return Result.takeError();

  if (Result->size() != 1)
    return make_error<StringError>("Error in lookup result",
                                   inconvertibleErrorCode());
  if (Result->front().size() != Pairs.size())
    return make_error<StringError>("Error in lookup result elements",
                                   inconvertibleErrorCode());

  for (unsigned I = 0; I != Pairs.size(); ++I)
    Pairs[I].second->setValue(Result->front()[I]);

  return Error::success();
}

} // End namespace orc.
} // End namespace llvm.