diff options
Diffstat (limited to 'lib/ExecutionEngine/RuntimeDyld')
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h | 388 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp | 76 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp | 308 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h | 178 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h | 93 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp | 522 | ||||
-rw-r--r-- | lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h | 94 |
8 files changed, 1351 insertions, 309 deletions
diff --git a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt index 59bdfee3..002e63c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt +++ b/lib/ExecutionEngine/RuntimeDyld/CMakeLists.txt @@ -1,4 +1,5 @@ add_llvm_library(LLVMRuntimeDyld RuntimeDyld.cpp RuntimeDyldMachO.cpp + RuntimeDyldELF.cpp ) diff --git a/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h b/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h new file mode 100644 index 0000000..2d777da --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h @@ -0,0 +1,388 @@ +//===-- 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/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 33dd705..2896c2d 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -1,4 +1,4 @@ -//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===// +//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -13,6 +13,10 @@ #define DEBUG_TYPE "dyld" #include "RuntimeDyldImpl.h" +#include "RuntimeDyldELF.h" +#include "RuntimeDyldMachO.h" +#include "llvm/Support/Path.h" + using namespace llvm; using namespace llvm::object; @@ -24,6 +28,7 @@ namespace llvm { void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, uint8_t *EndAddress) { + // FIXME: DEPRECATED in favor of by-section allocation. // Allocate memory for the function via the memory manager. uintptr_t Size = EndAddress - StartAddress + 1; uintptr_t AllocSize = Size; @@ -34,21 +39,30 @@ void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, memcpy(Mem, StartAddress, Size); MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); // Remember where we put it. - Functions[Name] = sys::MemoryBlock(Mem, Size); + unsigned SectionID = Sections.size(); + Sections.push_back(sys::MemoryBlock(Mem, Size)); + // Default the assigned address for this symbol to wherever this // allocated it. - SymbolTable[Name] = Mem; + SymbolTable[Name] = SymbolLoc(SectionID, 0); DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); } // Resolve the relocations for all symbols we currently know about. void RuntimeDyldImpl::resolveRelocations() { - // Just iterate over the symbols in our symbol table and assign their - // addresses. - StringMap<uint8_t*>::iterator i = SymbolTable.begin(); - StringMap<uint8_t*>::iterator e = SymbolTable.end(); - for (;i != e; ++i) - reassignSymbolAddress(i->getKey(), i->getValue()); + // Just iterate over the sections we have and resolve all the relocations + // in them. Gross overkill, but it gets the job done. + for (int i = 0, e = Sections.size(); i != e; ++i) { + reassignSectionAddress(i, SectionLoadAddress[i]); + } +} + +void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress, + uint64_t TargetAddress) { + assert(SectionLocalMemToID.count(LocalAddress) && + "Attempting to remap address of unknown section!"); + unsigned SectionID = SectionLocalMemToID[LocalAddress]; + reassignSectionAddress(SectionID, TargetAddress); } //===----------------------------------------------------------------------===// @@ -64,12 +78,36 @@ RuntimeDyld::~RuntimeDyld() { bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { if (!Dyld) { - if (RuntimeDyldMachO::isKnownFormat(InputBuffer)) - Dyld = new RuntimeDyldMachO(MM); - else - report_fatal_error("Unknown object format!"); + sys::LLVMFileType type = sys::IdentifyFileType( + InputBuffer->getBufferStart(), + static_cast<unsigned>(InputBuffer->getBufferSize())); + switch (type) { + case sys::ELF_Relocatable_FileType: + case sys::ELF_Executable_FileType: + case sys::ELF_SharedObject_FileType: + case sys::ELF_Core_FileType: + Dyld = new RuntimeDyldELF(MM); + break; + case sys::Mach_O_Object_FileType: + case sys::Mach_O_Executable_FileType: + case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: + case sys::Mach_O_Core_FileType: + case sys::Mach_O_PreloadExecutable_FileType: + case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: + case sys::Mach_O_DynamicLinker_FileType: + case sys::Mach_O_Bundle_FileType: + case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: + case sys::Mach_O_DSYMCompanion_FileType: + Dyld = new RuntimeDyldMachO(MM); + break; + case sys::Unknown_FileType: + case sys::Bitcode_FileType: + case sys::Archive_FileType: + case sys::COFF_FileType: + report_fatal_error("Incompatible object format!"); + } } else { - if(!Dyld->isCompatibleFormat(InputBuffer)) + if (!Dyld->isCompatibleFormat(InputBuffer)) report_fatal_error("Incompatible object format!"); } @@ -84,8 +122,14 @@ void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); } -void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { - Dyld->reassignSymbolAddress(Name, Addr); +void RuntimeDyld::reassignSectionAddress(unsigned SectionID, + uint64_t Addr) { + Dyld->reassignSectionAddress(SectionID, Addr); +} + +void RuntimeDyld::mapSectionAddress(void *LocalAddress, + uint64_t TargetAddress) { + Dyld->mapSectionAddress(LocalAddress, TargetAddress); } StringRef RuntimeDyld::getErrorString() { diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp new file mode 100644 index 0000000..e15b200 --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -0,0 +1,308 @@ +//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Implementation of ELF support for the MC-JIT runtime dynamic linker. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "dyld" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/IntervalMap.h" +#include "RuntimeDyldELF.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/ELF.h" +#include "llvm/ADT/Triple.h" +using namespace llvm; +using namespace llvm::object; + +namespace llvm { + +namespace { + +// FIXME: this function should probably not live here... +// +// Returns the name and address of an unrelocated symbol in an ELF section +void getSymbolInfo(symbol_iterator Sym, uint64_t &Addr, StringRef &Name) { + //FIXME: error checking here required to catch corrupt ELF objects... + error_code Err = Sym->getName(Name); + + uint64_t AddrInSection; + Err = Sym->getAddress(AddrInSection); + + SectionRef empty_section; + section_iterator Section(empty_section); + Err = Sym->getSection(Section); + + StringRef SectionContents; + Section->getContents(SectionContents); + + Addr = reinterpret_cast<uint64_t>(SectionContents.data()) + AddrInSection; +} + +} + +bool RuntimeDyldELF::loadObject(MemoryBuffer *InputBuffer) { + if (!isCompatibleFormat(InputBuffer)) + return true; + + OwningPtr<ObjectFile> Obj(ObjectFile::createELFObjectFile(InputBuffer)); + + Arch = Obj->getArch(); + + // Map address in the Object file image to function names + IntervalMap<uint64_t, StringRef>::Allocator A; + IntervalMap<uint64_t, StringRef> FuncMap(A); + + // This is a bit of a hack. The ObjectFile we've just loaded reports + // section addresses as 0 and doesn't provide access to the section + // offset (from which we could calculate the address. Instead, + // we're storing the address when it comes up in the ST_Debug case + // below. + // + StringMap<uint64_t> DebugSymbolMap; + + symbol_iterator SymEnd = Obj->end_symbols(); + error_code Err; + for (symbol_iterator Sym = Obj->begin_symbols(); + Sym != SymEnd; Sym.increment(Err)) { + SymbolRef::Type Type; + Sym->getType(Type); + if (Type == SymbolRef::ST_Function) { + StringRef Name; + uint64_t Addr; + getSymbolInfo(Sym, Addr, Name); + + uint64_t Size; + Err = Sym->getSize(Size); + + uint8_t *Start; + uint8_t *End; + Start = reinterpret_cast<uint8_t*>(Addr); + End = reinterpret_cast<uint8_t*>(Addr + Size - 1); + + extractFunction(Name, Start, End); + FuncMap.insert(Addr, Addr + Size - 1, Name); + } else if (Type == SymbolRef::ST_Debug) { + // This case helps us find section addresses + StringRef Name; + uint64_t Addr; + getSymbolInfo(Sym, Addr, Name); + DebugSymbolMap[Name] = Addr; + } + } + + // Iterate through the relocations for this object + section_iterator SecEnd = Obj->end_sections(); + for (section_iterator Sec = Obj->begin_sections(); + Sec != SecEnd; Sec.increment(Err)) { + StringRef SecName; + uint64_t SecAddr; + Sec->getName(SecName); + // Ignore sections that aren't in our map + if (DebugSymbolMap.find(SecName) == DebugSymbolMap.end()) { + continue; + } + SecAddr = DebugSymbolMap[SecName]; + relocation_iterator RelEnd = Sec->end_relocations(); + for (relocation_iterator Rel = Sec->begin_relocations(); + Rel != RelEnd; Rel.increment(Err)) { + uint64_t RelOffset; + uint64_t RelType; + int64_t RelAddend; + SymbolRef RelSym; + StringRef SymName; + uint64_t SymAddr; + uint64_t SymOffset; + + Rel->getAddress(RelOffset); + Rel->getType(RelType); + Rel->getAdditionalInfo(RelAddend); + Rel->getSymbol(RelSym); + RelSym.getName(SymName); + RelSym.getAddress(SymAddr); + RelSym.getFileOffset(SymOffset); + + // If this relocation is inside a function, we want to store the + // function name and a function-relative offset + IntervalMap<uint64_t, StringRef>::iterator ContainingFunc + = FuncMap.find(SecAddr + RelOffset); + if (ContainingFunc.valid()) { + // Re-base the relocation to make it relative to the target function + RelOffset = (SecAddr + RelOffset) - ContainingFunc.start(); + Relocations[SymName].push_back(RelocationEntry(ContainingFunc.value(), + RelOffset, + RelType, + RelAddend, + true)); + } else { + Relocations[SymName].push_back(RelocationEntry(SecName, + RelOffset, + RelType, + RelAddend, + false)); + } + } + } + return false; +} + +void RuntimeDyldELF::resolveRelocations() { + // FIXME: deprecated. should be changed to use the by-section + // allocation and relocation scheme. + + // Just iterate over the symbols in our symbol table and assign their + // addresses. + StringMap<SymbolLoc>::iterator i = SymbolTable.begin(); + StringMap<SymbolLoc>::iterator e = SymbolTable.end(); + for (;i != e; ++i) { + assert (i->getValue().second == 0 && "non-zero offset in by-function sym!"); + reassignSymbolAddress(i->getKey(), + (uint8_t*)Sections[i->getValue().first].base()); + } +} + +void RuntimeDyldELF::resolveX86_64Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE) { + uint8_t *TargetAddr; + if (RE.IsFunctionRelative) { + StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target); + assert(Loc != SymbolTable.end() && "Function for relocation not found"); + TargetAddr = + reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) + + Loc->second.second + RE.Offset; + } else { + // FIXME: Get the address of the target section and add that to RE.Offset + llvm_unreachable("Non-function relocation not implemented yet!"); + } + + switch (RE.Type) { + default: llvm_unreachable("Relocation type not implemented yet!"); + case ELF::R_X86_64_64: { + uint8_t **Target = reinterpret_cast<uint8_t**>(TargetAddr); + *Target = Addr + RE.Addend; + break; + } + case ELF::R_X86_64_32: + case ELF::R_X86_64_32S: { + uint64_t Value = reinterpret_cast<uint64_t>(Addr) + RE.Addend; + // FIXME: Handle the possibility of this assertion failing + assert((RE.Type == ELF::R_X86_64_32 && !(Value & 0xFFFFFFFF00000000ULL)) || + (RE.Type == ELF::R_X86_64_32S && + (Value & 0xFFFFFFFF00000000ULL) == 0xFFFFFFFF00000000ULL)); + uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); + uint32_t *Target = reinterpret_cast<uint32_t*>(TargetAddr); + *Target = TruncatedAddr; + break; + } + case ELF::R_X86_64_PC32: { + uint32_t *Placeholder = reinterpret_cast<uint32_t*>(TargetAddr); + uint64_t RealOffset = *Placeholder + + reinterpret_cast<uint64_t>(Addr) + + RE.Addend - reinterpret_cast<uint64_t>(TargetAddr); + assert((RealOffset & 0xFFFFFFFF) == RealOffset); + uint32_t TruncOffset = (RealOffset & 0xFFFFFFFF); + *Placeholder = TruncOffset; + break; + } + } +} + +void RuntimeDyldELF::resolveX86Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE) { + uint8_t *TargetAddr; + if (RE.IsFunctionRelative) { + StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(RE.Target); + assert(Loc != SymbolTable.end() && "Function for relocation not found"); + TargetAddr = + reinterpret_cast<uint8_t*>(Sections[Loc->second.first].base()) + + Loc->second.second + RE.Offset; + } else { + // FIXME: Get the address of the target section and add that to RE.Offset + llvm_unreachable("Non-function relocation not implemented yet!"); + } + + switch (RE.Type) { + case ELF::R_386_32: { + uint8_t **Target = reinterpret_cast<uint8_t**>(TargetAddr); + *Target = Addr + RE.Addend; + break; + } + case ELF::R_386_PC32: { + uint32_t *Placeholder = reinterpret_cast<uint32_t*>(TargetAddr); + uint32_t RealOffset = *Placeholder + reinterpret_cast<uintptr_t>(Addr) + + RE.Addend - reinterpret_cast<uintptr_t>(TargetAddr); + *Placeholder = RealOffset; + break; + } + default: + // There are other relocation types, but it appears these are the + // only ones currently used by the LLVM ELF object writer + llvm_unreachable("Relocation type not implemented yet!"); + } +} + +void RuntimeDyldELF::resolveArmRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE) { +} + +void RuntimeDyldELF::resolveRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE) { + switch (Arch) { + case Triple::x86_64: + resolveX86_64Relocation(Name, Addr, RE); + break; + case Triple::x86: + resolveX86Relocation(Name, Addr, RE); + break; + case Triple::arm: + resolveArmRelocation(Name, Addr, RE); + break; + default: llvm_unreachable("Unsupported CPU type!"); + } +} + +void RuntimeDyldELF::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { + // FIXME: deprecated. switch to reassignSectionAddress() instead. + // + // Actually moving the symbol address requires by-section mapping. + assert(Sections[SymbolTable.lookup(Name).first].base() == (void*)Addr && + "Unable to relocate section in by-function JIT allocation model!"); + + RelocationList &Relocs = Relocations[Name]; + for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { + RelocationEntry &RE = Relocs[i]; + resolveRelocation(Name, Addr, RE); + } +} + +// Assign an address to a symbol name and resolve all the relocations +// associated with it. +void RuntimeDyldELF::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { + // The address to use for relocation resolution is not + // the address of the local section buffer. We must be doing + // a remote execution environment of some sort. Re-apply any + // relocations referencing this section with the given address. + // + // Addr is a uint64_t because we can't assume the pointer width + // of the target is the same as that of the host. Just use a generic + // "big enough" type. + assert(0); +} + +bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const { + StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT); + return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; +} +} // namespace llvm diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h new file mode 100644 index 0000000..e0f7d54 --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h @@ -0,0 +1,178 @@ +//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// ELF support for MC-JIT runtime dynamic linker. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_RUNTIME_DYLD_ELF_H +#define LLVM_RUNTIME_DYLD_ELF_H + +#include "RuntimeDyldImpl.h" + +using namespace llvm; + + +namespace llvm { +class RuntimeDyldELF : public RuntimeDyldImpl { + // 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. + struct RelocationEntry { + // Function or section this relocation is contained in. + std::string Target; + // Offset into the target function or section for the relocation. + uint32_t Offset; + // Relocation type + uint32_t Type; + // Addend encoded in the instruction itself, if any. + int32_t Addend; + // Has the relocation been recalcuated as an offset within a function? + bool IsFunctionRelative; + // Has this relocation been resolved previously? + bool isResolved; + + RelocationEntry(StringRef t, + uint32_t offset, + uint32_t type, + int32_t addend, + bool isFunctionRelative) + : Target(t) + , Offset(offset) + , Type(type) + , Addend(addend) + , IsFunctionRelative(isFunctionRelative) + , isResolved(false) { } + }; + typedef SmallVector<RelocationEntry, 4> RelocationList; + StringMap<RelocationList> Relocations; + unsigned Arch; + + void resolveRelocations(); + + void resolveX86_64Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveX86Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveArmRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + +public: + RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} + + bool loadObject(MemoryBuffer *InputBuffer); + + void reassignSymbolAddress(StringRef Name, uint8_t *Addr); + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); + + bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const; +}; + +} // end namespace llvm + +#endif + +//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// ELF support for MC-JIT runtime dynamic linker. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_RUNTIME_DYLD_ELF_H +#define LLVM_RUNTIME_DYLD_ELF_H + +#include "RuntimeDyldImpl.h" + +using namespace llvm; + + +namespace llvm { +class RuntimeDyldELF : public RuntimeDyldImpl { + // 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. + struct RelocationEntry { + // Function or section this relocation is contained in. + std::string Target; + // Offset into the target function or section for the relocation. + uint32_t Offset; + // Relocation type + uint32_t Type; + // Addend encoded in the instruction itself, if any. + int32_t Addend; + // Has the relocation been recalcuated as an offset within a function? + bool IsFunctionRelative; + // Has this relocation been resolved previously? + bool isResolved; + + RelocationEntry(StringRef t, + uint32_t offset, + uint32_t type, + int32_t addend, + bool isFunctionRelative) + : Target(t) + , Offset(offset) + , Type(type) + , Addend(addend) + , IsFunctionRelative(isFunctionRelative) + , isResolved(false) { } + }; + typedef SmallVector<RelocationEntry, 4> RelocationList; + StringMap<RelocationList> Relocations; + unsigned Arch; + + void resolveRelocations(); + + void resolveX86_64Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveX86Relocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveArmRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + + void resolveRelocation(StringRef Name, + uint8_t *Addr, + const RelocationEntry &RE); + +public: + RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} + + bool loadObject(MemoryBuffer *InputBuffer); + + void reassignSymbolAddress(StringRef Name, uint8_t *Addr); + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); + + bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const; +}; + +} // end namespace llvm + +#endif + diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index 7190a3c..28e99be 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -1,4 +1,4 @@ -//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===// +//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -15,12 +15,11 @@ #define LLVM_RUNTIME_DYLD_IMPL_H #include "llvm/ExecutionEngine/RuntimeDyld.h" -#include "llvm/Object/MachOObject.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/Support/Format.h" #include "llvm/Support/Memory.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/system_error.h" @@ -29,7 +28,6 @@ #include "llvm/Support/ErrorHandling.h" using namespace llvm; -using namespace llvm::object; namespace llvm { class RuntimeDyldImpl { @@ -40,17 +38,22 @@ protected: // The MemoryManager to load objects into. RTDyldMemoryManager *MemMgr; - // FIXME: This all assumes we're dealing with external symbols for anything - // explicitly referenced. I.e., we can index by name and things - // will work out. In practice, this may not be the case, so we - // should find a way to effectively generalize. + // For each section, we have a MemoryBlock of it's data. + // Indexed by SectionID. + SmallVector<sys::MemoryBlock, 32> Sections; + // For each section, the address it will be considered to live at for + // relocations. The same as the pointer to the above memory block for hosted + // JITs. Indexed by SectionID. + SmallVector<uint64_t, 32> SectionLoadAddress; - // For each function, we have a MemoryBlock of it's instruction data. - StringMap<sys::MemoryBlock> Functions; + // Keep a map of starting local address to the SectionID which references it. + // Lookup function for when we assign virtual addresses. + DenseMap<void *, unsigned> SectionLocalMemToID; // Master symbol table. As modules are loaded and external symbols are - // resolved, their addresses are stored here. - StringMap<uint8_t*> SymbolTable; + // resolved, their addresses are stored here as a SectionID/Offset pair. + typedef std::pair<unsigned, uint64_t> SymbolLoc; + StringMap<SymbolLoc> SymbolTable; bool HasError; std::string ErrorStr; @@ -62,6 +65,9 @@ protected: return true; } + uint8_t *getSectionAddress(unsigned SectionID) { + return (uint8_t*)Sections[SectionID].base(); + } void extractFunction(StringRef Name, uint8_t *StartAddress, uint8_t *EndAddress); @@ -75,12 +81,17 @@ public: void *getSymbolAddress(StringRef Name) { // FIXME: Just look up as a function for now. Overly simple of course. // Work in progress. - return SymbolTable.lookup(Name); + if (SymbolTable.find(Name) == SymbolTable.end()) + return 0; + SymbolLoc Loc = SymbolTable.lookup(Name); + return getSectionAddress(Loc.first) + Loc.second; } - void resolveRelocations(); + virtual void resolveRelocations(); + + virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0; - virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0; + void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress); // Is the linker in an error state? bool hasError() { return HasError; } @@ -94,58 +105,6 @@ public: virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0; }; - -class RuntimeDyldMachO : public RuntimeDyldImpl { - - // 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. - struct RelocationEntry { - std::string Target; // Object this relocation is contained in. - uint64_t Offset; // Offset into the object for the relocation. - uint32_t Data; // Second word of the raw macho relocation entry. - int64_t Addend; // Addend encoded in the instruction itself, if any. - bool isResolved; // Has this relocation been resolved previously? - - RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend) - : Target(t), Offset(offset), Data(data), Addend(addend), - isResolved(false) {} - }; - typedef SmallVector<RelocationEntry, 4> RelocationList; - StringMap<RelocationList> Relocations; - - // FIXME: Also keep a map of all the relocations contained in an object. Use - // this to dynamically answer whether all of the relocations in it have - // been resolved or not. - - bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, - unsigned Type, unsigned Size); - bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size); - bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size); - - bool loadSegment32(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); - bool loadSegment64(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); - -public: - RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} - - bool loadObject(MemoryBuffer *InputBuffer); - - void reassignSymbolAddress(StringRef Name, uint8_t *Addr); - - static bool isKnownFormat(const MemoryBuffer *InputBuffer); - - bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { - return isKnownFormat(InputBuffer); - } -}; - } // end namespace llvm diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index f5a68c8..c11b2c3 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -1,4 +1,4 @@ -//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===// +//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=// // // The LLVM Compiler Infrastructure // @@ -15,32 +15,30 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/STLExtras.h" -#include "RuntimeDyldImpl.h" +#include "RuntimeDyldMachO.h" using namespace llvm; using namespace llvm::object; namespace llvm { bool RuntimeDyldMachO:: -resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, - unsigned Type, unsigned Size) { +resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend) { // This just dispatches to the proper target specific routine. switch (CPUType) { - default: assert(0 && "Unsupported CPU type!"); + default: llvm_unreachable("Unsupported CPU type!"); case mach::CTM_x86_64: return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value, - isPCRel, Type, Size); + isPCRel, Type, Size, Addend); case mach::CTM_ARM: return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value, - isPCRel, Type, Size); + isPCRel, Type, Size, Addend); } - llvm_unreachable(""); } bool RuntimeDyldMachO:: -resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, - bool isPCRel, unsigned Type, - unsigned Size) { +resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend) { // If the relocation is PC-relative, the value to be encoded is the // pointer difference. if (isPCRel) @@ -51,8 +49,13 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, switch(Type) { default: llvm_unreachable("Invalid relocation type!"); + case macho::RIT_X86_64_Signed1: + case macho::RIT_X86_64_Signed2: + case macho::RIT_X86_64_Signed4: + case macho::RIT_X86_64_Signed: case macho::RIT_X86_64_Unsigned: case macho::RIT_X86_64_Branch: { + Value += Addend; // Mask in the target value a byte at a time (we don't have an alignment // guarantee for the target address, so this is safest). uint8_t *p = (uint8_t*)Address; @@ -62,22 +65,17 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, } return false; } - case macho::RIT_X86_64_Signed: case macho::RIT_X86_64_GOTLoad: case macho::RIT_X86_64_GOT: case macho::RIT_X86_64_Subtractor: - case macho::RIT_X86_64_Signed1: - case macho::RIT_X86_64_Signed2: - case macho::RIT_X86_64_Signed4: case macho::RIT_X86_64_TLV: return Error("Relocation type not implemented yet!"); } - return false; } -bool RuntimeDyldMachO::resolveARMRelocation(uintptr_t Address, uintptr_t Value, - bool isPCRel, unsigned Type, - unsigned Size) { +bool RuntimeDyldMachO:: +resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend) { // If the relocation is PC-relative, the value to be encoded is the // pointer difference. if (isPCRel) { @@ -92,7 +90,6 @@ bool RuntimeDyldMachO::resolveARMRelocation(uintptr_t Address, uintptr_t Value, default: llvm_unreachable("Invalid relocation type!"); case macho::RIT_Vanilla: { - llvm_unreachable("Invalid relocation type!"); // Mask in the target value a byte at a time (we don't have an alignment // guarantee for the target address, so this is safest). uint8_t *p = (uint8_t*)Address; @@ -135,84 +132,64 @@ bool RuntimeDyldMachO:: loadSegment32(const MachOObject *Obj, const MachOObject::LoadCommandInfo *SegmentLCI, const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { + // FIXME: This should really be combined w/ loadSegment64. Templatized + // function on the 32/64 datatypes maybe? InMemoryStruct<macho::SegmentLoadCommand> SegmentLC; Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); if (!SegmentLC) return Error("unable to load segment load command"); + + SmallVector<unsigned, 16> SectionMap; for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { InMemoryStruct<macho::Section> Sect; Obj->ReadSection(*SegmentLCI, SectNum, Sect); if (!Sect) return Error("unable to load section: '" + Twine(SectNum) + "'"); - // FIXME: For the time being, we're only loading text segments. - if (Sect->Flags != 0x80000400) - continue; + // Allocate memory via the MM for the section. + uint8_t *Buffer; + uint32_t SectionID = Sections.size(); + if (Sect->Flags == 0x80000400) + Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID); + else + Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID); + + DEBUG(dbgs() << "Loading " + << ((Sect->Flags == 0x80000400) ? "text" : "data") + << " (ID #" << SectionID << ")" + << " '" << Sect->SegmentName << "," + << Sect->Name << "' of size " << Sect->Size + << " to address " << Buffer << ".\n"); + + // Copy the payload from the object file into the allocated buffer. + uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, + SegmentLC->FileSize).data(); + memcpy(Buffer, Base + Sect->Address, Sect->Size); - // Address and names of symbols in the section. - typedef std::pair<uint64_t, StringRef> SymbolEntry; - SmallVector<SymbolEntry, 64> Symbols; - // Index of all the names, in this section or not. Used when we're - // dealing with relocation entries. - SmallVector<StringRef, 64> SymbolNames; - for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { - InMemoryStruct<macho::SymbolTableEntry> STE; - Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); - if (!STE) - return Error("unable to read symbol: '" + Twine(i) + "'"); - if (STE->SectionIndex > SegmentLC->NumSections) - return Error("invalid section index for symbol: '" + Twine(i) + "'"); - // Get the symbol name. - StringRef Name = Obj->getStringAtIndex(STE->StringIndex); - SymbolNames.push_back(Name); - - // Just skip symbols not defined in this section. - if ((unsigned)STE->SectionIndex - 1 != SectNum) - continue; - - // FIXME: Check the symbol type and flags. - if (STE->Type != 0xF) // external, defined in this section. - continue; - // Flags == 0x8 marks a thumb function for ARM, which is fine as it - // doesn't require any special handling here. - // Flags in the upper nibble we don't care about. - if ((STE->Flags & 0xf) != 0x0 && STE->Flags != 0x8) - continue; - - // Remember the symbol. - Symbols.push_back(SymbolEntry(STE->Value, Name)); - - DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << - (Sect->Address + STE->Value) << "\n"); - } - // Sort the symbols by address, just in case they didn't come in that way. - array_pod_sort(Symbols.begin(), Symbols.end()); + // Remember what got allocated for this SectionID. + Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); + SectionLocalMemToID[Buffer] = SectionID; - // If there weren't any functions (odd, but just in case...) - if (!Symbols.size()) - continue; + // By default, the load address of a section is its memory buffer. + SectionLoadAddress.push_back((uint64_t)Buffer); - // Extract the function data. - uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, - SegmentLC->FileSize).data(); - for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { - uint64_t StartOffset = Sect->Address + Symbols[i].first; - uint64_t EndOffset = Symbols[i + 1].first - 1; - DEBUG(dbgs() << "Extracting function: " << Symbols[i].second - << " from [" << StartOffset << ", " << EndOffset << "]\n"); - extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); - } - // The last symbol we do after since the end address is calculated - // differently because there is no next symbol to reference. - uint64_t StartOffset = Symbols[Symbols.size() - 1].first; - uint64_t EndOffset = Sect->Size - 1; - DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second - << " from [" << StartOffset << ", " << EndOffset << "]\n"); - extractFunction(Symbols[Symbols.size()-1].second, - Base + StartOffset, Base + EndOffset); - - // Now extract the relocation information for each function and process it. + // Keep a map of object file section numbers to corresponding SectionIDs + // while processing the file. + SectionMap.push_back(SectionID); + } + + // Process the symbol table. + SmallVector<StringRef, 64> SymbolNames; + processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC); + + // Process the relocations for each section we're loading. + Relocations.grow(Relocations.size() + SegmentLC->NumSections); + for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { + InMemoryStruct<macho::Section> Sect; + Obj->ReadSection(*SegmentLCI, SectNum, Sect); + if (!Sect) + return Error("unable to load section: '" + Twine(SectNum) + "'"); for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { InMemoryStruct<macho::RelocationEntry> RE; Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); @@ -222,51 +199,53 @@ loadSegment32(const MachOObject *Obj, // relocation should be applied. We need to translate that into an // offset into a function since that's our atom. uint32_t Offset = RE->Word0; - // Look for the function containing the address. This is used for JIT - // code, so the number of functions in section is almost always going - // to be very small (usually just one), so until we have use cases - // where that's not true, just use a trivial linear search. - unsigned SymbolNum; - unsigned NumSymbols = Symbols.size(); - assert(NumSymbols > 0 && Symbols[0].first <= Offset && - "No symbol containing relocation!"); - for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) - if (Symbols[SymbolNum + 1].first > Offset) - break; - // Adjust the offset to be relative to the symbol. - Offset -= Symbols[SymbolNum].first; - // Get the name of the symbol containing the relocation. - StringRef TargetName = SymbolNames[SymbolNum]; - bool isExtern = (RE->Word1 >> 27) & 1; + + // FIXME: Get the relocation addend from the target address. + // FIXME: VERY imporant for internal relocations. + // Figure out the source symbol of the relocation. If isExtern is true, // this relocation references the symbol table, otherwise it references // a section in the same object, numbered from 1 through NumSections // (SectionBases is [0, NumSections-1]). - // FIXME: Some targets (ARM) use internal relocations even for - // externally visible symbols, if the definition is in the same - // file as the reference. We need to convert those back to by-name - // references. We can resolve the address based on the section - // offset and see if we have a symbol at that address. If we do, - // use that; otherwise, puke. - if (!isExtern) - return Error("Internal relocations not supported."); uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value - StringRef SourceName = SymbolNames[SourceNum]; - - // FIXME: Get the relocation addend from the target address. - - // Now store the relocation information. Associate it with the source - // symbol. - Relocations[SourceName].push_back(RelocationEntry(TargetName, - Offset, - RE->Word1, - 0 /*Addend*/)); - DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset - << " from '" << SourceName << "(Word1: " - << format("0x%x", RE->Word1) << ")\n"); + if (!isExtern) { + assert(SourceNum > 0 && "Invalid relocation section number!"); + unsigned SectionID = SectionMap[SourceNum - 1]; + unsigned TargetID = SectionMap[SectNum]; + DEBUG(dbgs() << "Internal relocation at Section #" + << TargetID << " + " << Offset + << " from Section #" + << SectionID << " (Word1: " + << format("0x%x", RE->Word1) << ")\n"); + + // Store the relocation information. It will get resolved when + // the section addresses are assigned. + Relocations[SectionID].push_back(RelocationEntry(TargetID, + Offset, + RE->Word1, + 0 /*Addend*/)); + } else { + StringRef SourceName = SymbolNames[SourceNum]; + + // Now store the relocation information. Associate it with the source + // symbol. Just add it to the unresolved list and let the general + // path post-load resolve it if we know where the symbol is. + UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, + Offset, + RE->Word1, + 0 /*Addend*/)); + DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset + << " from '" << SourceName << "(Word1: " + << format("0x%x", RE->Word1) << ")\n"); + } } } + + // Resolve the addresses of any symbols that were defined in this segment. + for (int i = 0, e = SymbolNames.size(); i != e; ++i) + resolveSymbol(SymbolNames[i]); + return false; } @@ -280,77 +259,59 @@ loadSegment64(const MachOObject *Obj, if (!Segment64LC) return Error("unable to load segment load command"); + + SmallVector<unsigned, 16> SectionMap; for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { InMemoryStruct<macho::Section64> Sect; Obj->ReadSection64(*SegmentLCI, SectNum, Sect); if (!Sect) return Error("unable to load section: '" + Twine(SectNum) + "'"); - // FIXME: For the time being, we're only loading text segments. - if (Sect->Flags != 0x80000400) - continue; + // Allocate memory via the MM for the section. + uint8_t *Buffer; + uint32_t SectionID = Sections.size(); + unsigned Align = 1 << Sect->Align; // .o file has log2 alignment. + if (Sect->Flags == 0x80000400) + Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID); + else + Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID); + + DEBUG(dbgs() << "Loading " + << ((Sect->Flags == 0x80000400) ? "text" : "data") + << " (ID #" << SectionID << ")" + << " '" << Sect->SegmentName << "," + << Sect->Name << "' of size " << Sect->Size + << " (align " << Align << ")" + << " to address " << Buffer << ".\n"); + + // Copy the payload from the object file into the allocated buffer. + uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, + Segment64LC->FileSize).data(); + memcpy(Buffer, Base + Sect->Address, Sect->Size); - // Address and names of symbols in the section. - typedef std::pair<uint64_t, StringRef> SymbolEntry; - SmallVector<SymbolEntry, 64> Symbols; - // Index of all the names, in this section or not. Used when we're - // dealing with relocation entries. - SmallVector<StringRef, 64> SymbolNames; - for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { - InMemoryStruct<macho::Symbol64TableEntry> STE; - Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); - if (!STE) - return Error("unable to read symbol: '" + Twine(i) + "'"); - if (STE->SectionIndex > Segment64LC->NumSections) - return Error("invalid section index for symbol: '" + Twine(i) + "'"); - // Get the symbol name. - StringRef Name = Obj->getStringAtIndex(STE->StringIndex); - SymbolNames.push_back(Name); - - // Just skip symbols not defined in this section. - if ((unsigned)STE->SectionIndex - 1 != SectNum) - continue; - - // FIXME: Check the symbol type and flags. - if (STE->Type != 0xF) // external, defined in this section. - continue; - // Flags in the upper nibble we don't care about. - if ((STE->Flags & 0xf) != 0x0) - continue; - - // Remember the symbol. - Symbols.push_back(SymbolEntry(STE->Value, Name)); - - DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << - (Sect->Address + STE->Value) << "\n"); - } - // Sort the symbols by address, just in case they didn't come in that way. - array_pod_sort(Symbols.begin(), Symbols.end()); + // Remember what got allocated for this SectionID. + Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); + SectionLocalMemToID[Buffer] = SectionID; - // If there weren't any functions (odd, but just in case...) - if (!Symbols.size()) - continue; + // By default, the load address of a section is its memory buffer. + SectionLoadAddress.push_back((uint64_t)Buffer); - // Extract the function data. - uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, - Segment64LC->FileSize).data(); - for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { - uint64_t StartOffset = Sect->Address + Symbols[i].first; - uint64_t EndOffset = Symbols[i + 1].first - 1; - DEBUG(dbgs() << "Extracting function: " << Symbols[i].second - << " from [" << StartOffset << ", " << EndOffset << "]\n"); - extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); - } - // The last symbol we do after since the end address is calculated - // differently because there is no next symbol to reference. - uint64_t StartOffset = Symbols[Symbols.size() - 1].first; - uint64_t EndOffset = Sect->Size - 1; - DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second - << " from [" << StartOffset << ", " << EndOffset << "]\n"); - extractFunction(Symbols[Symbols.size()-1].second, - Base + StartOffset, Base + EndOffset); - - // Now extract the relocation information for each function and process it. + // Keep a map of object file section numbers to corresponding SectionIDs + // while processing the file. + SectionMap.push_back(SectionID); + } + + // Process the symbol table. + SmallVector<StringRef, 64> SymbolNames; + processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC); + + // Process the relocations for each section we're loading. + Relocations.grow(Relocations.size() + Segment64LC->NumSections); + for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { + InMemoryStruct<macho::Section64> Sect; + Obj->ReadSection64(*SegmentLCI, SectNum, Sect); + if (!Sect) + return Error("unable to load section: '" + Twine(SectNum) + "'"); for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { InMemoryStruct<macho::RelocationEntry> RE; Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); @@ -360,48 +321,142 @@ loadSegment64(const MachOObject *Obj, // relocation should be applied. We need to translate that into an // offset into a function since that's our atom. uint32_t Offset = RE->Word0; - // Look for the function containing the address. This is used for JIT - // code, so the number of functions in section is almost always going - // to be very small (usually just one), so until we have use cases - // where that's not true, just use a trivial linear search. - unsigned SymbolNum; - unsigned NumSymbols = Symbols.size(); - assert(NumSymbols > 0 && Symbols[0].first <= Offset && - "No symbol containing relocation!"); - for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) - if (Symbols[SymbolNum + 1].first > Offset) - break; - // Adjust the offset to be relative to the symbol. - Offset -= Symbols[SymbolNum].first; - // Get the name of the symbol containing the relocation. - StringRef TargetName = SymbolNames[SymbolNum]; - bool isExtern = (RE->Word1 >> 27) & 1; + + // FIXME: Get the relocation addend from the target address. + // FIXME: VERY imporant for internal relocations. + // Figure out the source symbol of the relocation. If isExtern is true, // this relocation references the symbol table, otherwise it references // a section in the same object, numbered from 1 through NumSections // (SectionBases is [0, NumSections-1]). - if (!isExtern) - return Error("Internal relocations not supported."); uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value - StringRef SourceName = SymbolNames[SourceNum]; + if (!isExtern) { + assert(SourceNum > 0 && "Invalid relocation section number!"); + unsigned SectionID = SectionMap[SourceNum - 1]; + unsigned TargetID = SectionMap[SectNum]; + DEBUG(dbgs() << "Internal relocation at Section #" + << TargetID << " + " << Offset + << " from Section #" + << SectionID << " (Word1: " + << format("0x%x", RE->Word1) << ")\n"); + + // Store the relocation information. It will get resolved when + // the section addresses are assigned. + Relocations[SectionID].push_back(RelocationEntry(TargetID, + Offset, + RE->Word1, + 0 /*Addend*/)); + } else { + StringRef SourceName = SymbolNames[SourceNum]; + + // Now store the relocation information. Associate it with the source + // symbol. Just add it to the unresolved list and let the general + // path post-load resolve it if we know where the symbol is. + UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum, + Offset, + RE->Word1, + 0 /*Addend*/)); + DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset + << " from '" << SourceName << "(Word1: " + << format("0x%x", RE->Word1) << ")\n"); + } + } + } - // FIXME: Get the relocation addend from the target address. + // Resolve the addresses of any symbols that were defined in this segment. + for (int i = 0, e = SymbolNames.size(); i != e; ++i) + resolveSymbol(SymbolNames[i]); - // Now store the relocation information. Associate it with the source - // symbol. - Relocations[SourceName].push_back(RelocationEntry(TargetName, - Offset, - RE->Word1, - 0 /*Addend*/)); - DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset - << " from '" << SourceName << "(Word1: " - << format("0x%x", RE->Word1) << ")\n"); - } + return false; +} + +bool RuntimeDyldMachO:: +processSymbols32(const MachOObject *Obj, + SmallVectorImpl<unsigned> &SectionMap, + SmallVectorImpl<StringRef> &SymbolNames, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { + // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such. + for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { + InMemoryStruct<macho::SymbolTableEntry> STE; + Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); + if (!STE) + return Error("unable to read symbol: '" + Twine(i) + "'"); + // Get the symbol name. + StringRef Name = Obj->getStringAtIndex(STE->StringIndex); + SymbolNames.push_back(Name); + + // FIXME: Check the symbol type and flags. + if (STE->Type != 0xF) // external, defined in this segment. + continue; + // Flags in the upper nibble we don't care about. + if ((STE->Flags & 0xf) != 0x0) + continue; + + // Remember the symbol. + uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; + SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); + + DEBUG(dbgs() << "Symbol: '" << Name << "' @ " + << (getSectionAddress(SectionID) + STE->Value) + << "\n"); } return false; } +bool RuntimeDyldMachO:: +processSymbols64(const MachOObject *Obj, + SmallVectorImpl<unsigned> &SectionMap, + SmallVectorImpl<StringRef> &SymbolNames, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { + for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { + InMemoryStruct<macho::Symbol64TableEntry> STE; + Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); + if (!STE) + return Error("unable to read symbol: '" + Twine(i) + "'"); + // Get the symbol name. + StringRef Name = Obj->getStringAtIndex(STE->StringIndex); + SymbolNames.push_back(Name); + + // FIXME: Check the symbol type and flags. + if (STE->Type != 0xF) // external, defined in this segment. + continue; + // Flags in the upper nibble we don't care about. + if ((STE->Flags & 0xf) != 0x0) + continue; + + // Remember the symbol. + uint32_t SectionID = SectionMap[STE->SectionIndex - 1]; + SymbolTable[Name] = SymbolLoc(SectionID, STE->Value); + + DEBUG(dbgs() << "Symbol: '" << Name << "' @ " + << (getSectionAddress(SectionID) + STE->Value) + << "\n"); + } + return false; +} + +// resolveSymbol - Resolve any relocations to the specified symbol if +// we know where it lives. +void RuntimeDyldMachO::resolveSymbol(StringRef Name) { + StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name); + if (Loc == SymbolTable.end()) + return; + + RelocationList &Relocs = UnresolvedRelocations[Name]; + DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n"); + for (int i = 0, e = Relocs.size(); i != e; ++i) { + // Change the relocation to be section relative rather than symbol + // relative and move it to the resolved relocation list. + RelocationEntry Entry = Relocs[i]; + Entry.Addend += Loc->second.second; + Relocations[Loc->second.first].push_back(Entry); + } + // FIXME: Keep a worklist of the relocations we've added so that we can + // resolve more selectively later. + Relocs.clear(); +} + bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) { // If the linker is in an error state, don't do anything. if (hasError()) @@ -451,7 +506,7 @@ bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) { if (!SymtabLCI) return Error("no symbol table found in object"); if (!SegmentLCI) - return Error("no symbol table found in object"); + return Error("no segments found in object"); // Read and register the symbol table data. InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; @@ -486,31 +541,46 @@ bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) { return true; } + // Assign the addresses of the sections from the object so that any + // relocations to them get set properly. + // FIXME: This is done directly from the client at the moment. We should + // default the values to the local storage, at least when the target arch + // is the same as the host arch. + return false; } // Assign an address to a symbol name and resolve all the relocations // associated with it. -void RuntimeDyldMachO::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { - // Assign the address in our symbol table. - SymbolTable[Name] = Addr; - - RelocationList &Relocs = Relocations[Name]; +void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID, + uint64_t Addr) { + // The address to use for relocation resolution is not + // the address of the local section buffer. We must be doing + // a remote execution environment of some sort. Re-apply any + // relocations referencing this section with the given address. + // + // Addr is a uint64_t because we can't assume the pointer width + // of the target is the same as that of the host. Just use a generic + // "big enough" type. + + SectionLoadAddress[SectionID] = Addr; + + RelocationList &Relocs = Relocations[SectionID]; for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { RelocationEntry &RE = Relocs[i]; - uint8_t *Target = SymbolTable[RE.Target] + RE.Offset; + uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset; bool isPCRel = (RE.Data >> 24) & 1; unsigned Type = (RE.Data >> 28) & 0xf; unsigned Size = 1 << ((RE.Data >> 25) & 3); - DEBUG(dbgs() << "Resolving relocation at '" << RE.Target - << "' + " << RE.Offset << " (" << format("%p", Target) << ")" - << " from '" << Name << " (" << format("%p", Addr) << ")" + DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID + << " + " << RE.Offset << " (" << format("%p", Target) << ")" + << " from Section #" << SectionID << " (" << format("%p", Addr) << ")" << "(" << (isPCRel ? "pcrel" : "absolute") - << ", type: " << Type << ", Size: " << Size << ").\n"); + << ", type: " << Type << ", Size: " << Size << ", Addend: " + << RE.Addend << ").\n"); - resolveRelocation(Target, Addr, isPCRel, Type, Size); - RE.isResolved = true; + resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend); } } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h new file mode 100644 index 0000000..5798981 --- /dev/null +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -0,0 +1,94 @@ +//===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// MachO support for MC-JIT runtime dynamic linker. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_RUNTIME_DYLD_MACHO_H +#define LLVM_RUNTIME_DYLD_MACHO_H + +#include "llvm/ADT/IndexedMap.h" +#include "llvm/Object/MachOObject.h" +#include "llvm/Support/Format.h" +#include "RuntimeDyldImpl.h" + +using namespace llvm; +using namespace llvm::object; + + +namespace llvm { +class RuntimeDyldMachO : public RuntimeDyldImpl { + + // 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. + // The symbol (or section) the relocation is sourced from is the Key + // in the relocation list where it's stored. + struct RelocationEntry { + unsigned SectionID; // Section the relocation is contained in. + uint64_t Offset; // Offset into the section for the relocation. + uint32_t Data; // Second word of the raw macho relocation entry. + int64_t Addend; // Addend encoded in the instruction itself, if any, + // plus the offset into the source section for + // the symbol once the relocation is resolvable. + + RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) + : SectionID(id), Offset(offset), Data(data), Addend(addend) {} + }; + typedef SmallVector<RelocationEntry, 4> RelocationList; + // Relocations to sections already loaded. Indexed by SectionID which is the + // source of the address. The target where the address will be writen is + // SectionID/Offset in the relocation itself. + IndexedMap<RelocationList> Relocations; + // Relocations to symbols that are not yet resolved. Must be external + // relocations by definition. Indexed by symbol name. + StringMap<RelocationList> UnresolvedRelocations; + + bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend); + bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend); + bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, + unsigned Type, unsigned Size, int64_t Addend); + + bool loadSegment32(const MachOObject *Obj, + const MachOObject::LoadCommandInfo *SegmentLCI, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); + bool loadSegment64(const MachOObject *Obj, + const MachOObject::LoadCommandInfo *SegmentLCI, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); + bool processSymbols32(const MachOObject *Obj, + SmallVectorImpl<unsigned> &SectionMap, + SmallVectorImpl<StringRef> &SymbolNames, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); + bool processSymbols64(const MachOObject *Obj, + SmallVectorImpl<unsigned> &SectionMap, + SmallVectorImpl<StringRef> &SymbolNames, + const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); + + void resolveSymbol(StringRef Name); + +public: + RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} + + bool loadObject(MemoryBuffer *InputBuffer); + + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); + + static bool isKnownFormat(const MemoryBuffer *InputBuffer); + + bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { + return isKnownFormat(InputBuffer); + } +}; + +} // end namespace llvm + +#endif |