diff options
author | Andrew Kaylor <andrew.kaylor@intel.com> | 2013-10-16 00:14:21 +0000 |
---|---|---|
committer | Andrew Kaylor <andrew.kaylor@intel.com> | 2013-10-16 00:14:21 +0000 |
commit | 43507d026bef31100cb0c35614bcf419029a265b (patch) | |
tree | efd60ca4a91ebf848debd8691d136c594c5a23fe | |
parent | 50fd83e832b8a7361e362407555da2e9bd6085eb (diff) | |
download | external_llvm-43507d026bef31100cb0c35614bcf419029a265b.zip external_llvm-43507d026bef31100cb0c35614bcf419029a265b.tar.gz external_llvm-43507d026bef31100cb0c35614bcf419029a265b.tar.bz2 |
Adding support for deregistering EH frames with MCJIT.
Patch by Yaron Keren
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192753 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/ExecutionEngine/RTDyldMemoryManager.h | 2 | ||||
-rw-r--r-- | include/llvm/ExecutionEngine/RuntimeDyld.h | 2 | ||||
-rw-r--r-- | lib/ExecutionEngine/MCJIT/MCJIT.cpp | 2 | ||||
-rw-r--r-- | lib/ExecutionEngine/MCJIT/MCJIT.h | 6 | ||||
-rw-r--r-- | lib/ExecutionEngine/RTDyldMemoryManager.cpp | 83 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp | 11 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp | 14 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h | 2 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h | 2 | ||||
-rw-r--r-- | tools/lli/RemoteMemoryManager.h | 1 |
10 files changed, 118 insertions, 7 deletions
diff --git a/include/llvm/ExecutionEngine/RTDyldMemoryManager.h b/include/llvm/ExecutionEngine/RTDyldMemoryManager.h index 468b823..3ad2e50 100644 --- a/include/llvm/ExecutionEngine/RTDyldMemoryManager.h +++ b/include/llvm/ExecutionEngine/RTDyldMemoryManager.h @@ -60,6 +60,8 @@ public: /// be the case for local execution) these two values will be the same. virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size); + virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size); + /// This method returns the address of the specified function or variable. /// It is used to resolve symbols during module linking. virtual uint64_t getSymbolAddress(const std::string &Name); diff --git a/include/llvm/ExecutionEngine/RuntimeDyld.h b/include/llvm/ExecutionEngine/RuntimeDyld.h index a5610fb..b832438 100644 --- a/include/llvm/ExecutionEngine/RuntimeDyld.h +++ b/include/llvm/ExecutionEngine/RuntimeDyld.h @@ -71,6 +71,8 @@ public: /// the actual target-specific EH frame registration. void registerEHFrames(); + void deregisterEHFrames(); + StringRef getErrorString(); }; diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index fa2c984..bcd0886 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -62,6 +62,8 @@ MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM, } MCJIT::~MCJIT() { + Dyld.deregisterEHFrames(); + LoadedObjectMap::iterator it, end = LoadedObjects.end(); for (it = LoadedObjects.begin(); it != end; ++it) { ObjectImage *Obj = it->second; diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.h b/lib/ExecutionEngine/MCJIT/MCJIT.h index fe59288..3932692 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.h +++ b/lib/ExecutionEngine/MCJIT/MCJIT.h @@ -55,6 +55,12 @@ public: ClientMM->registerEHFrames(Addr, LoadAddr, Size); } + virtual void deregisterEHFrames(uint8_t *Addr, + uint64_t LoadAddr, + size_t Size) { + ClientMM->deregisterEHFrames(Addr, LoadAddr, Size); + } + virtual bool finalizeMemory(std::string *ErrMsg = 0) { return ClientMM->finalizeMemory(ErrMsg); } diff --git a/lib/ExecutionEngine/RTDyldMemoryManager.cpp b/lib/ExecutionEngine/RTDyldMemoryManager.cpp index 9926576..11a5ec7 100644 --- a/lib/ExecutionEngine/RTDyldMemoryManager.cpp +++ b/lib/ExecutionEngine/RTDyldMemoryManager.cpp @@ -42,17 +42,53 @@ RTDyldMemoryManager::~RTDyldMemoryManager() {} #if HAVE_EHTABLE_SUPPORT extern "C" void __register_frame(void*); +extern "C" void __deregister_frame(void*); +#else +// The building compiler does not have __(de)register_frame but +// it may be found at runtime in a dynamically-loaded library. +// For example, this happens when building LLVM with Visual C++ +// but using the MingW runtime. +void __register_frame(void *p) { + static bool Searched = false; + static void *rf = 0; + + if (!Searched) { + Searched = true; + rf = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( + "__register_frame"); + } + if (rf) + ((void (*)(void *))rf)(p); +} + +void __deregister_frame(void *p) { + static bool Searched = false; + static void *df = 0; + + if (!Searched) { + Searched = true; + df = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol( + "__deregister_frame"); + } + if (df) + ((void (*)(void *))df)(p); +} +#endif + +#ifdef __APPLE__ -static const char *processFDE(const char *Entry) { +static const char *processFDE(const char *Entry, bool isDeregister) { const char *P = Entry; uint32_t Length = *((const uint32_t *)P); P += 4; uint32_t Offset = *((const uint32_t *)P); if (Offset != 0) - __register_frame(const_cast<char *>(Entry)); + if (isDeregister) + __deregister_frame(const_cast<char *>(Entry)); + else + __register_frame(const_cast<char *>(Entry)); return P + Length; } -#endif // This implementation handles frame registration for local targets. // Memory managers for remote targets should re-implement this function @@ -60,15 +96,50 @@ static const char *processFDE(const char *Entry) { void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) { -#if HAVE_EHTABLE_SUPPORT + // On OS X OS X __register_frame takes a single FDE as an argument. + // See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html const char *P = (const char *)Addr; const char *End = P + Size; do { - P = processFDE(P); + P = processFDE(P, false); } while(P != End); -#endif } +void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, + uint64_t LoadAddr, + size_t Size) { + const char *P = (const char *)Addr; + const char *End = P + Size; + do { + P = processFDE(P, true); + } while(P != End); +} + +#else + +void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, + uint64_t LoadAddr, + size_t Size) { + // On Linux __register_frame takes a single argument: + // a pointer to the start of the .eh_frame section. + + // How can it find the end? Because crtendS.o is linked + // in and it has an .eh_frame section with four zero chars. + // FIXME: make sure EH frame is followed by four zero bytes. + // This should be done in the linker RuntimeDyldELF::getEHFrameSection(), + // return pointer to .eh_frame properly appended by four zero bytes. + // If the linker can not fixed, do it here. + __register_frame(Addr); +} + +void RTDyldMemoryManager::deregisterEHFrames(uint8_t *Addr, + uint64_t LoadAddr, + size_t Size) { + __deregister_frame(Addr); +} + +#endif + static int jit_noop() { return 0; } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index bda32d4..49f8dc3 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -32,6 +32,9 @@ namespace llvm { void RuntimeDyldImpl::registerEHFrames() { } +void RuntimeDyldImpl::deregisterEHFrames() { +} + // Resolve the relocations for all symbols we currently know about. void RuntimeDyldImpl::resolveRelocations() { // First, resolve relocations associated with external symbols. @@ -595,7 +598,13 @@ StringRef RuntimeDyld::getErrorString() { } void RuntimeDyld::registerEHFrames() { - return Dyld->registerEHFrames(); + if (Dyld) + Dyld->registerEHFrames(); +} + +void RuntimeDyld::deregisterEHFrames() { + if (Dyld) + Dyld->deregisterEHFrames(); } } // end namespace llvm diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 97e03f0..fa14c8c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -160,10 +160,24 @@ void RuntimeDyldELF::registerEHFrames() { uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; size_t EHFrameSize = Sections[EHFrameSID].Size; MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); + RegisteredEHFrameSections.push_back(EHFrameSID); } UnregisteredEHFrameSections.clear(); } +void RuntimeDyldELF::deregisterEHFrames() { + if (!MemMgr) + return; + for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) { + SID EHFrameSID = RegisteredEHFrameSections[i]; + uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; + uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; + size_t EHFrameSize = Sections[EHFrameSID].Size; + MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); + } + RegisteredEHFrameSections.clear(); +} + ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { if (Buffer->getBufferSize() < ELF::EI_NIDENT) llvm_unreachable("Unexpected ELF object size"); diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h index 058078b..3adf827 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h @@ -126,6 +126,7 @@ class RuntimeDyldELF : public RuntimeDyldImpl { // in a table until we receive a request to register all unregistered // EH frame sections with the memory manager. SmallVector<SID, 2> UnregisteredEHFrameSections; + SmallVector<SID, 2> RegisteredEHFrameSections; public: RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) @@ -141,6 +142,7 @@ public: virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const; virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer); virtual void registerEHFrames(); + virtual void deregisterEHFrames(); virtual void finalizeLoad(ObjSectionToIDMap &SectionMap); virtual ~RuntimeDyldELF(); }; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index 2fb2813..0628fa7 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -342,6 +342,8 @@ public: virtual void registerEHFrames(); + virtual void deregisterEHFrames(); + virtual void finalizeLoad(ObjSectionToIDMap &SectionMap) {} }; diff --git a/tools/lli/RemoteMemoryManager.h b/tools/lli/RemoteMemoryManager.h index 16d0a80..5d0456f 100644 --- a/tools/lli/RemoteMemoryManager.h +++ b/tools/lli/RemoteMemoryManager.h @@ -87,6 +87,7 @@ public: // For now, remote EH frame registration isn't supported. Remote symbol // resolution is a prerequisite to supporting remote EH frame registration. void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {} + void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {} // This is a non-interface function used by lli void setRemoteTarget(RemoteTarget *T) { Target = T; } |