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