1//===---- llvm-jitlink.h - Session and format-specific decls ----*- C++ -*-===//
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// llvm-jitlink Session class and tool utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
14#define LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
15
16#include "llvm/ADT/StringSet.h"
17#include "llvm/ExecutionEngine/Orc/Core.h"
18#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
19#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
20#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
21#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/Regex.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/TargetParser/SubtargetFeature.h"
26#include "llvm/TargetParser/Triple.h"
27
28namespace llvm {
29
30struct Session {
31
32 orc::ExecutionSession ES;
33 orc::JITDylib *MainJD = nullptr;
34 orc::JITDylib *ProcessSymsJD = nullptr;
35 orc::JITDylib *PlatformJD = nullptr;
36 orc::ObjectLinkingLayer ObjLayer;
37 orc::JITDylibSearchOrder JDSearchOrder;
38 SubtargetFeatures Features;
39
40 ~Session();
41
42 static Expected<std::unique_ptr<Session>> Create(Triple TT,
43 SubtargetFeatures Features);
44 void dumpSessionInfo(raw_ostream &OS);
45 void modifyPassConfig(const Triple &FTT,
46 jitlink::PassConfiguration &PassConfig);
47
48 using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
49
50 struct FileInfo {
51 StringMap<MemoryRegionInfo> SectionInfos;
52 StringMap<SmallVector<MemoryRegionInfo, 1>> StubInfos;
53 StringMap<MemoryRegionInfo> GOTEntryInfos;
54
55 using Symbol = jitlink::Symbol;
56 using LinkGraph = jitlink::LinkGraph;
57 using GetSymbolTargetFunction =
58 unique_function<Expected<Symbol &>(LinkGraph &G, jitlink::Block &)>;
59
60 Error registerGOTEntry(LinkGraph &G, Symbol &Sym,
61 GetSymbolTargetFunction GetSymbolTarget);
62 Error registerStubEntry(LinkGraph &G, Symbol &Sym,
63 GetSymbolTargetFunction GetSymbolTarget);
64 Error registerMultiStubEntry(LinkGraph &G, Symbol &Sym,
65 GetSymbolTargetFunction GetSymbolTarget);
66 };
67
68 using DynLibJDMap = std::map<std::string, orc::JITDylib *>;
69 using SymbolInfoMap = StringMap<MemoryRegionInfo>;
70 using FileInfoMap = StringMap<FileInfo>;
71
72 Expected<orc::JITDylib *> getOrLoadDynamicLibrary(StringRef LibPath);
73 Error loadAndLinkDynamicLibrary(orc::JITDylib &JD, StringRef LibPath);
74
75 Expected<FileInfo &> findFileInfo(StringRef FileName);
76 Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
77 StringRef SectionName);
78 Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
79 StringRef TargetName,
80 StringRef KindNameFilter);
81 Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
82 StringRef TargetName);
83
84 bool isSymbolRegistered(StringRef Name);
85 Expected<MemoryRegionInfo &> findSymbolInfo(StringRef SymbolName,
86 Twine ErrorMsgStem);
87
88 DynLibJDMap DynLibJDs;
89
90 SymbolInfoMap SymbolInfos;
91 FileInfoMap FileInfos;
92
93 StringSet<> HarnessFiles;
94 StringSet<> HarnessExternals;
95 StringSet<> HarnessDefinitions;
96 DenseMap<StringRef, StringRef> CanonicalWeakDefs;
97
98 std::optional<Regex> ShowGraphsRegex;
99
100private:
101 Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
102};
103
104/// Record symbols, GOT entries, stubs, and sections for ELF file.
105Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
106
107/// Record symbols, GOT entries, stubs, and sections for MachO file.
108Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
109
110/// Record symbols, GOT entries, stubs, and sections for COFF file.
111Error registerCOFFGraphInfo(Session &S, jitlink::LinkGraph &G);
112
113/// Adds a statistics gathering plugin if any stats options are used.
114void enableStatistics(Session &S, bool UsingOrcRuntime);
115
116} // end namespace llvm
117
118#endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
119