1//===- InstanceSymbolGenerator.cpp - Executor instance symbols ------------===//
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/InstanceSymbolGenerator.h"
10
11#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
12#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
13
14#define DEBUG_TYPE "orc"
15
16namespace llvm::orc {
17
18Error InstanceSymbolGenerator::tryToGenerate(LookupState &LS, LookupKind K,
19 JITDylib &JD,
20 JITDylibLookupFlags JDLookupFlags,
21 const SymbolLookupSet &Symbols) {
22
23 if (Symbols.empty())
24 return Error::success();
25
26 LLVM_DEBUG({
27 dbgs() << "InstanceSymbolGenerator trying to generate " << Symbols << "\n";
28 });
29
30 // Look the symbols up as required, so that names that aren't instance
31 // symbols come back as std::nullopt rather than as null addresses. This
32 // doesn't fail the lookup: those names are just left undefined.
33 SymbolLookupSet LookupSymbols;
34 for (auto &[Name, Flags] : Symbols)
35 LookupSymbols.add(Name, Flags: SymbolLookupFlags::RequiredSymbol);
36
37 Lookup(
38 [&JD, LS = std::move(LS),
39 LookupSymbols](Expected<SymbolLookupResult> Result) mutable {
40 if (!Result)
41 return LS.continueLookup(Err: Result.takeError());
42
43 if (Result->size() != LookupSymbols.size())
44 return LS.continueLookup(Err: make_error<StringError>(
45 Args: "Instance symbol lookup returned " + Twine(Result->size()) +
46 " results for " + Twine(LookupSymbols.size()) + " symbols",
47 Args: inconvertibleErrorCode()));
48
49 SymbolMap NewSymbols;
50 auto ResultIt = Result->begin();
51 for (auto &[Name, Flags] : LookupSymbols) {
52 const auto &Addr = *ResultIt++;
53 if (Addr)
54 NewSymbols[Name] = {*Addr, JITSymbolFlags::Exported};
55 }
56
57 LLVM_DEBUG({
58 dbgs() << "InstanceSymbolGenerator lookup returned " << NewSymbols
59 << "\n";
60 });
61
62 if (NewSymbols.empty())
63 return LS.continueLookup(Err: Error::success());
64
65 LS.continueLookup(Err: JD.define(MU: absoluteSymbols(Symbols: std::move(NewSymbols))));
66 },
67 JD.getExecutionSession(), LookupSymbols);
68
69 return Error::success();
70}
71
72} // namespace llvm::orc
73