diff options
Diffstat (limited to 'lib/ExecutionEngine')
18 files changed, 890 insertions, 605 deletions
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 2890174..a744d0c 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -402,14 +402,15 @@ ExecutionEngine *ExecutionEngine::create(Module *M, std::string *ErrorStr, CodeGenOpt::Level OptLevel, bool GVsWithCode) { - return EngineBuilder(M) + EngineBuilder EB = EngineBuilder(M) .setEngineKind(ForceInterpreter ? EngineKind::Interpreter : EngineKind::JIT) .setErrorStr(ErrorStr) .setOptLevel(OptLevel) - .setAllocateGVsWithCode(GVsWithCode) - .create(); + .setAllocateGVsWithCode(GVsWithCode); + + return EB.create(); } /// createJIT - This is the factory method for creating a JIT for the current @@ -430,21 +431,25 @@ ExecutionEngine *ExecutionEngine::createJIT(Module *M, // Use the defaults for extra parameters. Users can use EngineBuilder to // set them. - StringRef MArch = ""; - StringRef MCPU = ""; - SmallVector<std::string, 1> MAttrs; + EngineBuilder EB(M); + EB.setEngineKind(EngineKind::JIT); + EB.setErrorStr(ErrorStr); + EB.setRelocationModel(RM); + EB.setCodeModel(CMM); + EB.setAllocateGVsWithCode(GVsWithCode); + EB.setOptLevel(OL); + EB.setJITMemoryManager(JMM); - Triple TT(M->getTargetTriple()); // TODO: permit custom TargetOptions here - TargetMachine *TM = - EngineBuilder::selectTarget(TT, MArch, MCPU, MAttrs, TargetOptions(), RM, - CMM, OL, ErrorStr); + TargetMachine *TM = EB.selectTarget(); if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0; return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM); } -ExecutionEngine *EngineBuilder::create() { +ExecutionEngine *EngineBuilder::create(TargetMachine *TM) { + OwningPtr<TargetMachine> TheTM(TM); // Take ownership. + // Make sure we can resolve symbols in the program as well. The zero arg // to the function tells DynamicLibrary to load the program, not a library. if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) @@ -465,29 +470,24 @@ ExecutionEngine *EngineBuilder::create() { // Unless the interpreter was explicitly selected or the JIT is not linked, // try making a JIT. - if (WhichEngine & EngineKind::JIT) { + if ((WhichEngine & EngineKind::JIT) && TheTM) { Triple TT(M->getTargetTriple()); - if (TargetMachine *TM = EngineBuilder::selectTarget(TT, MArch, MCPU, MAttrs, - Options, - RelocModel, CMModel, - OptLevel, ErrorStr)) { - if (!TM->getTarget().hasJIT()) { - errs() << "WARNING: This target JIT is not designed for the host" - << " you are running. If bad things happen, please choose" - << " a different -march switch.\n"; - } + if (!TM->getTarget().hasJIT()) { + errs() << "WARNING: This target JIT is not designed for the host" + << " you are running. If bad things happen, please choose" + << " a different -march switch.\n"; + } - if (UseMCJIT && ExecutionEngine::MCJITCtor) { - ExecutionEngine *EE = - ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, - AllocateGVsWithCode, TM); - if (EE) return EE; - } else if (ExecutionEngine::JITCtor) { - ExecutionEngine *EE = - ExecutionEngine::JITCtor(M, ErrorStr, JMM, - AllocateGVsWithCode, TM); - if (EE) return EE; - } + if (UseMCJIT && ExecutionEngine::MCJITCtor) { + ExecutionEngine *EE = + ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, + AllocateGVsWithCode, TheTM.take()); + if (EE) return EE; + } else if (ExecutionEngine::JITCtor) { + ExecutionEngine *EE = + ExecutionEngine::JITCtor(M, ErrorStr, JMM, + AllocateGVsWithCode, TheTM.take()); + if (EE) return EE; } } diff --git a/lib/ExecutionEngine/JIT/JIT.cpp b/lib/ExecutionEngine/JIT/JIT.cpp index 16b8ee2..a942299 100644 --- a/lib/ExecutionEngine/JIT/JIT.cpp +++ b/lib/ExecutionEngine/JIT/JIT.cpp @@ -269,7 +269,8 @@ extern "C" { JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, JITMemoryManager *jmm, bool GVsWithCode) - : ExecutionEngine(M), TM(tm), TJI(tji), JMM(jmm), + : ExecutionEngine(M), TM(tm), TJI(tji), + JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()), AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) { setTargetData(TM.getTargetData()); @@ -323,6 +324,7 @@ JIT::~JIT() { AllJits->Remove(this); delete jitstate; delete JCE; + // JMM is a ownership of JCE, so we no need delete JMM here. delete &TM; } diff --git a/lib/ExecutionEngine/JIT/JIT.h b/lib/ExecutionEngine/JIT/JIT.h index c557981..2ae155b 100644 --- a/lib/ExecutionEngine/JIT/JIT.h +++ b/lib/ExecutionEngine/JIT/JIT.h @@ -118,7 +118,7 @@ public: const std::vector<GenericValue> &ArgValues); /// getPointerToNamedFunction - This method returns the address of the - /// specified function by using the dlsym function call. As such it is only + /// specified function by using the MemoryManager. 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 diff --git a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp index d404d0c..2d1775c 100644 --- a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp +++ b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp @@ -23,10 +23,22 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/Memory.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/DynamicLibrary.h" +#include "llvm/Config/config.h" #include <vector> #include <cassert> #include <climits> #include <cstring> + +#if defined(__linux__) +#if defined(HAVE_SYS_STAT_H) +#include <sys/stat.h> +#endif +#include <fcntl.h> +#include <unistd.h> +#endif + using namespace llvm; STATISTIC(NumSlabs, "Number of slabs of memory allocated by the JIT"); @@ -315,13 +327,7 @@ namespace { static const size_t DefaultSizeThreshold; /// 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. - /// + /// specified function by using the dlsym function call. virtual void *getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure = true); @@ -771,9 +777,6 @@ bool DefaultJITMemoryManager::CheckInvariants(std::string &ErrorStr) { //===----------------------------------------------------------------------===// // getPointerToNamedFunction() implementation. //===----------------------------------------------------------------------===// -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/DynamicLibrary.h" -#include "llvm/Config/config.h" // AtExitHandlers - List of functions to call when the program exits, // registered with the atexit() library function. @@ -793,8 +796,7 @@ static void runAtExitHandlers() { //===----------------------------------------------------------------------===// // Function stubs that are invoked instead of certain library calls -//===----------------------------------------------------------------------===// - +// // Force the following functions to be linked in to anything that uses the // JIT. This is a hack designed to work around the all-too-clever Glibc // strategy of making these functions work differently when inlined vs. when @@ -802,11 +804,6 @@ static void runAtExitHandlers() { // that the dynamic linker can't see. For more info, search for // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274. #if defined(__linux__) -#if defined(HAVE_SYS_STAT_H) -#include <sys/stat.h> -#endif -#include <fcntl.h> -#include <unistd.h> /* stat functions are redirecting to __xstat with a version number. On x86-64 * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat' * available as an exported symbol, so we have to add it explicitly. diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index cbb23d3..44f89cf 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -74,9 +74,9 @@ MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji, OS.flush(); // Load the object into the dynamic linker. - // FIXME: It would be nice to avoid making yet another copy. - MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(), - Buffer.size())); + MemoryBuffer *MB = MemoryBuffer::getMemBuffer(StringRef(Buffer.data(), + Buffer.size()), + "", false); if (Dyld.loadObject(MB)) report_fatal_error(Dyld.getErrorString()); // Resolve any relocations. @@ -218,7 +218,7 @@ GenericValue MCJIT::runFunction(Function *F, void *MCJIT::getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure){ - if (!isSymbolSearchingDisabled()) { + if (!isSymbolSearchingDisabled() && MemMgr) { void *ptr = MemMgr->getPointerToNamedFunction(Name, false); if (ptr) return ptr; diff --git a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h index dac8b26..a68949a 100644 --- a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h +++ b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h @@ -27,7 +27,8 @@ class MCJITMemoryManager : public RTDyldMemoryManager { // FIXME: Multiple modules. Module *M; public: - MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {} + MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : + JMM(jmm?jmm:JITMemoryManager::CreateDefaultMemManager()), M(m) {} // We own the JMM, so make sure to delete it. ~MCJITMemoryManager() { delete JMM; } diff --git a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt index 002e63c..cbf7cf1 100644 --- a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt +++ b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_library(LLVMRuntimeDyld + GDBRegistrar.cpp RuntimeDyld.cpp - RuntimeDyldMachO.cpp RuntimeDyldELF.cpp + RuntimeDyldMachO.cpp ) diff --git a/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h b/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h deleted file mode 100644 index 2d777da..0000000 --- a/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h +++ /dev/null @@ -1,388 +0,0 @@ -//===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used -// to represent a loadable ELF image. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H -#define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H - -#include "llvm/Object/ELF.h" - - -namespace llvm { - -using support::endianness; -using namespace llvm::object; - -template<support::endianness target_endianness, bool is64Bits> -class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> { - LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) - - typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; - typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; - typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; - typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; - - typedef typename ELFObjectFile<target_endianness, is64Bits>:: - Elf_Ehdr Elf_Ehdr; - Elf_Ehdr *Header; - - // Update section headers according to the current location in memory - virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap); - // Record memory addresses for cleanup - virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr); - -protected: - virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; - -public: - DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap, - error_code &ec); - - // Methods for type inquiry through isa, cast, and dyn_cast - static inline bool classof(const Binary *v) { - return (isa<ELFObjectFile<target_endianness, is64Bits> >(v) - && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v))); - } - static inline bool classof( - const ELFObjectFile<target_endianness, is64Bits> *v) { - return v->isDyldType(); - } - static inline bool classof(const DyldELFObject *v) { - return true; - } -}; - -template<support::endianness target_endianness, bool is64Bits> -DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object, - std::vector<uint8_t*> *MemoryMap, error_code &ec) - : ELFObjectFile<target_endianness, is64Bits>(Object, ec) - , Header(0) { - this->isDyldELFObject = true; - Header = const_cast<Elf_Ehdr *>( - reinterpret_cast<const Elf_Ehdr *>(this->base())); - if (Header->e_shoff == 0) - return; - - // Mark the image as a dynamic shared library - Header->e_type = ELF::ET_DYN; - - rebaseObject(MemoryMap); -} - -// Walk through the ELF headers, updating virtual addresses to reflect where -// the object is currently loaded in memory -template<support::endianness target_endianness, bool is64Bits> -void DyldELFObject<target_endianness, is64Bits>::rebaseObject( - std::vector<uint8_t*> *MemoryMap) { - typedef typename ELFDataTypeTypedefHelper< - target_endianness, is64Bits>::value_type addr_type; - - uint8_t *base_p = const_cast<uint8_t *>(this->base()); - Elf_Shdr *sectionTable = - reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff); - uint64_t numSections = this->getNumSections(); - - // Allocate memory space for NOBITS sections (such as .bss), which only exist - // in memory, but don't occupy space in the object file. - // Update the address in the section headers to reflect this allocation. - for (uint64_t index = 0; index < numSections; index++) { - Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>( - reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize); - - // Only update sections that are meant to be present in program memory - if (sec->sh_flags & ELF::SHF_ALLOC) { - uint8_t *addr = base_p + sec->sh_offset; - if (sec->sh_type == ELF::SHT_NOBITS) { - addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1)); - saveAddress(MemoryMap, addr); - } - else { - // FIXME: Currently memory with RWX permissions is allocated. In the - // future, make sure that permissions are as necessary - if (sec->sh_flags & ELF::SHF_WRITE) { - // see FIXME above - } - if (sec->sh_flags & ELF::SHF_EXECINSTR) { - // see FIXME above - } - } - assert(sizeof(addr_type) == sizeof(intptr_t) && - "Cross-architecture ELF dy-load is not supported!"); - sec->sh_addr = static_cast<addr_type>(intptr_t(addr)); - } - } - - // Now allocate actual space for COMMON symbols, which also don't occupy - // space in the object file. - // We want to allocate space for all COMMON symbols at once, so the flow is: - // 1. Go over all symbols, find those that are in COMMON. For each such - // symbol, record its size and the value field in its symbol header in a - // special vector. - // 2. Allocate memory for all COMMON symbols in one fell swoop. - // 3. Using the recorded information from (1), update the address fields in - // the symbol headers of the COMMON symbols to reflect their allocated - // address. - uint64_t TotalSize = 0; - std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo; - error_code ec = object_error::success; - for (symbol_iterator si = this->begin_symbols(), - se = this->end_symbols(); si != se; si.increment(ec)) { - uint64_t Size = 0; - ec = si->getSize(Size); - Elf_Sym* symb = const_cast<Elf_Sym*>( - this->getSymbol(si->getRawDataRefImpl())); - if (ec == object_error::success && - this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) { - SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size)); - TotalSize += Size; - } - } - - uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1); - saveAddress(MemoryMap, SectionPtr); - - typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator - AddrInfoIterator; - AddrInfoIterator EndIter = SymbAddrInfo.end(); - for (AddrInfoIterator AddrIter = SymbAddrInfo.begin(); - AddrIter != EndIter; ++AddrIter) { - assert(sizeof(addr_type) == sizeof(intptr_t) && - "Cross-architecture ELF dy-load is not supported!"); - *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr)); - SectionPtr += AddrIter->second; - } -} - -// Record memory addresses for callers -template<support::endianness target_endianness, bool is64Bits> -void DyldELFObject<target_endianness, is64Bits>::saveAddress( - std::vector<uint8_t*> *MemoryMap, uint8_t* addr) { - if (MemoryMap) - MemoryMap->push_back(addr); - else - errs() << "WARNING: Memory leak - cannot record memory for ELF dyld."; -} - -template<support::endianness target_endianness, bool is64Bits> -error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress( - DataRefImpl Symb, uint64_t &Result) const { - this->validateSymbol(Symb); - const Elf_Sym *symb = this->getSymbol(Symb); - if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) { - Result = symb->st_value; - return object_error::success; - } - else { - return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress( - Symb, Result); - } -} - -} - -#endif - -//===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used -// to represent a loadable ELF image. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H -#define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H - -#include "llvm/Object/ELF.h" - - -namespace llvm { - -using support::endianness; -using namespace llvm::object; - -template<support::endianness target_endianness, bool is64Bits> -class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> { - LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) - - typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; - typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; - typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; - typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; - - typedef typename ELFObjectFile<target_endianness, is64Bits>:: - Elf_Ehdr Elf_Ehdr; - Elf_Ehdr *Header; - - // Update section headers according to the current location in memory - virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap); - // Record memory addresses for cleanup - virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr); - -protected: - virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; - -public: - DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap, - error_code &ec); - - // Methods for type inquiry through isa, cast, and dyn_cast - static inline bool classof(const Binary *v) { - return (isa<ELFObjectFile<target_endianness, is64Bits> >(v) - && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v))); - } - static inline bool classof( - const ELFObjectFile<target_endianness, is64Bits> *v) { - return v->isDyldType(); - } - static inline bool classof(const DyldELFObject *v) { - return true; - } -}; - -template<support::endianness target_endianness, bool is64Bits> -DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object, - std::vector<uint8_t*> *MemoryMap, error_code &ec) - : ELFObjectFile<target_endianness, is64Bits>(Object, ec) - , Header(0) { - this->isDyldELFObject = true; - Header = const_cast<Elf_Ehdr *>( - reinterpret_cast<const Elf_Ehdr *>(this->base())); - if (Header->e_shoff == 0) - return; - - // Mark the image as a dynamic shared library - Header->e_type = ELF::ET_DYN; - - rebaseObject(MemoryMap); -} - -// Walk through the ELF headers, updating virtual addresses to reflect where -// the object is currently loaded in memory -template<support::endianness target_endianness, bool is64Bits> -void DyldELFObject<target_endianness, is64Bits>::rebaseObject( - std::vector<uint8_t*> *MemoryMap) { - typedef typename ELFDataTypeTypedefHelper< - target_endianness, is64Bits>::value_type addr_type; - - uint8_t *base_p = const_cast<uint8_t *>(this->base()); - Elf_Shdr *sectionTable = - reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff); - uint64_t numSections = this->getNumSections(); - - // Allocate memory space for NOBITS sections (such as .bss), which only exist - // in memory, but don't occupy space in the object file. - // Update the address in the section headers to reflect this allocation. - for (uint64_t index = 0; index < numSections; index++) { - Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>( - reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize); - - // Only update sections that are meant to be present in program memory - if (sec->sh_flags & ELF::SHF_ALLOC) { - uint8_t *addr = base_p + sec->sh_offset; - if (sec->sh_type == ELF::SHT_NOBITS) { - addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1)); - saveAddress(MemoryMap, addr); - } - else { - // FIXME: Currently memory with RWX permissions is allocated. In the - // future, make sure that permissions are as necessary - if (sec->sh_flags & ELF::SHF_WRITE) { - // see FIXME above - } - if (sec->sh_flags & ELF::SHF_EXECINSTR) { - // see FIXME above - } - } - assert(sizeof(addr_type) == sizeof(intptr_t) && - "Cross-architecture ELF dy-load is not supported!"); - sec->sh_addr = static_cast<addr_type>(intptr_t(addr)); - } - } - - // Now allocate actual space for COMMON symbols, which also don't occupy - // space in the object file. - // We want to allocate space for all COMMON symbols at once, so the flow is: - // 1. Go over all symbols, find those that are in COMMON. For each such - // symbol, record its size and the value field in its symbol header in a - // special vector. - // 2. Allocate memory for all COMMON symbols in one fell swoop. - // 3. Using the recorded information from (1), update the address fields in - // the symbol headers of the COMMON symbols to reflect their allocated - // address. - uint64_t TotalSize = 0; - std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo; - error_code ec = object_error::success; - for (symbol_iterator si = this->begin_symbols(), - se = this->end_symbols(); si != se; si.increment(ec)) { - uint64_t Size = 0; - ec = si->getSize(Size); - Elf_Sym* symb = const_cast<Elf_Sym*>( - this->getSymbol(si->getRawDataRefImpl())); - if (ec == object_error::success && - this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) { - SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size)); - TotalSize += Size; - } - } - - uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1); - saveAddress(MemoryMap, SectionPtr); - - typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator - AddrInfoIterator; - AddrInfoIterator EndIter = SymbAddrInfo.end(); - for (AddrInfoIterator AddrIter = SymbAddrInfo.begin(); - AddrIter != EndIter; ++AddrIter) { - assert(sizeof(addr_type) == sizeof(intptr_t) && - "Cross-architecture ELF dy-load is not supported!"); - *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr)); - SectionPtr += AddrIter->second; - } -} - -// Record memory addresses for callers -template<support::endianness target_endianness, bool is64Bits> -void DyldELFObject<target_endianness, is64Bits>::saveAddress( - std::vector<uint8_t*> *MemoryMap, uint8_t* addr) { - if (MemoryMap) - MemoryMap->push_back(addr); - else - errs() << "WARNING: Memory leak - cannot record memory for ELF dyld."; -} - -template<support::endianness target_endianness, bool is64Bits> -error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress( - DataRefImpl Symb, uint64_t &Result) const { - this->validateSymbol(Symb); - const Elf_Sym *symb = this->getSymbol(Symb); - if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) { - Result = symb->st_value; - return object_error::success; - } - else { - return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress( - Symb, Result); - } -} - -} - -#endif - diff --git a/lib/ExecutionEngine/RuntimeDyld/GDBRegistrar.cpp b/lib/ExecutionEngine/RuntimeDyld/GDBRegistrar.cpp new file mode 100644 index 0000000..8b50101 --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/GDBRegistrar.cpp @@ -0,0 +1,214 @@ +//===-- GDBRegistrar.cpp - Registers objects with GDB ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "JITRegistrar.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/Support/MutexGuard.h" +#include "llvm/Support/Mutex.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Compiler.h" + +using namespace llvm; + +// This must be kept in sync with gdb/gdb/jit.h . +extern "C" { + + typedef enum { + JIT_NOACTION = 0, + JIT_REGISTER_FN, + JIT_UNREGISTER_FN + } jit_actions_t; + + struct jit_code_entry { + struct jit_code_entry *next_entry; + struct jit_code_entry *prev_entry; + const char *symfile_addr; + uint64_t symfile_size; + }; + + struct jit_descriptor { + uint32_t version; + // This should be jit_actions_t, but we want to be specific about the + // bit-width. + uint32_t action_flag; + struct jit_code_entry *relevant_entry; + struct jit_code_entry *first_entry; + }; + + // We put information about the JITed function in this global, which the + // debugger reads. Make sure to specify the version statically, because the + // debugger checks the version before we can set it during runtime. + static struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 }; + + // Debuggers puts a breakpoint in this function. + LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() { } + +} + +namespace { + +// Buffer for an in-memory object file in executable memory +typedef llvm::DenseMap< const char*, + std::pair<std::size_t, jit_code_entry*> > + RegisteredObjectBufferMap; + +/// Global access point for the JIT debugging interface designed for use with a +/// singleton toolbox. Handles thread-safe registration and deregistration of +/// object files that are in executable memory managed by the client of this +/// class. +class GDBJITRegistrar : public JITRegistrar { + /// A map of in-memory object files that have been registered with the + /// JIT interface. + RegisteredObjectBufferMap ObjectBufferMap; + +public: + /// Instantiates the JIT service. + GDBJITRegistrar() : ObjectBufferMap() {} + + /// Unregisters each object that was previously registered and releases all + /// internal resources. + virtual ~GDBJITRegistrar(); + + /// Creates an entry in the JIT registry for the buffer @p Object, + /// which must contain an object file in executable memory with any + /// debug information for the debugger. + void registerObject(const MemoryBuffer &Object); + + /// Removes the internal registration of @p Object, and + /// frees associated resources. + /// Returns true if @p Object was found in ObjectBufferMap. + bool deregisterObject(const MemoryBuffer &Object); + +private: + /// Deregister the debug info for the given object file from the debugger + /// and delete any temporary copies. This private method does not remove + /// the function from Map so that it can be called while iterating over Map. + void deregisterObjectInternal(RegisteredObjectBufferMap::iterator I); +}; + +/// Lock used to serialize all jit registration events, since they +/// modify global variables. +llvm::sys::Mutex JITDebugLock; + +/// Acquire the lock and do the registration. +void NotifyDebugger(jit_code_entry* JITCodeEntry) { + llvm::MutexGuard locked(JITDebugLock); + __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; + + // Insert this entry at the head of the list. + JITCodeEntry->prev_entry = NULL; + jit_code_entry* NextEntry = __jit_debug_descriptor.first_entry; + JITCodeEntry->next_entry = NextEntry; + if (NextEntry != NULL) { + NextEntry->prev_entry = JITCodeEntry; + } + __jit_debug_descriptor.first_entry = JITCodeEntry; + __jit_debug_descriptor.relevant_entry = JITCodeEntry; + __jit_debug_register_code(); +} + +GDBJITRegistrar::~GDBJITRegistrar() { + // Free all registered object files. + for (RegisteredObjectBufferMap::iterator I = ObjectBufferMap.begin(), E = ObjectBufferMap.end(); + I != E; ++I) { + // Call the private method that doesn't update the map so our iterator + // doesn't break. + deregisterObjectInternal(I); + } + ObjectBufferMap.clear(); +} + +void GDBJITRegistrar::registerObject(const MemoryBuffer &Object) { + + const char *Buffer = Object.getBufferStart(); + size_t Size = Object.getBufferSize(); + + assert(Buffer && "Attempt to register a null object with a debugger."); + assert(ObjectBufferMap.find(Buffer) == ObjectBufferMap.end() && + "Second attempt to perform debug registration."); + jit_code_entry* JITCodeEntry = new jit_code_entry(); + + if (JITCodeEntry == 0) { + llvm::report_fatal_error( + "Allocation failed when registering a JIT entry!\n"); + } + else { + JITCodeEntry->symfile_addr = Buffer; + JITCodeEntry->symfile_size = Size; + + ObjectBufferMap[Buffer] = std::make_pair(Size, JITCodeEntry); + NotifyDebugger(JITCodeEntry); + } +} + +bool GDBJITRegistrar::deregisterObject(const MemoryBuffer& Object) { + const char *Buffer = Object.getBufferStart(); + RegisteredObjectBufferMap::iterator I = ObjectBufferMap.find(Buffer); + + if (I != ObjectBufferMap.end()) { + deregisterObjectInternal(I); + ObjectBufferMap.erase(I); + return true; + } + return false; +} + +void GDBJITRegistrar::deregisterObjectInternal( + RegisteredObjectBufferMap::iterator I) { + + jit_code_entry*& JITCodeEntry = I->second.second; + + // Acquire the lock and do the unregistration. + { + llvm::MutexGuard locked(JITDebugLock); + __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN; + + // Remove the jit_code_entry from the linked list. + jit_code_entry* PrevEntry = JITCodeEntry->prev_entry; + jit_code_entry* NextEntry = JITCodeEntry->next_entry; + + if (NextEntry) { + NextEntry->prev_entry = PrevEntry; + } + if (PrevEntry) { + PrevEntry->next_entry = NextEntry; + } + else { + assert(__jit_debug_descriptor.first_entry == JITCodeEntry); + __jit_debug_descriptor.first_entry = NextEntry; + } + + // Tell the debugger which entry we removed, and unregister the code. + __jit_debug_descriptor.relevant_entry = JITCodeEntry; + __jit_debug_register_code(); + } + + delete JITCodeEntry; + JITCodeEntry = NULL; +} + +} // end namespace + +namespace llvm { + +JITRegistrar& JITRegistrar::getGDBRegistrar() { + static GDBJITRegistrar* sRegistrar = NULL; + if (sRegistrar == NULL) { + // The mutex is here so that it won't slow down access once the registrar + // is instantiated + llvm::MutexGuard locked(JITDebugLock); + // Check again to be sure another thread didn't create this while we waited + if (sRegistrar == NULL) { + sRegistrar = new GDBJITRegistrar; + } + } + return *sRegistrar; +} + +} // namespace llvm diff --git a/lib/ExecutionEngine/RuntimeDyld/JITRegistrar.h b/lib/ExecutionEngine/RuntimeDyld/JITRegistrar.h new file mode 100644 index 0000000..f964bc6 --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/JITRegistrar.h @@ -0,0 +1,43 @@ +//===-- JITRegistrar.h - Registers objects with a debugger ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_EXECUTION_ENGINE_JIT_REGISTRAR_H +#define LLVM_EXECUTION_ENGINE_JIT_REGISTRAR_H + +#include "llvm/Support/MemoryBuffer.h" + +namespace llvm { + +/// Global access point for the JIT debugging interface. +class JITRegistrar { +public: + /// Instantiates the JIT service. + JITRegistrar() {} + + /// Unregisters each object that was previously registered and releases all + /// internal resources. + virtual ~JITRegistrar() {} + + /// Creates an entry in the JIT registry for the buffer @p Object, + /// which must contain an object file in executable memory with any + /// debug information for the debugger. + virtual void registerObject(const MemoryBuffer &Object) = 0; + + /// Removes the internal registration of @p Object, and + /// frees associated resources. + /// Returns true if @p Object was previously registered. + virtual bool deregisterObject(const MemoryBuffer &Object) = 0; + + /// Returns a reference to a GDB JIT registrar singleton + static JITRegistrar& getGDBRegistrar(); +}; + +} // end namespace llvm + +#endif // LLVM_EXECUTION_ENGINE_JIT_REGISTRAR_H diff --git a/lib/ExecutionEngine/RuntimeDyld/ObjectImage.h b/lib/ExecutionEngine/RuntimeDyld/ObjectImage.h new file mode 100644 index 0000000..8206ead --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/ObjectImage.h @@ -0,0 +1,59 @@ +//===---- ObjectImage.h - Format independent executuable object image -----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares a file format independent ObjectImage class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_RUNTIMEDYLD_OBJECT_IMAGE_H +#define LLVM_RUNTIMEDYLD_OBJECT_IMAGE_H + +#include "llvm/Object/ObjectFile.h" + +namespace llvm { + +class ObjectImage { + ObjectImage(); // = delete + ObjectImage(const ObjectImage &other); // = delete +protected: + object::ObjectFile *ObjFile; + +public: + ObjectImage(object::ObjectFile *Obj) { ObjFile = Obj; } + virtual ~ObjectImage() {} + + virtual object::symbol_iterator begin_symbols() const + { return ObjFile->begin_symbols(); } + virtual object::symbol_iterator end_symbols() const + { return ObjFile->end_symbols(); } + + virtual object::section_iterator begin_sections() const + { return ObjFile->begin_sections(); } + virtual object::section_iterator end_sections() const + { return ObjFile->end_sections(); } + + virtual /* Triple::ArchType */ unsigned getArch() const + { return ObjFile->getArch(); } + + // Subclasses can override these methods to update the image with loaded + // addresses for sections and common symbols + virtual void updateSectionAddress(const object::SectionRef &Sec, + uint64_t Addr) {} + virtual void updateSymbolAddress(const object::SymbolRef &Sym, uint64_t Addr) + {} + + // Subclasses can override this method to provide JIT debugging support + virtual void registerWithDebugger() {} + virtual void deregisterWithDebugger() {} +}; + +} // end namespace llvm + +#endif // LLVM_RUNTIMEDYLD_OBJECT_IMAGE_H + diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index ff4a2c8..1b1840a 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -26,11 +26,19 @@ RuntimeDyldImpl::~RuntimeDyldImpl() {} namespace llvm { - +namespace { + // Helper for extensive error checking in debug builds. + error_code Check(error_code Err) { + if (Err) { + report_fatal_error(Err.message()); + } + return Err; + } +} // end anonymous namespace // Resolve the relocations for all symbols we currently know about. void RuntimeDyldImpl::resolveRelocations() { - // First, resolve relocations assotiated with external symbols. + // First, resolve relocations associated with external symbols. resolveSymbols(); // Just iterate over the sections we have and resolve all the relocations @@ -51,90 +59,110 @@ void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress, llvm_unreachable("Attempting to remap address of unknown section!"); } +// Subclasses can implement this method to create specialized image instances +// The caller owns the the pointer that is returned. +ObjectImage *RuntimeDyldImpl::createObjectImage(const MemoryBuffer *InputBuffer) { + ObjectFile *ObjFile = ObjectFile::createObjectFile(const_cast<MemoryBuffer*> + (InputBuffer)); + ObjectImage *Obj = new ObjectImage(ObjFile); + return Obj; +} + bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) { - // FIXME: ObjectFile don't modify MemoryBuffer. - // It should use const MemoryBuffer as parameter. - ObjectFile *obj = ObjectFile:: - createObjectFile(const_cast<MemoryBuffer*>(InputBuffer)); + OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); + if (!obj) + report_fatal_error("Unable to create object image from memory buffer!"); Arch = (Triple::ArchType)obj->getArch(); LocalSymbolMap LocalSymbols; // Functions and data symbols from the // object file. ObjSectionToIDMap LocalSections; // Used sections from the object file + CommonSymbolMap CommonSymbols; // Common symbols requiring allocation + uint64_t CommonSize = 0; error_code err; - - // Parse symbols DEBUG(dbgs() << "Parse symbols:\n"); - for (symbol_iterator it = obj->begin_symbols(), itEnd = obj->end_symbols(); - it != itEnd; it.increment(err)) { - if (err) break; + for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); + i != e; i.increment(err)) { + Check(err); object::SymbolRef::Type SymType; StringRef Name; - if ((bool)(err = it->getType(SymType))) break; - if ((bool)(err = it->getName(Name))) break; - - if (SymType == object::SymbolRef::ST_Function || - SymType == object::SymbolRef::ST_Data) { - uint64_t FileOffset; - uint32_t flags; - StringRef sData; - section_iterator sIt = obj->end_sections(); - if ((bool)(err = it->getFileOffset(FileOffset))) break; - if ((bool)(err = it->getFlags(flags))) break; - if ((bool)(err = it->getSection(sIt))) break; - if (sIt == obj->end_sections()) continue; - if ((bool)(err = sIt->getContents(sData))) break; - const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + - (uintptr_t)FileOffset; - uintptr_t SectOffset = (uintptr_t)(SymPtr - (const uint8_t*)sData.begin()); - unsigned SectionID = - findOrEmitSection(*sIt, - SymType == object::SymbolRef::ST_Function, - LocalSections); - bool isGlobal = flags & SymbolRef::SF_Global; - LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); - DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) - << " flags: " << flags - << " SID: " << SectionID - << " Offset: " << format("%p", SectOffset)); - if (isGlobal) - SymbolTable[Name] = SymbolLoc(SectionID, SectOffset); + Check(i->getType(SymType)); + Check(i->getName(Name)); + + uint32_t flags; + Check(i->getFlags(flags)); + + bool isCommon = flags & SymbolRef::SF_Common; + if (isCommon) { + // Add the common symbols to a list. We'll allocate them all below. + uint64_t Size = 0; + Check(i->getSize(Size)); + CommonSize += Size; + CommonSymbols[*i] = Size; + } else { + if (SymType == object::SymbolRef::ST_Function || + SymType == object::SymbolRef::ST_Data) { + uint64_t FileOffset; + StringRef sData; + section_iterator si = obj->end_sections(); + Check(i->getFileOffset(FileOffset)); + Check(i->getSection(si)); + if (si == obj->end_sections()) continue; + Check(si->getContents(sData)); + const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + + (uintptr_t)FileOffset; + uintptr_t SectOffset = (uintptr_t)(SymPtr - (const uint8_t*)sData.begin()); + unsigned SectionID = + findOrEmitSection(*obj, + *si, + SymType == object::SymbolRef::ST_Function, + LocalSections); + bool isGlobal = flags & SymbolRef::SF_Global; + LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); + DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) + << " flags: " << flags + << " SID: " << SectionID + << " Offset: " << format("%p", SectOffset)); + if (isGlobal) + SymbolTable[Name] = SymbolLoc(SectionID, SectOffset); + } } DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); } - if (err) { - report_fatal_error(err.message()); - } + + // Allocate common symbols + if (CommonSize != 0) + emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); // Parse and proccess relocations DEBUG(dbgs() << "Parse relocations:\n"); - for (section_iterator sIt = obj->begin_sections(), - sItEnd = obj->end_sections(); sIt != sItEnd; sIt.increment(err)) { - if (err) break; + for (section_iterator si = obj->begin_sections(), + se = obj->end_sections(); si != se; si.increment(err)) { + Check(err); bool isFirstRelocation = true; unsigned SectionID = 0; StubMap Stubs; - for (relocation_iterator it = sIt->begin_relocations(), - itEnd = sIt->end_relocations(); it != itEnd; it.increment(err)) { - if (err) break; + for (relocation_iterator i = si->begin_relocations(), + e = si->end_relocations(); i != e; i.increment(err)) { + Check(err); // If it's first relocation in this section, find its SectionID if (isFirstRelocation) { - SectionID = findOrEmitSection(*sIt, true, LocalSections); + SectionID = findOrEmitSection(*obj, *si, true, LocalSections); DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); isFirstRelocation = false; } ObjRelocationInfo RI; RI.SectionID = SectionID; - if ((bool)(err = it->getAdditionalInfo(RI.AdditionalInfo))) break; - if ((bool)(err = it->getOffset(RI.Offset))) break; - if ((bool)(err = it->getSymbol(RI.Symbol))) break; - if ((bool)(err = it->getType(RI.Type))) break; + Check(i->getAdditionalInfo(RI.AdditionalInfo)); + Check(i->getOffset(RI.Offset)); + Check(i->getSymbol(RI.Symbol)); + Check(i->getType(RI.Type)); DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo << " Offset: " << format("%p", (uintptr_t)RI.Offset) @@ -142,61 +170,138 @@ bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) { << "\n"); processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs); } - if (err) { - report_fatal_error(err.message()); - } } + + handleObjectLoaded(obj.take()); + return false; } -unsigned RuntimeDyldImpl::emitSection(const SectionRef &Section, +unsigned RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, + const CommonSymbolMap &Map, + uint64_t TotalSize, + LocalSymbolMap &LocalSymbols) { + // Allocate memory for the section + unsigned SectionID = Sections.size(); + uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), + SectionID); + if (!Addr) + report_fatal_error("Unable to allocate memory for common symbols!"); + uint64_t Offset = 0; + Sections.push_back(SectionEntry(Addr, TotalSize, TotalSize, 0)); + memset(Addr, 0, TotalSize); + + DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID + << " new addr: " << format("%p", Addr) + << " DataSize: " << TotalSize + << "\n"); + + // Assign the address of each symbol + for (CommonSymbolMap::const_iterator it = Map.begin(), itEnd = Map.end(); + it != itEnd; it++) { + uint64_t Size = it->second; + StringRef Name; + it->first.getName(Name); + Obj.updateSymbolAddress(it->first, (uint64_t)Addr); + LocalSymbols[Name.data()] = SymbolLoc(SectionID, Offset); + Offset += Size; + Addr += Size; + } + + return SectionID; +} + +unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, + const SectionRef &Section, bool IsCode) { unsigned StubBufSize = 0, StubSize = getMaxStubSize(); error_code err; if (StubSize > 0) { - for (relocation_iterator it = Section.begin_relocations(), - itEnd = Section.end_relocations(); it != itEnd; it.increment(err)) + for (relocation_iterator i = Section.begin_relocations(), + e = Section.end_relocations(); i != e; i.increment(err), Check(err)) StubBufSize += StubSize; } StringRef data; uint64_t Alignment64; - if ((bool)(err = Section.getContents(data))) report_fatal_error(err.message()); - if ((bool)(err = Section.getAlignment(Alignment64))) - report_fatal_error(err.message()); + Check(Section.getContents(data)); + Check(Section.getAlignment(Alignment64)); unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; - unsigned DataSize = data.size(); - unsigned Allocate = DataSize + StubBufSize; + bool IsRequired; + bool IsVirtual; + bool IsZeroInit; + uint64_t DataSize; + Check(Section.isRequiredForExecution(IsRequired)); + Check(Section.isVirtual(IsVirtual)); + Check(Section.isZeroInit(IsZeroInit)); + Check(Section.getSize(DataSize)); + + unsigned Allocate; unsigned SectionID = Sections.size(); - const char *pData = data.data(); - uint8_t *Addr = IsCode - ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) - : MemMgr->allocateDataSection(Allocate, Alignment, SectionID); - - memcpy(Addr, pData, DataSize); - DEBUG(dbgs() << "emitSection SectionID: " << SectionID - << " obj addr: " << format("%p", pData) - << " new addr: " << format("%p", Addr) - << " DataSize: " << DataSize - << " StubBufSize: " << StubBufSize - << " Allocate: " << Allocate - << "\n"); + uint8_t *Addr; + const char *pData = 0; + + // Some sections, such as debug info, don't need to be loaded for execution. + // Leave those where they are. + if (IsRequired) { + Allocate = DataSize + StubBufSize; + Addr = IsCode + ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) + : MemMgr->allocateDataSection(Allocate, Alignment, SectionID); + if (!Addr) + report_fatal_error("Unable to allocate section memory!"); + + // Virtual sections have no data in the object image, so leave pData = 0 + if (!IsVirtual) + pData = data.data(); + + // Zero-initialize or copy the data from the image + if (IsZeroInit || IsVirtual) + memset(Addr, 0, DataSize); + else + memcpy(Addr, pData, DataSize); + + DEBUG(dbgs() << "emitSection SectionID: " << SectionID + << " obj addr: " << format("%p", pData) + << " new addr: " << format("%p", Addr) + << " DataSize: " << DataSize + << " StubBufSize: " << StubBufSize + << " Allocate: " << Allocate + << "\n"); + Obj.updateSectionAddress(Section, (uint64_t)Addr); + } + else { + // Even if we didn't load the section, we need to record an entry for it + // to handle later processing (and by 'handle' I mean don't do anything + // with these sections). + Allocate = 0; + Addr = 0; + DEBUG(dbgs() << "emitSection SectionID: " << SectionID + << " obj addr: " << format("%p", data.data()) + << " new addr: 0" + << " DataSize: " << DataSize + << " StubBufSize: " << StubBufSize + << " Allocate: " << Allocate + << "\n"); + } + Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData)); return SectionID; } -unsigned RuntimeDyldImpl:: -findOrEmitSection(const SectionRef &Section, bool IsCode, - ObjSectionToIDMap &LocalSections) { +unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, + const SectionRef &Section, + bool IsCode, + ObjSectionToIDMap &LocalSections) { unsigned SectionID = 0; - ObjSectionToIDMap::iterator sIDIt = LocalSections.find(Section); - if (sIDIt != LocalSections.end()) - SectionID = sIDIt->second; + ObjSectionToIDMap::iterator i = LocalSections.find(Section); + if (i != LocalSections.end()) + SectionID = i->second; else { - SectionID = emitSection(Section, IsCode); + SectionID = emitSection(Obj, Section, IsCode); LocalSections[Section] = SectionID; } return SectionID; @@ -259,15 +364,18 @@ void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value) { - uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset; - DEBUG(dbgs() << "\tSectionID: " << RE.SectionID - << " + " << RE.Offset << " (" << format("%p", Target) << ")" - << " Data: " << RE.Data - << " Addend: " << RE.Addend - << "\n"); - - resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset, - Value, RE.Data, RE.Addend); + // Ignore relocations for sections that were not loaded + if (Sections[RE.SectionID].Address != 0) { + uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset; + DEBUG(dbgs() << "\tSectionID: " << RE.SectionID + << " + " << RE.Offset << " (" << format("%p", Target) << ")" + << " Data: " << RE.Data + << " Addend: " << RE.Addend + << "\n"); + + resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset, + Value, RE.Data, RE.Addend); + } } void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, @@ -280,11 +388,11 @@ void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, // resolveSymbols - Resolve any relocations to the specified symbols if // we know where it lives. void RuntimeDyldImpl::resolveSymbols() { - StringMap<RelocationList>::iterator it = SymbolRelocations.begin(), - itEnd = SymbolRelocations.end(); - for (; it != itEnd; it++) { - StringRef Name = it->first(); - RelocationList &Relocs = it->second; + StringMap<RelocationList>::iterator i = SymbolRelocations.begin(), + e = SymbolRelocations.end(); + for (; i != e; i++) { + StringRef Name = i->first(); + RelocationList &Relocs = i->second; StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name); if (Loc == SymbolTable.end()) { // This is an external symbol, try to get it address from diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 9351b6c..db6da8c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -20,11 +20,176 @@ #include "llvm/Object/ObjectFile.h" #include "llvm/Support/ELF.h" #include "llvm/ADT/Triple.h" +#include "llvm/Object/ELF.h" +#include "JITRegistrar.h" using namespace llvm; using namespace llvm::object; +namespace { + +template<support::endianness target_endianness, bool is64Bits> +class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> { + LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) + + typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; + typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; + typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; + typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; + + typedef typename ELFObjectFile<target_endianness, is64Bits>:: + Elf_Ehdr Elf_Ehdr; + + typedef typename ELFDataTypeTypedefHelper< + target_endianness, is64Bits>::value_type addr_type; + +protected: + // This duplicates the 'Data' member in the 'Binary' base class + // but it is necessary to workaround a bug in gcc 4.2 + MemoryBuffer *InputData; + +public: + DyldELFObject(MemoryBuffer *Object, error_code &ec); + + void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); + void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr); + + const MemoryBuffer& getBuffer() const { return *InputData; } + + // Methods for type inquiry through isa, cast, and dyn_cast + static inline bool classof(const Binary *v) { + return (isa<ELFObjectFile<target_endianness, is64Bits> >(v) + && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v))); + } + static inline bool classof( + const ELFObjectFile<target_endianness, is64Bits> *v) { + return v->isDyldType(); + } + static inline bool classof(const DyldELFObject *v) { + return true; + } +}; + +template<support::endianness target_endianness, bool is64Bits> +class ELFObjectImage : public ObjectImage { + protected: + DyldELFObject<target_endianness, is64Bits> *DyldObj; + bool Registered; + + public: + ELFObjectImage(DyldELFObject<target_endianness, is64Bits> *Obj) + : ObjectImage(Obj), + DyldObj(Obj), + Registered(false) {} + + virtual ~ELFObjectImage() { + if (Registered) + deregisterWithDebugger(); + } + + // Subclasses can override these methods to update the image with loaded + // addresses for sections and common symbols + virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) + { + DyldObj->updateSectionAddress(Sec, Addr); + } + + virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) + { + DyldObj->updateSymbolAddress(Sym, Addr); + } + + virtual void registerWithDebugger() + { + JITRegistrar::getGDBRegistrar().registerObject(DyldObj->getBuffer()); + Registered = true; + } + virtual void deregisterWithDebugger() + { + JITRegistrar::getGDBRegistrar().deregisterObject(DyldObj->getBuffer()); + } +}; + +template<support::endianness target_endianness, bool is64Bits> +DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object, + error_code &ec) + : ELFObjectFile<target_endianness, is64Bits>(Object, ec), + InputData(Object) { + this->isDyldELFObject = true; +} + +template<support::endianness target_endianness, bool is64Bits> +void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress( + const SectionRef &Sec, + uint64_t Addr) { + DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); + Elf_Shdr *shdr = const_cast<Elf_Shdr*>( + reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); + + // This assumes the address passed in matches the target address bitness + // The template-based type cast handles everything else. + shdr->sh_addr = static_cast<addr_type>(Addr); +} + +template<support::endianness target_endianness, bool is64Bits> +void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress( + const SymbolRef &SymRef, + uint64_t Addr) { + + Elf_Sym *sym = const_cast<Elf_Sym*>( + ELFObjectFile<target_endianness, is64Bits>:: + getSymbol(SymRef.getRawDataRefImpl())); + + // This assumes the address passed in matches the target address bitness + // The template-based type cast handles everything else. + sym->st_value = static_cast<addr_type>(Addr); +} + +} // namespace + + namespace llvm { +ObjectImage *RuntimeDyldELF::createObjectImage( + const MemoryBuffer *ConstInputBuffer) { + MemoryBuffer *InputBuffer = const_cast<MemoryBuffer*>(ConstInputBuffer); + std::pair<unsigned char, unsigned char> Ident = getElfArchType(InputBuffer); + error_code ec; + + if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { + DyldELFObject<support::little, false> *Obj = + new DyldELFObject<support::little, false>(InputBuffer, ec); + return new ELFObjectImage<support::little, false>(Obj); + } + else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { + DyldELFObject<support::big, false> *Obj = + new DyldELFObject<support::big, false>(InputBuffer, ec); + return new ELFObjectImage<support::big, false>(Obj); + } + else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { + DyldELFObject<support::big, true> *Obj = + new DyldELFObject<support::big, true>(InputBuffer, ec); + return new ELFObjectImage<support::big, true>(Obj); + } + else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { + DyldELFObject<support::little, true> *Obj = + new DyldELFObject<support::little, true>(InputBuffer, ec); + return new ELFObjectImage<support::little, true>(Obj); + } + else + llvm_unreachable("Unexpected ELF format"); +} + +void RuntimeDyldELF::handleObjectLoaded(ObjectImage *Obj) +{ + Obj->registerWithDebugger(); + // Save the loaded object. It will deregister itself when deleted + LoadedObject = Obj; +} + +RuntimeDyldELF::~RuntimeDyldELF() { + if (LoadedObject) + delete LoadedObject; +} void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress, uint64_t FinalAddress, @@ -71,7 +236,8 @@ void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress, switch (Type) { case ELF::R_386_32: { uint32_t *Target = (uint32_t*)(LocalAddress); - *Target = Value + Addend; + uint32_t Placeholder = *Target; + *Target = Placeholder + Value + Addend; break; } case ELF::R_386_PC32: { @@ -165,10 +331,11 @@ void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress, } } -void RuntimeDyldELF:: -processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, - ObjSectionToIDMap &ObjSectionToID, - LocalSymbolMap &Symbols, StubMap &Stubs) { +void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, + ObjectImage &Obj, + ObjSectionToIDMap &ObjSectionToID, + LocalSymbolMap &Symbols, + StubMap &Stubs) { uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL); intptr_t Addend = (intptr_t)Rel.AdditionalInfo; @@ -181,16 +348,16 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, << " TargetName: " << TargetName << "\n"); // First look the symbol in object file symbols. - LocalSymbolMap::iterator it = Symbols.find(TargetName.data()); - if (it != Symbols.end()) { - Value.SectionID = it->second.first; - Value.Addend = it->second.second; + LocalSymbolMap::iterator lsi = Symbols.find(TargetName.data()); + if (lsi != Symbols.end()) { + Value.SectionID = lsi->second.first; + Value.Addend = lsi->second.second; } else { // Second look the symbol in global symbol table. - StringMap<SymbolLoc>::iterator itS = SymbolTable.find(TargetName.data()); - if (itS != SymbolTable.end()) { - Value.SectionID = itS->second.first; - Value.Addend = itS->second.second; + StringMap<SymbolLoc>::iterator gsi = SymbolTable.find(TargetName.data()); + if (gsi != SymbolTable.end()) { + Value.SectionID = gsi->second.first; + Value.Addend = gsi->second.second; } else { SymbolRef::Type SymType; Symbol.getType(SymType); @@ -199,12 +366,12 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously // and can be changed by another developers. Maybe best way is add // a new symbol type ST_Section to SymbolRef and use it. - section_iterator sIt = Obj.end_sections(); - Symbol.getSection(sIt); - if (sIt == Obj.end_sections()) + section_iterator si = Obj.end_sections(); + Symbol.getSection(si); + if (si == Obj.end_sections()) llvm_unreachable("Symbol section not found, bad object file format!"); DEBUG(dbgs() << "\t\tThis is section symbol\n"); - Value.SectionID = findOrEmitSection((*sIt), true, ObjSectionToID); + Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID); Value.Addend = Addend; break; } @@ -232,10 +399,10 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, uint8_t *Target = Section.Address + Rel.Offset; // Look up for existing stub. - StubMap::const_iterator stubIt = Stubs.find(Value); - if (stubIt != Stubs.end()) { - resolveRelocation(Target, Section.LoadAddress, (uint64_t)Section.Address + - stubIt->second, RelType, 0); + StubMap::const_iterator i = Stubs.find(Value); + if (i != Stubs.end()) { + resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + + i->second, RelType, 0); DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. @@ -245,7 +412,7 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, Section.StubOffset); AddRelocation(Value, Rel.SectionID, StubTargetAddr - Section.Address, ELF::R_ARM_ABS32); - resolveRelocation(Target, Section.LoadAddress, (uint64_t)Section.Address + + resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + Section.StubOffset, RelType, 0); Section.StubOffset += getMaxStubSize(); } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h index 36566da..e7f6fab 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h @@ -22,6 +22,8 @@ using namespace llvm; namespace llvm { class RuntimeDyldELF : public RuntimeDyldImpl { protected: + ObjectImage *LoadedObject; + void resolveX86_64Relocation(uint8_t *LocalAddress, uint64_t FinalAddress, uint64_t Value, @@ -47,12 +49,18 @@ protected: int64_t Addend); virtual void processRelocationRef(const ObjRelocationInfo &Rel, - const ObjectFile &Obj, + ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, LocalSymbolMap &Symbols, StubMap &Stubs); + virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer); + virtual void handleObjectLoaded(ObjectImage *Obj); + public: - RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} + RuntimeDyldELF(RTDyldMemoryManager *mm) + : RuntimeDyldImpl(mm), LoadedObject(0) {} + + virtual ~RuntimeDyldELF(); bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const; }; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index d6430a9..2dea13f 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -29,6 +29,7 @@ #include "llvm/ADT/Triple.h" #include <map> #include "llvm/Support/Format.h" +#include "ObjectImage.h" using namespace llvm; using namespace llvm::object; @@ -110,6 +111,9 @@ protected: StringMap<SymbolLoc> SymbolTable; typedef DenseMap<const char*, SymbolLoc> LocalSymbolMap; + // Keep a map of common symbols to their sizes + typedef std::map<SymbolRef, unsigned> CommonSymbolMap; + // For each symbol, keep a list of relocations based on it. Anytime // its address is reassigned (the JIT re-compiled the function, e.g.), // the relocations get re-resolved. @@ -149,18 +153,29 @@ protected: return (uint8_t*)Sections[SectionID].Address; } + /// \brief Emits a section containing common symbols. + /// \return SectionID. + unsigned emitCommonSymbols(ObjectImage &Obj, + const CommonSymbolMap &Map, + uint64_t TotalSize, + LocalSymbolMap &Symbols); + /// \brief Emits section data from the object file to the MemoryManager. /// \param IsCode if it's true then allocateCodeSection() will be /// used for emmits, else allocateDataSection() will be used. /// \return SectionID. - unsigned emitSection(const SectionRef &Section, bool IsCode); + unsigned emitSection(ObjectImage &Obj, + const SectionRef &Section, + bool IsCode); /// \brief Find Section in LocalSections. If the secton is not found - emit /// it and store in LocalSections. /// \param IsCode if it's true then allocateCodeSection() will be /// used for emmits, else allocateDataSection() will be used. /// \return SectionID. - unsigned findOrEmitSection(const SectionRef &Section, bool IsCode, + unsigned findOrEmitSection(ObjectImage &Obj, + const SectionRef &Section, + bool IsCode, ObjSectionToIDMap &LocalSections); /// \brief If Value.SymbolName is NULL then store relocation to the @@ -191,11 +206,18 @@ protected: /// \brief Parses the object file relocation and store it to Relocations /// or SymbolRelocations. Its depend from object file type. virtual void processRelocationRef(const ObjRelocationInfo &Rel, - const ObjectFile &Obj, + ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, LocalSymbolMap &Symbols, StubMap &Stubs) = 0; void resolveSymbols(); + virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer); + virtual void handleObjectLoaded(ObjectImage *Obj) + { + // Subclasses may choose to retain this image if they have a use for it + delete Obj; + } + public: RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 24437e0..b7f515d 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -42,8 +42,7 @@ void RuntimeDyldMachO::resolveRelocation(uint8_t *LocalAddress, // This just dispatches to the proper target specific routine. switch (Arch) { default: llvm_unreachable("Unsupported CPU type!"); - case Triple::x86_64: // Fall through. - case Triple::x86: + case Triple::x86_64: resolveX86_64Relocation(LocalAddress, FinalAddress, (uintptr_t)Value, @@ -52,6 +51,15 @@ void RuntimeDyldMachO::resolveRelocation(uint8_t *LocalAddress, Size, Addend); break; + case Triple::x86: + resolveI386Relocation(LocalAddress, + FinalAddress, + (uintptr_t)Value, + isPCRel, + Type, + Size, + Addend); + break; case Triple::arm: // Fall through. case Triple::thumb: resolveARMRelocation(LocalAddress, @@ -66,6 +74,35 @@ void RuntimeDyldMachO::resolveRelocation(uint8_t *LocalAddress, } bool RuntimeDyldMachO:: +resolveI386Relocation(uint8_t *LocalAddress, + uint64_t FinalAddress, + uint64_t Value, + bool isPCRel, + unsigned Type, + unsigned Size, + int64_t Addend) { + if (isPCRel) + Value -= FinalAddress + 4; // see resolveX86_64Relocation + + switch (Type) { + default: + llvm_unreachable("Invalid relocation type!"); + case macho::RIT_Vanilla: { + uint8_t *p = LocalAddress; + uint64_t ValueToWrite = Value + Addend; + for (unsigned i = 0; i < Size; ++i) { + *p++ = (uint8_t)(ValueToWrite & 0xff); + ValueToWrite >>= 8; + } + } + case macho::RIT_Difference: + case macho::RIT_Generic_LocalDifference: + case macho::RIT_Generic_PreboundLazyPointer: + return Error("Relocation type not implemented yet!"); + } +} + +bool RuntimeDyldMachO:: resolveX86_64Relocation(uint8_t *LocalAddress, uint64_t FinalAddress, uint64_t Value, @@ -167,10 +204,11 @@ resolveARMRelocation(uint8_t *LocalAddress, return false; } -void RuntimeDyldMachO:: -processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, - ObjSectionToIDMap &ObjSectionToID, - LocalSymbolMap &Symbols, StubMap &Stubs) { +void RuntimeDyldMachO::processRelocationRef(const ObjRelocationInfo &Rel, + ObjectImage &Obj, + ObjSectionToIDMap &ObjSectionToID, + LocalSymbolMap &Symbols, + StubMap &Stubs) { uint32_t RelType = (uint32_t) (Rel.Type & 0xffffffffL); RelocationValueRef Value; @@ -183,32 +221,32 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, const SymbolRef &Symbol = Rel.Symbol; Symbol.getName(TargetName); // First look the symbol in object file symbols. - LocalSymbolMap::iterator it = Symbols.find(TargetName.data()); - if (it != Symbols.end()) { - Value.SectionID = it->second.first; - Value.Addend = it->second.second; + LocalSymbolMap::iterator lsi = Symbols.find(TargetName.data()); + if (lsi != Symbols.end()) { + Value.SectionID = lsi->second.first; + Value.Addend = lsi->second.second; } else { // Second look the symbol in global symbol table. - StringMap<SymbolLoc>::iterator itS = SymbolTable.find(TargetName.data()); - if (itS != SymbolTable.end()) { - Value.SectionID = itS->second.first; - Value.Addend = itS->second.second; + StringMap<SymbolLoc>::iterator gsi = SymbolTable.find(TargetName.data()); + if (gsi != SymbolTable.end()) { + Value.SectionID = gsi->second.first; + Value.Addend = gsi->second.second; } else Value.SymbolName = TargetName.data(); } } else { error_code err; - uint8_t sIdx = static_cast<uint8_t>(RelType & 0xFF); - section_iterator sIt = Obj.begin_sections(), - sItEnd = Obj.end_sections(); - for (uint8_t i = 1; i < sIdx; i++) { + uint8_t sectionIndex = static_cast<uint8_t>(RelType & 0xFF); + section_iterator si = Obj.begin_sections(), + se = Obj.end_sections(); + for (uint8_t i = 1; i < sectionIndex; i++) { error_code err; - sIt.increment(err); - if (sIt == sItEnd) + si.increment(err); + if (si == se) break; } - assert(sIt != sItEnd && "No section containing relocation!"); - Value.SectionID = findOrEmitSection(*sIt, true, ObjSectionToID); + assert(si != se && "No section containing relocation!"); + Value.SectionID = findOrEmitSection(Obj, *si, true, ObjSectionToID); Value.Addend = *(const intptr_t *)Target; if (Value.Addend) { // The MachO addend is offset from the current section, we need set it @@ -221,10 +259,10 @@ processRelocationRef(const ObjRelocationInfo &Rel, const ObjectFile &Obj, // This is an ARM branch relocation, need to use a stub function. // Look up for existing stub. - StubMap::const_iterator stubIt = Stubs.find(Value); - if (stubIt != Stubs.end()) + StubMap::const_iterator i = Stubs.find(Value); + if (i != Stubs.end()) resolveRelocation(Target, (uint64_t)Target, - (uint64_t)Section.Address + stubIt->second, + (uint64_t)Section.Address + i->second, RelType, 0); else { // Create a new stub function. diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h index 36b39dd..418d130 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -26,6 +26,13 @@ using namespace llvm::object; namespace llvm { class RuntimeDyldMachO : public RuntimeDyldImpl { protected: + bool resolveI386Relocation(uint8_t *LocalAddress, + uint64_t FinalAddress, + uint64_t Value, + bool isPCRel, + unsigned Type, + unsigned Size, + int64_t Addend); bool resolveX86_64Relocation(uint8_t *LocalAddress, uint64_t FinalAddress, uint64_t Value, @@ -42,7 +49,7 @@ protected: int64_t Addend); virtual void processRelocationRef(const ObjRelocationInfo &Rel, - const ObjectFile &Obj, + ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, LocalSymbolMap &Symbols, StubMap &Stubs); @@ -52,7 +59,7 @@ public: uint64_t Value, uint32_t Type, int64_t Addend); - + RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const; diff --git a/lib/ExecutionEngine/TargetSelect.cpp b/lib/ExecutionEngine/TargetSelect.cpp index 3937fe5..42364f9 100644 --- a/lib/ExecutionEngine/TargetSelect.cpp +++ b/lib/ExecutionEngine/TargetSelect.cpp @@ -15,6 +15,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ExecutionEngine/ExecutionEngine.h" +#include "llvm/Module.h" #include "llvm/ADT/Triple.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Target/TargetMachine.h" @@ -24,17 +25,21 @@ using namespace llvm; +TargetMachine *EngineBuilder::selectTarget() { + StringRef MArch = ""; + StringRef MCPU = ""; + SmallVector<std::string, 1> MAttrs; + Triple TT(M->getTargetTriple()); + + return selectTarget(TT, MArch, MCPU, MAttrs); +} + /// selectTarget - Pick a target either via -march or by guessing the native /// arch. Add any CPU features specified via -mcpu or -mattr. TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, StringRef MArch, StringRef MCPU, - const SmallVectorImpl<std::string>& MAttrs, - const TargetOptions &Options, - Reloc::Model RM, - CodeModel::Model CM, - CodeGenOpt::Level OL, - std::string *ErrorStr) { + const SmallVectorImpl<std::string>& MAttrs) { Triple TheTriple(TargetTriple); if (TheTriple.getTriple().empty()) TheTriple.setTriple(sys::getDefaultTargetTriple()); @@ -84,7 +89,8 @@ TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr, Options, - RM, CM, OL); + RelocModel, CMModel, + OptLevel); assert(Target && "Could not allocate target machine!"); return Target; } |