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/ADT/Triple.h"
18 #include "llvm/ExecutionEngine/Orc/Core.h"
19 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
20 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
21 #include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
22 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/Regex.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 #include <vector>
28 
29 namespace llvm {
30 
31 struct Session;
32 
33 struct Session {
34 
35   orc::ExecutionSession ES;
36   orc::JITDylib *MainJD = nullptr;
37   orc::ObjectLinkingLayer ObjLayer;
38   orc::JITDylibSearchOrder JDSearchOrder;
39 
40   ~Session();
41 
42   static Expected<std::unique_ptr<Session>> Create(Triple TT);
43   void dumpSessionInfo(raw_ostream &OS);
44   void modifyPassConfig(const Triple &FTT,
45                         jitlink::PassConfiguration &PassConfig);
46 
47   using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
48 
49   struct FileInfo {
50     StringMap<MemoryRegionInfo> SectionInfos;
51     StringMap<MemoryRegionInfo> StubInfos;
52     StringMap<MemoryRegionInfo> GOTEntryInfos;
53   };
54 
55   using DynLibJDMap = std::map<std::string, orc::JITDylib *>;
56   using SymbolInfoMap = StringMap<MemoryRegionInfo>;
57   using FileInfoMap = StringMap<FileInfo>;
58 
59   Expected<orc::JITDylib *> getOrLoadDynamicLibrary(StringRef LibPath);
60   Error loadAndLinkDynamicLibrary(orc::JITDylib &JD, StringRef LibPath);
61 
62   Expected<FileInfo &> findFileInfo(StringRef FileName);
63   Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
64                                                StringRef SectionName);
65   Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
66                                             StringRef TargetName);
67   Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
68                                                 StringRef TargetName);
69 
70   bool isSymbolRegistered(StringRef Name);
71   Expected<MemoryRegionInfo &> findSymbolInfo(StringRef SymbolName,
72                                               Twine ErrorMsgStem);
73 
74   DynLibJDMap DynLibJDs;
75 
76   SymbolInfoMap SymbolInfos;
77   FileInfoMap FileInfos;
78   uint64_t SizeBeforePruning = 0;
79   uint64_t SizeAfterFixups = 0;
80 
81   StringSet<> HarnessFiles;
82   StringSet<> HarnessExternals;
83   StringSet<> HarnessDefinitions;
84   DenseMap<StringRef, StringRef> CanonicalWeakDefs;
85 
86 private:
87   Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
88 };
89 
90 /// Record symbols, GOT entries, stubs, and sections for ELF file.
91 Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
92 
93 /// Record symbols, GOT entries, stubs, and sections for MachO file.
94 Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
95 
96 /// Record symbols, GOT entries, stubs, and sections for COFF file.
97 Error registerCOFFGraphInfo(Session &S, jitlink::LinkGraph &G);
98 
99 } // end namespace llvm
100 
101 #endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
102