1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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 //===----------------------------------------------------------------------===//
11 #include "llvm/ExecutionEngine/GenericValue.h"
12 #include "llvm/ExecutionEngine/JITEventListener.h"
13 #include "llvm/ExecutionEngine/MCJIT.h"
14 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/LegacyPassManager.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/DynamicLibrary.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/MutexGuard.h"
31 void ObjectCache::anchor() {}
35 static struct RegisterJIT {
36 RegisterJIT() { MCJIT::Register(); }
41 extern "C" void LLVMLinkInMCJIT() {
44 ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M,
45 std::string *ErrorStr,
46 std::unique_ptr<RTDyldMemoryManager> MemMgr,
47 std::unique_ptr<TargetMachine> TM) {
48 // Try to register the program as a source of symbols to resolve against.
50 // FIXME: Don't do this here.
51 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
53 std::unique_ptr<RTDyldMemoryManager> MM = std::move(MemMgr);
55 MM = std::unique_ptr<SectionMemoryManager>(new SectionMemoryManager());
57 return new MCJIT(std::move(M), std::move(TM), std::move(MM));
60 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
61 std::unique_ptr<RTDyldMemoryManager> MM)
62 : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
63 MemMgr(this, std::move(MM)), Dyld(&MemMgr), ObjCache(nullptr) {
64 // FIXME: We are managing our modules, so we do not want the base class
65 // ExecutionEngine to manage them as well. To avoid double destruction
66 // of the first (and only) module added in ExecutionEngine constructor
67 // we remove it from EE and will destruct it ourselves.
69 // It may make sense to move our module manager (based on SmallStPtr) back
70 // into EE if the JIT and Interpreter can live with it.
71 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
72 // runStaticConstructorsDestructors could be moved back to EE as well.
74 std::unique_ptr<Module> First = std::move(Modules[0]);
77 OwnedModules.addModule(std::move(First));
78 setDataLayout(TM->getDataLayout());
79 RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
83 MutexGuard locked(lock);
85 Dyld.deregisterEHFrames();
87 for (auto &Obj : LoadedObjects)
89 NotifyFreeingObject(*Obj);
94 void MCJIT::addModule(std::unique_ptr<Module> M) {
95 MutexGuard locked(lock);
96 OwnedModules.addModule(std::move(M));
99 bool MCJIT::removeModule(Module *M) {
100 MutexGuard locked(lock);
101 return OwnedModules.removeModule(M);
104 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
105 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
107 report_fatal_error(Dyld.getErrorString());
109 NotifyObjectEmitted(*Obj, *L);
111 LoadedObjects.push_back(std::move(Obj));
114 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
115 std::unique_ptr<object::ObjectFile> ObjFile;
116 std::unique_ptr<MemoryBuffer> MemBuf;
117 std::tie(ObjFile, MemBuf) = Obj.takeBinary();
118 addObjectFile(std::move(ObjFile));
119 Buffers.push_back(std::move(MemBuf));
122 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
123 Archives.push_back(std::move(A));
126 void MCJIT::setObjectCache(ObjectCache* NewCache) {
127 MutexGuard locked(lock);
131 std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
132 MutexGuard locked(lock);
134 // This must be a module which has already been added but not loaded to this
135 // MCJIT instance, since these conditions are tested by our caller,
136 // generateCodeForModule.
138 legacy::PassManager PM;
140 M->setDataLayout(*TM->getDataLayout());
142 // The RuntimeDyld will take ownership of this shortly
143 SmallVector<char, 4096> ObjBufferSV;
144 raw_svector_ostream ObjStream(ObjBufferSV);
146 // Turn the machine code intermediate representation into bytes in memory
147 // that may be executed.
148 if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
149 report_fatal_error("Target does not support MC emission!");
151 // Initialize passes.
153 // Flush the output buffer to get the generated code into memory
156 std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
157 new ObjectMemoryBuffer(std::move(ObjBufferSV)));
159 // If we have an object cache, tell it about the new object.
160 // Note that we're using the compiled image, not the loaded image (as below).
162 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
163 // to create a temporary object here and delete it after the call.
164 MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
165 ObjCache->notifyObjectCompiled(M, MB);
168 return CompiledObjBuffer;
171 void MCJIT::generateCodeForModule(Module *M) {
172 // Get a thread lock to make sure we aren't trying to load multiple times
173 MutexGuard locked(lock);
175 // This must be a module which has already been added to this MCJIT instance.
176 assert(OwnedModules.ownsModule(M) &&
177 "MCJIT::generateCodeForModule: Unknown module.");
179 // Re-compilation is not supported
180 if (OwnedModules.hasModuleBeenLoaded(M))
183 std::unique_ptr<MemoryBuffer> ObjectToLoad;
184 // Try to load the pre-compiled object from cache if possible
186 ObjectToLoad = ObjCache->getObject(M);
188 // If the cache did not contain a suitable object, compile the object
190 ObjectToLoad = emitObject(M);
191 assert(ObjectToLoad && "Compilation did not produce an object.");
194 // Load the object into the dynamic linker.
195 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
196 ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =
197 object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
198 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
199 Dyld.loadObject(*LoadedObject.get());
202 report_fatal_error(Dyld.getErrorString());
204 NotifyObjectEmitted(*LoadedObject.get(), *L);
206 Buffers.push_back(std::move(ObjectToLoad));
207 LoadedObjects.push_back(std::move(*LoadedObject));
209 OwnedModules.markModuleAsLoaded(M);
212 void MCJIT::finalizeLoadedModules() {
213 MutexGuard locked(lock);
215 // Resolve any outstanding relocations.
216 Dyld.resolveRelocations();
218 OwnedModules.markAllLoadedModulesAsFinalized();
220 // Register EH frame data for any module we own which has been loaded
221 Dyld.registerEHFrames();
223 // Set page permissions.
224 MemMgr.finalizeMemory();
227 // FIXME: Rename this.
228 void MCJIT::finalizeObject() {
229 MutexGuard locked(lock);
231 // Generate code for module is going to move objects out of the 'added' list,
232 // so we need to copy that out before using it:
233 SmallVector<Module*, 16> ModsToAdd;
234 for (auto M : OwnedModules.added())
235 ModsToAdd.push_back(M);
237 for (auto M : ModsToAdd)
238 generateCodeForModule(M);
240 finalizeLoadedModules();
243 void MCJIT::finalizeModule(Module *M) {
244 MutexGuard locked(lock);
246 // This must be a module which has already been added to this MCJIT instance.
247 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
249 // If the module hasn't been compiled, just do that.
250 if (!OwnedModules.hasModuleBeenLoaded(M))
251 generateCodeForModule(M);
253 finalizeLoadedModules();
256 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
257 Mangler Mang(TM->getDataLayout());
258 SmallString<128> FullName;
259 Mang.getNameWithPrefix(FullName, Name);
260 return Dyld.getSymbol(FullName).getAddress();
263 Module *MCJIT::findModuleForSymbol(const std::string &Name,
264 bool CheckFunctionsOnly) {
265 MutexGuard locked(lock);
267 // If it hasn't already been generated, see if it's in one of our modules.
268 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
269 E = OwnedModules.end_added();
272 Function *F = M->getFunction(Name);
273 if (F && !F->isDeclaration())
275 if (!CheckFunctionsOnly) {
276 GlobalVariable *G = M->getGlobalVariable(Name);
277 if (G && !G->isDeclaration())
279 // FIXME: Do we need to worry about global aliases?
282 // We didn't find the symbol in any of our modules.
286 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
287 bool CheckFunctionsOnly)
289 MutexGuard locked(lock);
291 // First, check to see if we already have this symbol.
292 uint64_t Addr = getExistingSymbolAddress(Name);
296 for (object::OwningBinary<object::Archive> &OB : Archives) {
297 object::Archive *A = OB.getBinary();
298 // Look for our symbols in each Archive
299 object::Archive::child_iterator ChildIt = A->findSym(Name);
300 if (ChildIt != A->child_end()) {
301 // FIXME: Support nested archives?
302 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
303 ChildIt->getAsBinary();
304 if (ChildBinOrErr.getError())
306 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
307 if (ChildBin->isObject()) {
308 std::unique_ptr<object::ObjectFile> OF(
309 static_cast<object::ObjectFile *>(ChildBin.release()));
310 // This causes the object file to be loaded.
311 addObjectFile(std::move(OF));
312 // The address should be here now.
313 Addr = getExistingSymbolAddress(Name);
320 // If it hasn't already been generated, see if it's in one of our modules.
321 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
323 generateCodeForModule(M);
325 // Check the RuntimeDyld table again, it should be there now.
326 return getExistingSymbolAddress(Name);
329 // If a LazyFunctionCreator is installed, use it to get/create the function.
330 // FIXME: Should we instead have a LazySymbolCreator callback?
331 if (LazyFunctionCreator)
332 Addr = (uint64_t)LazyFunctionCreator(Name);
337 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
338 MutexGuard locked(lock);
339 uint64_t Result = getSymbolAddress(Name, false);
341 finalizeLoadedModules();
345 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
346 MutexGuard locked(lock);
347 uint64_t Result = getSymbolAddress(Name, true);
349 finalizeLoadedModules();
353 // Deprecated. Use getFunctionAddress instead.
354 void *MCJIT::getPointerToFunction(Function *F) {
355 MutexGuard locked(lock);
357 Mangler Mang(TM->getDataLayout());
358 SmallString<128> Name;
359 TM->getNameWithPrefix(Name, F, Mang);
361 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
362 bool AbortOnFailure = !F->hasExternalWeakLinkage();
363 void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
364 updateGlobalMapping(F, Addr);
368 Module *M = F->getParent();
369 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
371 // Make sure the relevant module has been compiled and loaded.
372 if (HasBeenAddedButNotLoaded)
373 generateCodeForModule(M);
374 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
375 // If this function doesn't belong to one of our modules, we're done.
376 // FIXME: Asking for the pointer to a function that hasn't been registered,
377 // and isn't a declaration (which is handled above) should probably
382 // FIXME: Should the Dyld be retaining module information? Probably not.
384 // This is the accessor for the target address, so make sure to check the
385 // load address of the symbol, not the local address.
386 return (void*)Dyld.getSymbol(Name).getAddress();
389 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
390 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
391 for (; I != E; ++I) {
392 ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
396 void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
397 // Execute global ctors/dtors for each module in the program.
398 runStaticConstructorsDestructorsInModulePtrSet(
399 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
400 runStaticConstructorsDestructorsInModulePtrSet(
401 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
402 runStaticConstructorsDestructorsInModulePtrSet(
403 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
406 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
407 ModulePtrSet::iterator I,
408 ModulePtrSet::iterator E) {
409 for (; I != E; ++I) {
410 Function *F = (*I)->getFunction(FnName);
411 if (F && !F->isDeclaration())
417 Function *MCJIT::FindFunctionNamed(const char *FnName) {
418 Function *F = FindFunctionNamedInModulePtrSet(
419 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
421 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
422 OwnedModules.end_loaded());
424 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
425 OwnedModules.end_finalized());
429 GenericValue MCJIT::runFunction(Function *F,
430 const std::vector<GenericValue> &ArgValues) {
431 assert(F && "Function *F was null at entry to run()");
433 void *FPtr = getPointerToFunction(F);
434 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
435 FunctionType *FTy = F->getFunctionType();
436 Type *RetTy = FTy->getReturnType();
438 assert((FTy->getNumParams() == ArgValues.size() ||
439 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
440 "Wrong number of arguments passed into function!");
441 assert(FTy->getNumParams() == ArgValues.size() &&
442 "This doesn't support passing arguments through varargs (yet)!");
444 // Handle some common cases first. These cases correspond to common `main'
446 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
447 switch (ArgValues.size()) {
449 if (FTy->getParamType(0)->isIntegerTy(32) &&
450 FTy->getParamType(1)->isPointerTy() &&
451 FTy->getParamType(2)->isPointerTy()) {
452 int (*PF)(int, char **, const char **) =
453 (int(*)(int, char **, const char **))(intptr_t)FPtr;
455 // Call the function.
457 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
458 (char **)GVTOP(ArgValues[1]),
459 (const char **)GVTOP(ArgValues[2])));
464 if (FTy->getParamType(0)->isIntegerTy(32) &&
465 FTy->getParamType(1)->isPointerTy()) {
466 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
468 // Call the function.
470 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
471 (char **)GVTOP(ArgValues[1])));
476 if (FTy->getNumParams() == 1 &&
477 FTy->getParamType(0)->isIntegerTy(32)) {
479 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
480 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
487 // Handle cases where no arguments are passed first.
488 if (ArgValues.empty()) {
490 switch (RetTy->getTypeID()) {
491 default: llvm_unreachable("Unknown return type for function call!");
492 case Type::IntegerTyID: {
493 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
495 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
496 else if (BitWidth <= 8)
497 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
498 else if (BitWidth <= 16)
499 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
500 else if (BitWidth <= 32)
501 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
502 else if (BitWidth <= 64)
503 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
505 llvm_unreachable("Integer types > 64 bits not supported");
509 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
511 case Type::FloatTyID:
512 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
514 case Type::DoubleTyID:
515 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
517 case Type::X86_FP80TyID:
518 case Type::FP128TyID:
519 case Type::PPC_FP128TyID:
520 llvm_unreachable("long double not supported yet");
521 case Type::PointerTyID:
522 return PTOGV(((void*(*)())(intptr_t)FPtr)());
526 llvm_unreachable("Full-featured argument passing not supported yet!");
529 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
530 if (!isSymbolSearchingDisabled()) {
531 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
536 /// If a LazyFunctionCreator is installed, use it to get/create the function.
537 if (LazyFunctionCreator)
538 if (void *RP = LazyFunctionCreator(Name))
541 if (AbortOnFailure) {
542 report_fatal_error("Program used external function '"+Name+
543 "' which could not be resolved!");
548 void MCJIT::RegisterJITEventListener(JITEventListener *L) {
551 MutexGuard locked(lock);
552 EventListeners.push_back(L);
555 void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
558 MutexGuard locked(lock);
559 auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
560 if (I != EventListeners.rend()) {
561 std::swap(*I, EventListeners.back());
562 EventListeners.pop_back();
566 void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj,
567 const RuntimeDyld::LoadedObjectInfo &L) {
568 MutexGuard locked(lock);
569 MemMgr.notifyObjectLoaded(this, Obj);
570 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
571 EventListeners[I]->NotifyObjectEmitted(Obj, L);
575 void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) {
576 MutexGuard locked(lock);
577 for (JITEventListener *L : EventListeners)
578 L->NotifyFreeingObject(Obj);
581 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
582 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
583 // If the symbols wasn't found and it begins with an underscore, try again
584 // without the underscore.
585 if (!Result && Name[0] == '_')
586 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
589 if (ParentEngine->isSymbolSearchingDisabled())
591 return ClientMM->getSymbolAddress(Name);