1//===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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
10#include "MCJIT.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ExecutionEngine/GenericValue.h"
13#include "llvm/ExecutionEngine/JITEventListener.h"
14#include "llvm/ExecutionEngine/MCJIT.h"
15#include "llvm/ExecutionEngine/SectionMemoryManager.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/LegacyPassManager.h"
20#include "llvm/IR/Mangler.h"
21#include "llvm/IR/Module.h"
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/Object/Archive.h"
24#include "llvm/Object/ObjectFile.h"
25#include "llvm/Support/DynamicLibrary.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/MutexGuard.h"
29
30using namespace llvm;
31
32void ObjectCache::anchor() {}
33
34namespace {
35
36static struct RegisterJIT {
37  RegisterJIT() { MCJIT::Register(); }
38} JITRegistrator;
39
40}
41
42extern "C" void LLVMLinkInMCJIT() {
43}
44
45ExecutionEngine*
46MCJIT::createJIT(std::unique_ptr<Module> M,
47                 std::string *ErrorStr,
48                 std::shared_ptr<MCJITMemoryManager> MemMgr,
49                 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
50                 std::unique_ptr<TargetMachine> TM) {
51  // Try to register the program as a source of symbols to resolve against.
52  //
53  // FIXME: Don't do this here.
54  sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
55
56  if (!MemMgr || !Resolver) {
57    auto RTDyldMM = std::make_shared<SectionMemoryManager>();
58    if (!MemMgr)
59      MemMgr = RTDyldMM;
60    if (!Resolver)
61      Resolver = RTDyldMM;
62  }
63
64  return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
65                   std::move(Resolver));
66}
67
68MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
69             std::shared_ptr<MCJITMemoryManager> MemMgr,
70             std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
71    : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
72      MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
73      Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
74  // FIXME: We are managing our modules, so we do not want the base class
75  // ExecutionEngine to manage them as well. To avoid double destruction
76  // of the first (and only) module added in ExecutionEngine constructor
77  // we remove it from EE and will destruct it ourselves.
78  //
79  // It may make sense to move our module manager (based on SmallStPtr) back
80  // into EE if the JIT and Interpreter can live with it.
81  // If so, additional functions: addModule, removeModule, FindFunctionNamed,
82  // runStaticConstructorsDestructors could be moved back to EE as well.
83  //
84  std::unique_ptr<Module> First = std::move(Modules[0]);
85  Modules.clear();
86
87  OwnedModules.addModule(std::move(First));
88  setDataLayout(TM->getDataLayout());
89  RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
90}
91
92MCJIT::~MCJIT() {
93  MutexGuard locked(lock);
94
95  Dyld.deregisterEHFrames();
96
97  for (auto &Obj : LoadedObjects)
98    if (Obj)
99      NotifyFreeingObject(*Obj);
100
101  Archives.clear();
102}
103
104void MCJIT::addModule(std::unique_ptr<Module> M) {
105  MutexGuard locked(lock);
106  OwnedModules.addModule(std::move(M));
107}
108
109bool MCJIT::removeModule(Module *M) {
110  MutexGuard locked(lock);
111  return OwnedModules.removeModule(M);
112}
113
114void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
115  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
116  if (Dyld.hasError())
117    report_fatal_error(Dyld.getErrorString());
118
119  NotifyObjectEmitted(*Obj, *L);
120
121  LoadedObjects.push_back(std::move(Obj));
122}
123
124void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
125  std::unique_ptr<object::ObjectFile> ObjFile;
126  std::unique_ptr<MemoryBuffer> MemBuf;
127  std::tie(ObjFile, MemBuf) = Obj.takeBinary();
128  addObjectFile(std::move(ObjFile));
129  Buffers.push_back(std::move(MemBuf));
130}
131
132void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
133  Archives.push_back(std::move(A));
134}
135
136void MCJIT::setObjectCache(ObjectCache* NewCache) {
137  MutexGuard locked(lock);
138  ObjCache = NewCache;
139}
140
141std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
142  MutexGuard locked(lock);
143
144  // This must be a module which has already been added but not loaded to this
145  // MCJIT instance, since these conditions are tested by our caller,
146  // generateCodeForModule.
147
148  legacy::PassManager PM;
149
150  M->setDataLayout(*TM->getDataLayout());
151
152  // The RuntimeDyld will take ownership of this shortly
153  SmallVector<char, 4096> ObjBufferSV;
154  raw_svector_ostream ObjStream(ObjBufferSV);
155
156  // Turn the machine code intermediate representation into bytes in memory
157  // that may be executed.
158  if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
159    report_fatal_error("Target does not support MC emission!");
160
161  // Initialize passes.
162  PM.run(*M);
163  // Flush the output buffer to get the generated code into memory
164  ObjStream.flush();
165
166  std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
167                                new ObjectMemoryBuffer(std::move(ObjBufferSV)));
168
169  // If we have an object cache, tell it about the new object.
170  // Note that we're using the compiled image, not the loaded image (as below).
171  if (ObjCache) {
172    // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
173    // to create a temporary object here and delete it after the call.
174    MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
175    ObjCache->notifyObjectCompiled(M, MB);
176  }
177
178  return CompiledObjBuffer;
179}
180
181void MCJIT::generateCodeForModule(Module *M) {
182  // Get a thread lock to make sure we aren't trying to load multiple times
183  MutexGuard locked(lock);
184
185  // This must be a module which has already been added to this MCJIT instance.
186  assert(OwnedModules.ownsModule(M) &&
187         "MCJIT::generateCodeForModule: Unknown module.");
188
189  // Re-compilation is not supported
190  if (OwnedModules.hasModuleBeenLoaded(M))
191    return;
192
193  std::unique_ptr<MemoryBuffer> ObjectToLoad;
194  // Try to load the pre-compiled object from cache if possible
195  if (ObjCache)
196    ObjectToLoad = ObjCache->getObject(M);
197
198  // If the cache did not contain a suitable object, compile the object
199  if (!ObjectToLoad) {
200    ObjectToLoad = emitObject(M);
201    assert(ObjectToLoad && "Compilation did not produce an object.");
202  }
203
204  // Load the object into the dynamic linker.
205  // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
206  ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =
207    object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
208  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
209    Dyld.loadObject(*LoadedObject.get());
210
211  if (Dyld.hasError())
212    report_fatal_error(Dyld.getErrorString());
213
214  NotifyObjectEmitted(*LoadedObject.get(), *L);
215
216  Buffers.push_back(std::move(ObjectToLoad));
217  LoadedObjects.push_back(std::move(*LoadedObject));
218
219  OwnedModules.markModuleAsLoaded(M);
220}
221
222void MCJIT::finalizeLoadedModules() {
223  MutexGuard locked(lock);
224
225  // Resolve any outstanding relocations.
226  Dyld.resolveRelocations();
227
228  OwnedModules.markAllLoadedModulesAsFinalized();
229
230  // Register EH frame data for any module we own which has been loaded
231  Dyld.registerEHFrames();
232
233  // Set page permissions.
234  MemMgr->finalizeMemory();
235}
236
237// FIXME: Rename this.
238void MCJIT::finalizeObject() {
239  MutexGuard locked(lock);
240
241  // Generate code for module is going to move objects out of the 'added' list,
242  // so we need to copy that out before using it:
243  SmallVector<Module*, 16> ModsToAdd;
244  for (auto M : OwnedModules.added())
245    ModsToAdd.push_back(M);
246
247  for (auto M : ModsToAdd)
248    generateCodeForModule(M);
249
250  finalizeLoadedModules();
251}
252
253void MCJIT::finalizeModule(Module *M) {
254  MutexGuard locked(lock);
255
256  // This must be a module which has already been added to this MCJIT instance.
257  assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
258
259  // If the module hasn't been compiled, just do that.
260  if (!OwnedModules.hasModuleBeenLoaded(M))
261    generateCodeForModule(M);
262
263  finalizeLoadedModules();
264}
265
266RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
267  Mangler Mang(TM->getDataLayout());
268  SmallString<128> FullName;
269  Mang.getNameWithPrefix(FullName, Name);
270  return Dyld.getSymbol(FullName);
271}
272
273Module *MCJIT::findModuleForSymbol(const std::string &Name,
274                                   bool CheckFunctionsOnly) {
275  MutexGuard locked(lock);
276
277  // If it hasn't already been generated, see if it's in one of our modules.
278  for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
279                              E = OwnedModules.end_added();
280       I != E; ++I) {
281    Module *M = *I;
282    Function *F = M->getFunction(Name);
283    if (F && !F->isDeclaration())
284      return M;
285    if (!CheckFunctionsOnly) {
286      GlobalVariable *G = M->getGlobalVariable(Name);
287      if (G && !G->isDeclaration())
288        return M;
289      // FIXME: Do we need to worry about global aliases?
290    }
291  }
292  // We didn't find the symbol in any of our modules.
293  return nullptr;
294}
295
296uint64_t MCJIT::getSymbolAddress(const std::string &Name,
297                                 bool CheckFunctionsOnly) {
298  return findSymbol(Name, CheckFunctionsOnly).getAddress();
299}
300
301RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name,
302                                          bool CheckFunctionsOnly) {
303  MutexGuard locked(lock);
304
305  // First, check to see if we already have this symbol.
306  if (auto Sym = findExistingSymbol(Name))
307    return Sym;
308
309  for (object::OwningBinary<object::Archive> &OB : Archives) {
310    object::Archive *A = OB.getBinary();
311    // Look for our symbols in each Archive
312    object::Archive::child_iterator ChildIt = A->findSym(Name);
313    if (ChildIt != A->child_end()) {
314      // FIXME: Support nested archives?
315      ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
316          ChildIt->getAsBinary();
317      if (ChildBinOrErr.getError())
318        continue;
319      std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
320      if (ChildBin->isObject()) {
321        std::unique_ptr<object::ObjectFile> OF(
322            static_cast<object::ObjectFile *>(ChildBin.release()));
323        // This causes the object file to be loaded.
324        addObjectFile(std::move(OF));
325        // The address should be here now.
326        if (auto Sym = findExistingSymbol(Name))
327          return Sym;
328      }
329    }
330  }
331
332  // If it hasn't already been generated, see if it's in one of our modules.
333  Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
334  if (M) {
335    generateCodeForModule(M);
336
337    // Check the RuntimeDyld table again, it should be there now.
338    return findExistingSymbol(Name);
339  }
340
341  // If a LazyFunctionCreator is installed, use it to get/create the function.
342  // FIXME: Should we instead have a LazySymbolCreator callback?
343  if (LazyFunctionCreator) {
344    auto Addr = static_cast<uint64_t>(
345                  reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
346    return RuntimeDyld::SymbolInfo(Addr, JITSymbolFlags::Exported);
347  }
348
349  return nullptr;
350}
351
352uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
353  MutexGuard locked(lock);
354  uint64_t Result = getSymbolAddress(Name, false);
355  if (Result != 0)
356    finalizeLoadedModules();
357  return Result;
358}
359
360uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
361  MutexGuard locked(lock);
362  uint64_t Result = getSymbolAddress(Name, true);
363  if (Result != 0)
364    finalizeLoadedModules();
365  return Result;
366}
367
368// Deprecated.  Use getFunctionAddress instead.
369void *MCJIT::getPointerToFunction(Function *F) {
370  MutexGuard locked(lock);
371
372  Mangler Mang(TM->getDataLayout());
373  SmallString<128> Name;
374  TM->getNameWithPrefix(Name, F, Mang);
375
376  if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
377    bool AbortOnFailure = !F->hasExternalWeakLinkage();
378    void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
379    updateGlobalMapping(F, Addr);
380    return Addr;
381  }
382
383  Module *M = F->getParent();
384  bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
385
386  // Make sure the relevant module has been compiled and loaded.
387  if (HasBeenAddedButNotLoaded)
388    generateCodeForModule(M);
389  else if (!OwnedModules.hasModuleBeenLoaded(M)) {
390    // If this function doesn't belong to one of our modules, we're done.
391    // FIXME: Asking for the pointer to a function that hasn't been registered,
392    //        and isn't a declaration (which is handled above) should probably
393    //        be an assertion.
394    return nullptr;
395  }
396
397  // FIXME: Should the Dyld be retaining module information? Probably not.
398  //
399  // This is the accessor for the target address, so make sure to check the
400  // load address of the symbol, not the local address.
401  return (void*)Dyld.getSymbol(Name).getAddress();
402}
403
404void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
405    bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
406  for (; I != E; ++I) {
407    ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
408  }
409}
410
411void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
412  // Execute global ctors/dtors for each module in the program.
413  runStaticConstructorsDestructorsInModulePtrSet(
414      isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
415  runStaticConstructorsDestructorsInModulePtrSet(
416      isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
417  runStaticConstructorsDestructorsInModulePtrSet(
418      isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
419}
420
421Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
422                                                 ModulePtrSet::iterator I,
423                                                 ModulePtrSet::iterator E) {
424  for (; I != E; ++I) {
425    Function *F = (*I)->getFunction(FnName);
426    if (F && !F->isDeclaration())
427      return F;
428  }
429  return nullptr;
430}
431
432Function *MCJIT::FindFunctionNamed(const char *FnName) {
433  Function *F = FindFunctionNamedInModulePtrSet(
434      FnName, OwnedModules.begin_added(), OwnedModules.end_added());
435  if (!F)
436    F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
437                                        OwnedModules.end_loaded());
438  if (!F)
439    F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
440                                        OwnedModules.end_finalized());
441  return F;
442}
443
444GenericValue MCJIT::runFunction(Function *F,
445                                const std::vector<GenericValue> &ArgValues) {
446  assert(F && "Function *F was null at entry to run()");
447
448  void *FPtr = getPointerToFunction(F);
449  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
450  FunctionType *FTy = F->getFunctionType();
451  Type *RetTy = FTy->getReturnType();
452
453  assert((FTy->getNumParams() == ArgValues.size() ||
454          (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
455         "Wrong number of arguments passed into function!");
456  assert(FTy->getNumParams() == ArgValues.size() &&
457         "This doesn't support passing arguments through varargs (yet)!");
458
459  // Handle some common cases first.  These cases correspond to common `main'
460  // prototypes.
461  if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
462    switch (ArgValues.size()) {
463    case 3:
464      if (FTy->getParamType(0)->isIntegerTy(32) &&
465          FTy->getParamType(1)->isPointerTy() &&
466          FTy->getParamType(2)->isPointerTy()) {
467        int (*PF)(int, char **, const char **) =
468          (int(*)(int, char **, const char **))(intptr_t)FPtr;
469
470        // Call the function.
471        GenericValue rv;
472        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
473                                 (char **)GVTOP(ArgValues[1]),
474                                 (const char **)GVTOP(ArgValues[2])));
475        return rv;
476      }
477      break;
478    case 2:
479      if (FTy->getParamType(0)->isIntegerTy(32) &&
480          FTy->getParamType(1)->isPointerTy()) {
481        int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
482
483        // Call the function.
484        GenericValue rv;
485        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
486                                 (char **)GVTOP(ArgValues[1])));
487        return rv;
488      }
489      break;
490    case 1:
491      if (FTy->getNumParams() == 1 &&
492          FTy->getParamType(0)->isIntegerTy(32)) {
493        GenericValue rv;
494        int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
495        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
496        return rv;
497      }
498      break;
499    }
500  }
501
502  // Handle cases where no arguments are passed first.
503  if (ArgValues.empty()) {
504    GenericValue rv;
505    switch (RetTy->getTypeID()) {
506    default: llvm_unreachable("Unknown return type for function call!");
507    case Type::IntegerTyID: {
508      unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
509      if (BitWidth == 1)
510        rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
511      else if (BitWidth <= 8)
512        rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
513      else if (BitWidth <= 16)
514        rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
515      else if (BitWidth <= 32)
516        rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
517      else if (BitWidth <= 64)
518        rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
519      else
520        llvm_unreachable("Integer types > 64 bits not supported");
521      return rv;
522    }
523    case Type::VoidTyID:
524      rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
525      return rv;
526    case Type::FloatTyID:
527      rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
528      return rv;
529    case Type::DoubleTyID:
530      rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
531      return rv;
532    case Type::X86_FP80TyID:
533    case Type::FP128TyID:
534    case Type::PPC_FP128TyID:
535      llvm_unreachable("long double not supported yet");
536    case Type::PointerTyID:
537      return PTOGV(((void*(*)())(intptr_t)FPtr)());
538    }
539  }
540
541  llvm_unreachable("Full-featured argument passing not supported yet!");
542}
543
544void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
545  if (!isSymbolSearchingDisabled()) {
546    void *ptr =
547      reinterpret_cast<void*>(
548        static_cast<uintptr_t>(Resolver.findSymbol(Name).getAddress()));
549    if (ptr)
550      return ptr;
551  }
552
553  /// If a LazyFunctionCreator is installed, use it to get/create the function.
554  if (LazyFunctionCreator)
555    if (void *RP = LazyFunctionCreator(Name))
556      return RP;
557
558  if (AbortOnFailure) {
559    report_fatal_error("Program used external function '"+Name+
560                       "' which could not be resolved!");
561  }
562  return nullptr;
563}
564
565void MCJIT::RegisterJITEventListener(JITEventListener *L) {
566  if (!L)
567    return;
568  MutexGuard locked(lock);
569  EventListeners.push_back(L);
570}
571
572void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
573  if (!L)
574    return;
575  MutexGuard locked(lock);
576  auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
577  if (I != EventListeners.rend()) {
578    std::swap(*I, EventListeners.back());
579    EventListeners.pop_back();
580  }
581}
582
583void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj,
584                                const RuntimeDyld::LoadedObjectInfo &L) {
585  MutexGuard locked(lock);
586  MemMgr->notifyObjectLoaded(this, Obj);
587  for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
588    EventListeners[I]->NotifyObjectEmitted(Obj, L);
589  }
590}
591
592void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) {
593  MutexGuard locked(lock);
594  for (JITEventListener *L : EventListeners)
595    L->NotifyFreeingObject(Obj);
596}
597
598RuntimeDyld::SymbolInfo
599LinkingSymbolResolver::findSymbol(const std::string &Name) {
600  auto Result = ParentEngine.findSymbol(Name, false);
601  // If the symbols wasn't found and it begins with an underscore, try again
602  // without the underscore.
603  if (!Result && Name[0] == '_')
604    Result = ParentEngine.findSymbol(Name.substr(1), false);
605  if (Result)
606    return Result;
607  if (ParentEngine.isSymbolSearchingDisabled())
608    return nullptr;
609  return ClientResolver->findSymbol(Name);
610}
611