1 //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
12 #include "llvm/BinaryFormat/Magic.h"
13 #include "llvm/ExecutionEngine/JITLink/MachO.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::object;
22 #define DEBUG_TYPE "jitlink"
26 enum JITLinkErrorCode { GenericJITLinkError = 1 };
28 // FIXME: This class is only here to support the transition to llvm::Error. It
29 // will be removed once this transition is complete. Clients should prefer to
30 // deal with the Error value directly, rather than converting to error_code.
31 class JITLinkerErrorCategory : public std::error_category {
33 const char *name() const noexcept override { return "runtimedyld"; }
35 std::string message(int Condition) const override {
36 switch (static_cast<JITLinkErrorCode>(Condition)) {
37 case GenericJITLinkError:
38 return "Generic JITLink error";
40 llvm_unreachable("Unrecognized JITLinkErrorCode");
44 static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
51 char JITLinkError::ID = 0;
53 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
55 std::error_code JITLinkError::convertToErrorCode() const {
56 return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
59 const StringRef getGenericEdgeKindName(Edge::Kind K) {
62 return "INVALID RELOCATION";
65 case Edge::LayoutNext:
68 llvm_unreachable("Unrecognized relocation kind");
72 raw_ostream &operator<<(raw_ostream &OS, const Atom &A) {
74 if (A.getName().empty())
75 OS << "anon@" << format("0x%016" PRIx64, A.getAddress());
80 auto &DA = static_cast<const DefinedAtom &>(A);
81 OS << " section=" << DA.getSection().getName();
84 if (DA.shouldDiscard())
85 OS << " should-discard";
92 void printEdge(raw_ostream &OS, const Atom &FixupAtom, const Edge &E,
93 StringRef EdgeKindName) {
94 OS << "edge@" << formatv("{0:x16}", FixupAtom.getAddress() + E.getOffset())
95 << ": " << FixupAtom << " + " << E.getOffset() << " -- " << EdgeKindName
96 << " -> " << E.getTarget() << " + " << E.getAddend();
100 for (auto *DA : DefinedAtoms)
104 void AtomGraph::dump(raw_ostream &OS,
105 std::function<StringRef(Edge::Kind)> EdgeKindToName) {
107 EdgeKindToName = [](Edge::Kind K) { return StringRef(); };
109 OS << "Defined atoms:\n";
110 for (auto *DA : defined_atoms()) {
111 OS << " " << format("0x%016" PRIx64, DA->getAddress()) << ": " << *DA
113 for (auto &E : DA->edges()) {
115 StringRef EdgeName = (E.getKind() < Edge::FirstRelocation
116 ? getGenericEdgeKindName(E.getKind())
117 : EdgeKindToName(E.getKind()));
119 if (!EdgeName.empty())
120 printEdge(OS, *DA, E, EdgeName);
122 auto EdgeNumberString = std::to_string(E.getKind());
123 printEdge(OS, *DA, E, EdgeNumberString);
129 OS << "Absolute atoms:\n";
130 for (auto *A : absolute_atoms())
131 OS << " " << format("0x%016" PRIx64, A->getAddress()) << ": " << *A
134 OS << "External atoms:\n";
135 for (auto *A : external_atoms())
136 OS << " " << format("0x%016" PRIx64, A->getAddress()) << ": " << *A
140 JITLinkContext::~JITLinkContext() {}
142 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
146 AtomGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
147 return AtomGraphPassFunction();
150 Error JITLinkContext::modifyPassConfig(const Triple &TT,
151 PassConfiguration &Config) {
152 return Error::success();
155 Error markAllAtomsLive(AtomGraph &G) {
156 for (auto *DA : G.defined_atoms())
158 return Error::success();
161 void jitLink(std::unique_ptr<JITLinkContext> Ctx) {
162 auto Magic = identify_magic(Ctx->getObjectBuffer().getBuffer());
164 case file_magic::macho_object:
165 return jitLink_MachO(std::move(Ctx));
167 Ctx->notifyFailed(make_error<JITLinkError>("Unsupported file format"));
171 } // end namespace jitlink
172 } // end namespace llvm