JITLink.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ExecutionEngine/JITLink/JITLink.h"
  10. #include "llvm/BinaryFormat/Magic.h"
  11. #include "llvm/ExecutionEngine/JITLink/ELF.h"
  12. #include "llvm/ExecutionEngine/JITLink/MachO.h"
  13. #include "llvm/Support/Format.h"
  14. #include "llvm/Support/ManagedStatic.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. using namespace llvm::object;
  19. #define DEBUG_TYPE "jitlink"
  20. namespace {
  21. enum JITLinkErrorCode { GenericJITLinkError = 1 };
  22. // FIXME: This class is only here to support the transition to llvm::Error. It
  23. // will be removed once this transition is complete. Clients should prefer to
  24. // deal with the Error value directly, rather than converting to error_code.
  25. class JITLinkerErrorCategory : public std::error_category {
  26. public:
  27. const char *name() const noexcept override { return "runtimedyld"; }
  28. std::string message(int Condition) const override {
  29. switch (static_cast<JITLinkErrorCode>(Condition)) {
  30. case GenericJITLinkError:
  31. return "Generic JITLink error";
  32. }
  33. llvm_unreachable("Unrecognized JITLinkErrorCode");
  34. }
  35. };
  36. static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
  37. } // namespace
  38. namespace llvm {
  39. namespace jitlink {
  40. char JITLinkError::ID = 0;
  41. void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
  42. std::error_code JITLinkError::convertToErrorCode() const {
  43. return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
  44. }
  45. const char *getGenericEdgeKindName(Edge::Kind K) {
  46. switch (K) {
  47. case Edge::Invalid:
  48. return "INVALID RELOCATION";
  49. case Edge::KeepAlive:
  50. return "Keep-Alive";
  51. default:
  52. return "<Unrecognized edge kind>";
  53. }
  54. }
  55. const char *getLinkageName(Linkage L) {
  56. switch (L) {
  57. case Linkage::Strong:
  58. return "strong";
  59. case Linkage::Weak:
  60. return "weak";
  61. }
  62. llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
  63. }
  64. const char *getScopeName(Scope S) {
  65. switch (S) {
  66. case Scope::Default:
  67. return "default";
  68. case Scope::Hidden:
  69. return "hidden";
  70. case Scope::Local:
  71. return "local";
  72. }
  73. llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
  74. }
  75. raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
  76. return OS << formatv("{0:x16}", B.getAddress()) << " -- "
  77. << formatv("{0:x16}", B.getAddress() + B.getSize()) << ": "
  78. << "size = " << formatv("{0:x}", B.getSize()) << ", "
  79. << (B.isZeroFill() ? "zero-fill" : "content")
  80. << ", align = " << B.getAlignment()
  81. << ", align-ofs = " << B.getAlignmentOffset()
  82. << ", section = " << B.getSection().getName();
  83. }
  84. raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
  85. OS << "<";
  86. if (Sym.getName().empty())
  87. OS << "*anon*";
  88. else
  89. OS << Sym.getName();
  90. OS << ": flags = ";
  91. switch (Sym.getLinkage()) {
  92. case Linkage::Strong:
  93. OS << 'S';
  94. break;
  95. case Linkage::Weak:
  96. OS << 'W';
  97. break;
  98. }
  99. switch (Sym.getScope()) {
  100. case Scope::Default:
  101. OS << 'D';
  102. break;
  103. case Scope::Hidden:
  104. OS << 'H';
  105. break;
  106. case Scope::Local:
  107. OS << 'L';
  108. break;
  109. }
  110. OS << (Sym.isLive() ? '+' : '-')
  111. << ", size = " << formatv("{0:x}", Sym.getSize())
  112. << ", addr = " << formatv("{0:x16}", Sym.getAddress()) << " ("
  113. << formatv("{0:x16}", Sym.getAddressable().getAddress()) << " + "
  114. << formatv("{0:x}", Sym.getOffset());
  115. if (Sym.isDefined())
  116. OS << " " << Sym.getBlock().getSection().getName();
  117. OS << ")>";
  118. return OS;
  119. }
  120. void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
  121. StringRef EdgeKindName) {
  122. OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": "
  123. << formatv("{0:x16}", B.getAddress()) << " + "
  124. << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName << " -> ";
  125. auto &TargetSym = E.getTarget();
  126. if (TargetSym.hasName())
  127. OS << TargetSym.getName();
  128. else {
  129. auto &TargetBlock = TargetSym.getBlock();
  130. auto &TargetSec = TargetBlock.getSection();
  131. JITTargetAddress SecAddress = ~JITTargetAddress(0);
  132. for (auto *B : TargetSec.blocks())
  133. if (B->getAddress() < SecAddress)
  134. SecAddress = B->getAddress();
  135. JITTargetAddress SecDelta = TargetSym.getAddress() - SecAddress;
  136. OS << formatv("{0:x16}", TargetSym.getAddress()) << " (section "
  137. << TargetSec.getName();
  138. if (SecDelta)
  139. OS << " + " << formatv("{0:x}", SecDelta);
  140. OS << " / block " << formatv("{0:x16}", TargetBlock.getAddress());
  141. if (TargetSym.getOffset())
  142. OS << " + " << formatv("{0:x}", TargetSym.getOffset());
  143. OS << ")";
  144. }
  145. if (E.getAddend() != 0)
  146. OS << " + " << E.getAddend();
  147. }
  148. Section::~Section() {
  149. for (auto *Sym : Symbols)
  150. Sym->~Symbol();
  151. for (auto *B : Blocks)
  152. B->~Block();
  153. }
  154. Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
  155. SplitBlockCache *Cache) {
  156. assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
  157. // If the split point covers all of B then just return B.
  158. if (SplitIndex == B.getSize())
  159. return B;
  160. assert(SplitIndex < B.getSize() && "SplitIndex out of range");
  161. // Create the new block covering [ 0, SplitIndex ).
  162. auto &NewBlock =
  163. B.isZeroFill()
  164. ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
  165. B.getAlignment(), B.getAlignmentOffset())
  166. : createContentBlock(
  167. B.getSection(), B.getContent().substr(0, SplitIndex),
  168. B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
  169. // Modify B to cover [ SplitIndex, B.size() ).
  170. B.setAddress(B.getAddress() + SplitIndex);
  171. B.setContent(B.getContent().substr(SplitIndex));
  172. B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
  173. B.getAlignment());
  174. // Handle edge transfer/update.
  175. {
  176. // Copy edges to NewBlock (recording their iterators so that we can remove
  177. // them from B), and update of Edges remaining on B.
  178. std::vector<Block::edge_iterator> EdgesToRemove;
  179. for (auto I = B.edges().begin(); I != B.edges().end();) {
  180. if (I->getOffset() < SplitIndex) {
  181. NewBlock.addEdge(*I);
  182. I = B.removeEdge(I);
  183. } else {
  184. I->setOffset(I->getOffset() - SplitIndex);
  185. ++I;
  186. }
  187. }
  188. }
  189. // Handle symbol transfer/update.
  190. {
  191. // Initialize the symbols cache if necessary.
  192. SplitBlockCache LocalBlockSymbolsCache;
  193. if (!Cache)
  194. Cache = &LocalBlockSymbolsCache;
  195. if (*Cache == None) {
  196. *Cache = SplitBlockCache::value_type();
  197. for (auto *Sym : B.getSection().symbols())
  198. if (&Sym->getBlock() == &B)
  199. (*Cache)->push_back(Sym);
  200. llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
  201. return LHS->getOffset() > RHS->getOffset();
  202. });
  203. }
  204. auto &BlockSymbols = **Cache;
  205. // Transfer all symbols with offset less than SplitIndex to NewBlock.
  206. while (!BlockSymbols.empty() &&
  207. BlockSymbols.back()->getOffset() < SplitIndex) {
  208. BlockSymbols.back()->setBlock(NewBlock);
  209. BlockSymbols.pop_back();
  210. }
  211. // Update offsets for all remaining symbols in B.
  212. for (auto *Sym : BlockSymbols)
  213. Sym->setOffset(Sym->getOffset() - SplitIndex);
  214. }
  215. return NewBlock;
  216. }
  217. void LinkGraph::dump(raw_ostream &OS,
  218. std::function<StringRef(Edge::Kind)> EdgeKindToName) {
  219. if (!EdgeKindToName)
  220. EdgeKindToName = [](Edge::Kind K) { return StringRef(); };
  221. OS << "Symbols:\n";
  222. for (auto *Sym : defined_symbols()) {
  223. OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
  224. << "\n";
  225. if (Sym->isDefined()) {
  226. for (auto &E : Sym->getBlock().edges()) {
  227. OS << " ";
  228. StringRef EdgeName = (E.getKind() < Edge::FirstRelocation
  229. ? getGenericEdgeKindName(E.getKind())
  230. : EdgeKindToName(E.getKind()));
  231. if (!EdgeName.empty())
  232. printEdge(OS, Sym->getBlock(), E, EdgeName);
  233. else {
  234. auto EdgeNumberString = std::to_string(E.getKind());
  235. printEdge(OS, Sym->getBlock(), E, EdgeNumberString);
  236. }
  237. OS << "\n";
  238. }
  239. }
  240. }
  241. OS << "Absolute symbols:\n";
  242. for (auto *Sym : absolute_symbols())
  243. OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
  244. << "\n";
  245. OS << "External symbols:\n";
  246. for (auto *Sym : external_symbols())
  247. OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
  248. << "\n";
  249. }
  250. raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
  251. switch (LF) {
  252. case SymbolLookupFlags::RequiredSymbol:
  253. return OS << "RequiredSymbol";
  254. case SymbolLookupFlags::WeaklyReferencedSymbol:
  255. return OS << "WeaklyReferencedSymbol";
  256. }
  257. llvm_unreachable("Unrecognized lookup flags");
  258. }
  259. void JITLinkAsyncLookupContinuation::anchor() {}
  260. JITLinkContext::~JITLinkContext() {}
  261. bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
  262. return true;
  263. }
  264. LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
  265. return LinkGraphPassFunction();
  266. }
  267. Error JITLinkContext::modifyPassConfig(const Triple &TT,
  268. PassConfiguration &Config) {
  269. return Error::success();
  270. }
  271. Error markAllSymbolsLive(LinkGraph &G) {
  272. for (auto *Sym : G.defined_symbols())
  273. Sym->setLive(true);
  274. return Error::success();
  275. }
  276. Expected<std::unique_ptr<LinkGraph>>
  277. createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
  278. auto Magic = identify_magic(ObjectBuffer.getBuffer());
  279. switch (Magic) {
  280. case file_magic::macho_object:
  281. return createLinkGraphFromMachOObject(std::move(ObjectBuffer));
  282. case file_magic::elf_relocatable:
  283. return createLinkGraphFromELFObject(std::move(ObjectBuffer));
  284. default:
  285. return make_error<JITLinkError>("Unsupported file format");
  286. };
  287. }
  288. void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
  289. switch (G->getTargetTriple().getObjectFormat()) {
  290. case Triple::MachO:
  291. return link_MachO(std::move(G), std::move(Ctx));
  292. case Triple::ELF:
  293. return link_ELF(std::move(G), std::move(Ctx));
  294. default:
  295. Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
  296. };
  297. }
  298. } // end namespace jitlink
  299. } // end namespace llvm