2010-11-18 00:06:43 +08:00
|
|
|
//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-10-24 08:19:14 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-11-07 02:51:59 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2013-04-26 05:02:36 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectCache.h"
|
2013-10-01 09:47:35 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectImage.h"
|
2011-03-22 09:06:42 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2013-10-24 08:19:14 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2013-10-01 09:47:35 +08:00
|
|
|
class MCJIT;
|
|
|
|
|
|
|
|
// This is a helper class that the MCJIT execution engine uses for linking
|
|
|
|
// functions across modules that it owns. It aggregates the memory manager
|
|
|
|
// that is passed in to the MCJIT constructor and defers most functionality
|
|
|
|
// to that object.
|
|
|
|
class LinkingMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
|
|
|
LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
|
|
|
|
: ParentEngine(Parent), ClientMM(MM) {}
|
|
|
|
|
|
|
|
virtual uint64_t getSymbolAddress(const std::string &Name);
|
|
|
|
|
|
|
|
// Functions deferred to client memory manager
|
|
|
|
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName) {
|
|
|
|
return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName,
|
|
|
|
bool IsReadOnly) {
|
2013-10-01 09:47:35 +08:00
|
|
|
return ClientMM->allocateDataSection(Size, Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
SectionID, SectionName, IsReadOnly);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
2014-02-13 05:30:07 +08:00
|
|
|
|
|
|
|
virtual void reserveAllocationSpace(
|
|
|
|
uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) {
|
|
|
|
return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool needsToReserveAllocationSpace() {
|
|
|
|
return ClientMM->needsToReserveAllocationSpace();
|
|
|
|
}
|
2013-10-01 09:47:35 +08:00
|
|
|
|
2013-10-04 08:49:38 +08:00
|
|
|
virtual void notifyObjectLoaded(ExecutionEngine *EE,
|
|
|
|
const ObjectImage *Obj) {
|
|
|
|
ClientMM->notifyObjectLoaded(EE, Obj);
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:25:48 +08:00
|
|
|
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
|
|
|
|
ClientMM->registerEHFrames(Addr, LoadAddr, Size);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
|
|
|
|
2013-10-16 08:14:21 +08:00
|
|
|
virtual void deregisterEHFrames(uint8_t *Addr,
|
|
|
|
uint64_t LoadAddr,
|
|
|
|
size_t Size) {
|
|
|
|
ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
|
|
|
|
}
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual bool finalizeMemory(std::string *ErrMsg = 0) {
|
|
|
|
return ClientMM->finalizeMemory(ErrMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MCJIT *ParentEngine;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<RTDyldMemoryManager> ClientMM;
|
2013-10-01 09:47:35 +08:00
|
|
|
};
|
2012-10-03 05:18:39 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
// About Module states: added->loaded->finalized.
|
2013-10-19 17:03:20 +08:00
|
|
|
//
|
|
|
|
// The purpose of the "added" state is having modules in standby. (added=known
|
|
|
|
// but not compiled). The idea is that you can add a module to provide function
|
|
|
|
// definitions but if nothing in that module is referenced by a module in which
|
2013-10-24 18:04:47 +08:00
|
|
|
// a function is executed (note the wording here because it's not exactly the
|
2013-10-19 17:03:20 +08:00
|
|
|
// ideal case) then the module never gets compiled. This is sort of lazy
|
|
|
|
// compilation.
|
|
|
|
//
|
|
|
|
// The purpose of the "loaded" state (loaded=compiled and required sections
|
|
|
|
// copied into local memory but not yet ready for execution) is to have an
|
|
|
|
// intermediate state wherein clients can remap the addresses of sections, using
|
|
|
|
// MCJIT::mapSectionAddress, (in preparation for later copying to a new location
|
|
|
|
// or an external process) before relocations and page permissions are applied.
|
|
|
|
//
|
|
|
|
// It might not be obvious at first glance, but the "remote-mcjit" case in the
|
|
|
|
// lli tool does this. In that case, the intermediate action is taken by the
|
|
|
|
// RemoteMemoryManager in response to the notifyObjectLoaded function being
|
|
|
|
// called.
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
class MCJIT : public ExecutionEngine {
|
2012-08-21 23:42:49 +08:00
|
|
|
MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
|
|
|
|
bool AllocateGVsWithCode);
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
|
2013-10-01 09:47:35 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
class OwningModuleContainer {
|
2013-10-01 09:47:35 +08:00
|
|
|
public:
|
2013-10-24 08:19:14 +08:00
|
|
|
OwningModuleContainer() {
|
|
|
|
}
|
|
|
|
~OwningModuleContainer() {
|
|
|
|
freeModulePtrSet(AddedModules);
|
|
|
|
freeModulePtrSet(LoadedModules);
|
|
|
|
freeModulePtrSet(FinalizedModules);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
|
|
|
|
ModulePtrSet::iterator end_added() { return AddedModules.end(); }
|
|
|
|
|
|
|
|
ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
|
|
|
|
ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }
|
|
|
|
|
|
|
|
ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
|
|
|
|
ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
|
|
|
|
|
|
|
|
void addModule(Module *M) {
|
|
|
|
AddedModules.insert(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeModule(Module *M) {
|
|
|
|
return AddedModules.erase(M) || LoadedModules.erase(M) ||
|
|
|
|
FinalizedModules.erase(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasModuleBeenAddedButNotLoaded(Module *M) {
|
|
|
|
return AddedModules.count(M) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasModuleBeenLoaded(Module *M) {
|
|
|
|
// If the module is in either the "loaded" or "finalized" sections it
|
|
|
|
// has been loaded.
|
|
|
|
return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasModuleBeenFinalized(Module *M) {
|
|
|
|
return FinalizedModules.count(M) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ownsModule(Module* M) {
|
|
|
|
return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) ||
|
|
|
|
(FinalizedModules.count(M) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void markModuleAsLoaded(Module *M) {
|
|
|
|
// This checks against logic errors in the MCJIT implementation.
|
|
|
|
// This function should never be called with either a Module that MCJIT
|
|
|
|
// does not own or a Module that has already been loaded and/or finalized.
|
|
|
|
assert(AddedModules.count(M) &&
|
|
|
|
"markModuleAsLoaded: Module not found in AddedModules");
|
2013-10-01 09:47:35 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
// Remove the module from the "Added" set.
|
|
|
|
AddedModules.erase(M);
|
|
|
|
|
|
|
|
// Add the Module to the "Loaded" set.
|
|
|
|
LoadedModules.insert(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void markModuleAsFinalized(Module *M) {
|
|
|
|
// This checks against logic errors in the MCJIT implementation.
|
|
|
|
// This function should never be called with either a Module that MCJIT
|
|
|
|
// does not own, a Module that has not been loaded or a Module that has
|
|
|
|
// already been finalized.
|
|
|
|
assert(LoadedModules.count(M) &&
|
|
|
|
"markModuleAsFinalized: Module not found in LoadedModules");
|
|
|
|
|
|
|
|
// Remove the module from the "Loaded" section of the list.
|
|
|
|
LoadedModules.erase(M);
|
|
|
|
|
|
|
|
// Add the Module to the "Finalized" section of the list by inserting it
|
|
|
|
// before the 'end' iterator.
|
|
|
|
FinalizedModules.insert(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void markAllLoadedModulesAsFinalized() {
|
|
|
|
for (ModulePtrSet::iterator I = LoadedModules.begin(),
|
|
|
|
E = LoadedModules.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Module *M = *I;
|
|
|
|
FinalizedModules.insert(M);
|
|
|
|
}
|
|
|
|
LoadedModules.clear();
|
|
|
|
}
|
2013-10-01 09:47:35 +08:00
|
|
|
|
|
|
|
private:
|
2013-10-24 08:19:14 +08:00
|
|
|
ModulePtrSet AddedModules;
|
|
|
|
ModulePtrSet LoadedModules;
|
|
|
|
ModulePtrSet FinalizedModules;
|
|
|
|
|
|
|
|
void freeModulePtrSet(ModulePtrSet& MPS) {
|
|
|
|
// Go through the module set and delete everything.
|
|
|
|
for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) {
|
|
|
|
Module *M = *I;
|
|
|
|
delete M;
|
|
|
|
}
|
|
|
|
MPS.clear();
|
|
|
|
}
|
2013-10-01 09:47:35 +08:00
|
|
|
};
|
|
|
|
|
2011-03-19 06:48:41 +08:00
|
|
|
TargetMachine *TM;
|
|
|
|
MCContext *Ctx;
|
2013-10-01 09:47:35 +08:00
|
|
|
LinkingMemoryManager MemMgr;
|
2012-08-08 02:33:00 +08:00
|
|
|
RuntimeDyld Dyld;
|
2012-11-07 02:51:59 +08:00
|
|
|
SmallVector<JITEventListener*, 2> EventListeners;
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
OwningModuleContainer OwnedModules;
|
2013-10-01 09:47:35 +08:00
|
|
|
|
2014-01-08 12:09:09 +08:00
|
|
|
SmallVector<object::Archive*, 2> Archives;
|
|
|
|
|
|
|
|
typedef SmallVector<ObjectImage *, 2> LoadedObjectList;
|
|
|
|
LoadedObjectList LoadedObjects;
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-04-26 05:02:36 +08:00
|
|
|
// An optional ObjectCache to be notified of compiled objects and used to
|
|
|
|
// perform lookup of pre-compiled code to avoid re-compilation.
|
|
|
|
ObjectCache *ObjCache;
|
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
Function *FindFunctionNamedInModulePtrSet(const char *FnName,
|
|
|
|
ModulePtrSet::iterator I,
|
|
|
|
ModulePtrSet::iterator E);
|
|
|
|
|
|
|
|
void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,
|
|
|
|
ModulePtrSet::iterator I,
|
|
|
|
ModulePtrSet::iterator E);
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
public:
|
|
|
|
~MCJIT();
|
|
|
|
|
|
|
|
/// @name ExecutionEngine interface implementation
|
|
|
|
/// @{
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void addModule(Module *M);
|
2014-01-08 12:09:09 +08:00
|
|
|
virtual void addObjectFile(object::ObjectFile *O);
|
|
|
|
virtual void addArchive(object::Archive *O);
|
2013-10-24 08:19:14 +08:00
|
|
|
virtual bool removeModule(Module *M);
|
|
|
|
|
|
|
|
/// FindFunctionNamed - Search all of the active modules to find the one that
|
|
|
|
/// defines FnName. This is very slow operation and shouldn't be used for
|
|
|
|
/// general code.
|
|
|
|
virtual Function *FindFunctionNamed(const char *FnName);
|
2010-11-18 00:06:43 +08:00
|
|
|
|
2013-04-26 05:02:36 +08:00
|
|
|
/// Sets the object manager that MCJIT should use to avoid compilation.
|
|
|
|
virtual void setObjectCache(ObjectCache *manager);
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void generateCodeForModule(Module *M);
|
|
|
|
|
2013-05-17 18:01:46 +08:00
|
|
|
/// finalizeObject - ensure the module is fully processed and is usable.
|
|
|
|
///
|
|
|
|
/// It is the user-level function for completing the process of making the
|
|
|
|
/// object usable for execution. It should be called after sections within an
|
|
|
|
/// object have been relocated using mapSectionAddress. When this method is
|
|
|
|
/// called the MCJIT execution engine will reapply relocations for a loaded
|
|
|
|
/// object.
|
2013-10-19 17:03:20 +08:00
|
|
|
/// Is it OK to finalize a set of modules, add modules and finalize again.
|
2013-10-22 07:27:02 +08:00
|
|
|
// FIXME: Do we really need both of these?
|
2012-11-06 04:57:16 +08:00
|
|
|
virtual void finalizeObject();
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void finalizeModule(Module *);
|
|
|
|
void finalizeLoadedModules();
|
2012-11-06 04:57:16 +08:00
|
|
|
|
2013-10-24 08:19:14 +08:00
|
|
|
/// runStaticConstructorsDestructors - This method is used to execute all of
|
|
|
|
/// the static constructors or destructors for a program.
|
|
|
|
///
|
|
|
|
/// \param isDtors - Run the destructors instead of constructors.
|
|
|
|
void runStaticConstructorsDestructors(bool isDtors);
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
virtual void *getPointerToBasicBlock(BasicBlock *BB);
|
|
|
|
|
|
|
|
virtual void *getPointerToFunction(Function *F);
|
|
|
|
|
|
|
|
virtual void *recompileAndRelinkFunction(Function *F);
|
|
|
|
|
|
|
|
virtual void freeMachineCodeForFunction(Function *F);
|
|
|
|
|
|
|
|
virtual GenericValue runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues);
|
|
|
|
|
2011-03-23 02:05:27 +08:00
|
|
|
/// getPointerToNamedFunction - This method returns the address of the
|
|
|
|
/// specified function by using the dlsym function call. As such it is only
|
|
|
|
/// useful for resolving library symbols, not code generated symbols.
|
|
|
|
///
|
|
|
|
/// If AbortOnFailure is false and no function with the given name is
|
|
|
|
/// found, this function silently returns a null pointer. Otherwise,
|
|
|
|
/// it prints a message to stderr and aborts.
|
|
|
|
///
|
2012-01-06 05:16:14 +08:00
|
|
|
virtual void *getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure = true);
|
2012-03-29 05:46:36 +08:00
|
|
|
|
2012-01-17 07:50:55 +08:00
|
|
|
/// mapSectionAddress - map a section to its target address space value.
|
|
|
|
/// Map the address of a JIT section as returned from the memory manager
|
|
|
|
/// to the address in the target process as the running code will see it.
|
|
|
|
/// This is the address which will be used for relocation resolution.
|
2012-09-14 05:50:06 +08:00
|
|
|
virtual void mapSectionAddress(const void *LocalAddress,
|
|
|
|
uint64_t TargetAddress) {
|
2012-01-17 07:50:55 +08:00
|
|
|
Dyld.mapSectionAddress(LocalAddress, TargetAddress);
|
|
|
|
}
|
2012-11-07 02:51:59 +08:00
|
|
|
virtual void RegisterJITEventListener(JITEventListener *L);
|
|
|
|
virtual void UnregisterJITEventListener(JITEventListener *L);
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// If successful, these function will implicitly finalize all loaded objects.
|
|
|
|
// To get a function address within MCJIT without causing a finalize, use
|
|
|
|
// getSymbolAddress.
|
|
|
|
virtual uint64_t getGlobalValueAddress(const std::string &Name);
|
|
|
|
virtual uint64_t getFunctionAddress(const std::string &Name);
|
|
|
|
|
2014-01-24 03:23:28 +08:00
|
|
|
virtual TargetMachine *getTargetMachine() { return TM; }
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
/// @}
|
|
|
|
/// @name (Private) Registration Interfaces
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
static void Register() {
|
|
|
|
MCJITCtor = createJIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExecutionEngine *createJIT(Module *M,
|
|
|
|
std::string *ErrorStr,
|
2013-05-15 03:29:00 +08:00
|
|
|
RTDyldMemoryManager *MemMgr,
|
2010-11-18 00:06:43 +08:00
|
|
|
bool GVsWithCode,
|
2011-05-14 05:51:29 +08:00
|
|
|
TargetMachine *TM);
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
// @}
|
2012-08-08 02:33:00 +08:00
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// This is not directly exposed via the ExecutionEngine API, but it is
|
|
|
|
// used by the LinkingMemoryManager.
|
|
|
|
uint64_t getSymbolAddress(const std::string &Name,
|
|
|
|
bool CheckFunctionsOnly);
|
|
|
|
|
2012-08-08 02:33:00 +08:00
|
|
|
protected:
|
|
|
|
/// emitObject -- Generate a JITed object in memory from the specified module
|
|
|
|
/// Currently, MCJIT only supports a single module and the module passed to
|
|
|
|
/// this function call is expected to be the contained module. The module
|
2013-12-07 19:21:42 +08:00
|
|
|
/// is passed as a parameter here to prepare for multiple module support in
|
2012-08-08 02:33:00 +08:00
|
|
|
/// the future.
|
2013-04-26 05:02:36 +08:00
|
|
|
ObjectBufferStream* emitObject(Module *M);
|
|
|
|
|
2012-11-07 02:51:59 +08:00
|
|
|
void NotifyObjectEmitted(const ObjectImage& Obj);
|
|
|
|
void NotifyFreeingObject(const ObjectImage& Obj);
|
2013-10-01 09:47:35 +08:00
|
|
|
|
|
|
|
uint64_t getExistingSymbolAddress(const std::string &Name);
|
|
|
|
Module *findModuleForSymbol(const std::string &Name,
|
|
|
|
bool CheckFunctionsOnly);
|
2010-11-18 00:06:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|