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 char *getGenericEdgeKindName(Edge::Kind K
) {
62 return "INVALID RELOCATION";
66 llvm_unreachable("Unrecognized relocation kind");
70 const char *getLinkageName(Linkage L
) {
77 llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
80 const char *getScopeName(Scope S
) {
89 llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
92 raw_ostream
&operator<<(raw_ostream
&OS
, const Block
&B
) {
93 return OS
<< formatv("{0:x16}", B
.getAddress()) << " -- "
94 << formatv("{0:x16}", B
.getAddress() + B
.getSize()) << ": "
95 << (B
.isZeroFill() ? "zero-fill" : "content")
96 << ", align = " << B
.getAlignment()
97 << ", align-ofs = " << B
.getAlignmentOffset()
98 << ", section = " << B
.getSection().getName();
101 raw_ostream
&operator<<(raw_ostream
&OS
, const Symbol
&Sym
) {
103 if (Sym
.getName().empty())
108 switch (Sym
.getLinkage()) {
109 case Linkage::Strong
:
116 switch (Sym
.getScope()) {
127 OS
<< (Sym
.isLive() ? '+' : '-')
128 << ", size = " << formatv("{0:x8}", Sym
.getSize())
129 << ", addr = " << formatv("{0:x16}", Sym
.getAddress()) << " ("
130 << formatv("{0:x16}", Sym
.getAddressable().getAddress()) << " + "
131 << formatv("{0:x8}", Sym
.getOffset());
133 OS
<< " " << Sym
.getBlock().getSection().getName();
138 void printEdge(raw_ostream
&OS
, const Block
&B
, const Edge
&E
,
139 StringRef EdgeKindName
) {
140 OS
<< "edge@" << formatv("{0:x16}", B
.getAddress() + E
.getOffset()) << ": "
141 << formatv("{0:x16}", B
.getAddress()) << " + " << E
.getOffset() << " -- "
142 << EdgeKindName
<< " -> " << E
.getTarget() << " + " << E
.getAddend();
145 Section::~Section() {
146 for (auto *Sym
: Symbols
)
150 LinkGraph::~LinkGraph() {
152 for (auto *B
: Blocks
)
156 void LinkGraph::dump(raw_ostream
&OS
,
157 std::function
<StringRef(Edge::Kind
)> EdgeKindToName
) {
159 EdgeKindToName
= [](Edge::Kind K
) { return StringRef(); };
162 for (auto *Sym
: defined_symbols()) {
163 OS
<< " " << format("0x%016" PRIx64
, Sym
->getAddress()) << ": " << *Sym
165 if (Sym
->isDefined()) {
166 for (auto &E
: Sym
->getBlock().edges()) {
168 StringRef EdgeName
= (E
.getKind() < Edge::FirstRelocation
169 ? getGenericEdgeKindName(E
.getKind())
170 : EdgeKindToName(E
.getKind()));
172 if (!EdgeName
.empty())
173 printEdge(OS
, Sym
->getBlock(), E
, EdgeName
);
175 auto EdgeNumberString
= std::to_string(E
.getKind());
176 printEdge(OS
, Sym
->getBlock(), E
, EdgeNumberString
);
183 OS
<< "Absolute symbols:\n";
184 for (auto *Sym
: absolute_symbols())
185 OS
<< " " << format("0x%016" PRIx64
, Sym
->getAddress()) << ": " << *Sym
188 OS
<< "External symbols:\n";
189 for (auto *Sym
: external_symbols())
190 OS
<< " " << format("0x%016" PRIx64
, Sym
->getAddress()) << ": " << *Sym
194 void JITLinkAsyncLookupContinuation::anchor() {}
196 JITLinkContext::~JITLinkContext() {}
198 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple
&TT
) const {
202 LinkGraphPassFunction
JITLinkContext::getMarkLivePass(const Triple
&TT
) const {
203 return LinkGraphPassFunction();
206 Error
JITLinkContext::modifyPassConfig(const Triple
&TT
,
207 PassConfiguration
&Config
) {
208 return Error::success();
211 Error
markAllSymbolsLive(LinkGraph
&G
) {
212 for (auto *Sym
: G
.defined_symbols())
214 return Error::success();
217 void jitLink(std::unique_ptr
<JITLinkContext
> Ctx
) {
218 auto Magic
= identify_magic(Ctx
->getObjectBuffer().getBuffer());
220 case file_magic::macho_object
:
221 return jitLink_MachO(std::move(Ctx
));
223 Ctx
->notifyFailed(make_error
<JITLinkError
>("Unsupported file format"));
227 } // end namespace jitlink
228 } // end namespace llvm