diff --git a/include/llvm/ExecutionEngine/JITMemoryManager.h b/include/llvm/ExecutionEngine/JITMemoryManager.h index a63f0da773a..8eb7590a21a 100644 --- a/include/llvm/ExecutionEngine/JITMemoryManager.h +++ b/include/llvm/ExecutionEngine/JITMemoryManager.h @@ -101,6 +101,22 @@ public: virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart, uint8_t *FunctionEnd) = 0; + /// allocateCodeSection - Allocate a memory block of (at least) the given + /// size suitable for executable code. The SectionID is a unique identifier + /// assigned by the JIT and passed through to the memory manager for + /// the instance class to use if it needs to communicate to the JIT about + /// a given section after the fact. + virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) = 0; + + /// allocateDataSection - Allocate a memory block of (at least) the given + /// size suitable for data. The SectionID is a unique identifier + /// assigned by the JIT and passed through to the memory manager for + /// the instance class to use if it needs to communicate to the JIT about + /// a given section after the fact. + virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) = 0; + /// allocateSpace - Allocate a memory block of the given size. This method /// cannot be called between calls to startFunctionBody and endFunctionBody. virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0; diff --git a/include/llvm/ExecutionEngine/RuntimeDyld.h b/include/llvm/ExecutionEngine/RuntimeDyld.h index 724b9f09e0e..d937ab49f4e 100644 --- a/include/llvm/ExecutionEngine/RuntimeDyld.h +++ b/include/llvm/ExecutionEngine/RuntimeDyld.h @@ -35,6 +35,16 @@ public: RTDyldMemoryManager() {} virtual ~RTDyldMemoryManager(); + /// allocateCodeSection - Allocate a memory block of (at least) the given + /// size suitable for executable code. + virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) = 0; + + /// allocateDataSection - Allocate a memory block of (at least) the given + /// size suitable for data. + virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) = 0; + // Allocate ActualSize bytes, or more, for the named function. Return // a pointer to the allocated memory and update Size to reflect how much // memory was acutally allocated. @@ -65,9 +75,9 @@ public: void *getSymbolAddress(StringRef Name); // Resolve the relocations for all symbols we currently know about. void resolveRelocations(); - // Change the address associated with a symbol when resolving relocations. + // Change the address associated with a section when resolving relocations. // Any relocations already associated with the symbol will be re-resolved. - void reassignSymbolAddress(StringRef Name, uint8_t *Addr); + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); StringRef getErrorString(); }; diff --git a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp index eec23cec0af..efd570d7c56 100644 --- a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp +++ b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp @@ -441,6 +441,50 @@ namespace { return (uint8_t*)DataAllocator.Allocate(Size, Alignment); } + /// allocateCodeSection - Allocate memory for a code section. + uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + // FIXME: Alignement handling. + FreeRangeHeader* candidateBlock = FreeMemoryList; + FreeRangeHeader* head = FreeMemoryList; + FreeRangeHeader* iter = head->Next; + + uintptr_t largest = candidateBlock->BlockSize; + + // Search for the largest free block. + while (iter != head) { + if (iter->BlockSize > largest) { + largest = iter->BlockSize; + candidateBlock = iter; + } + iter = iter->Next; + } + + largest = largest - sizeof(MemoryRangeHeader); + + // If this block isn't big enough for the allocation desired, allocate + // another block of memory and add it to the free list. + if (largest < Size || largest <= FreeRangeHeader::getMinBlockSize()) { + DEBUG(dbgs() << "JIT: Allocating another slab of memory for function."); + candidateBlock = allocateNewCodeSlab((size_t)Size); + } + + // Select this candidate block for allocation + CurBlock = candidateBlock; + + // Allocate the entire memory block. + FreeMemoryList = candidateBlock->AllocateBlock(); + // Release the memory at the end of this block that isn't needed. + FreeMemoryList = CurBlock->TrimAllocationToSize(FreeMemoryList, Size); + return (uint8_t *)(CurBlock + 1); + } + + /// allocateDataSection - Allocate memory for a data section. + uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + return (uint8_t*)DataAllocator.Allocate(Size, Alignment); + } + /// startExceptionTable - Use startFunctionBody to allocate memory for the /// function's exception table. uint8_t* startExceptionTable(const Function* F, uintptr_t &ActualSize) { diff --git a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h index bc7080d2d30..ac8c15579ef 100644 --- a/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h +++ b/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h @@ -31,6 +31,16 @@ public: // We own the JMM, so make sure to delete it. ~MCJITMemoryManager() { delete JMM; } + uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + return JMM->allocateDataSection(Size, Alignment, SectionID); + } + + uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + return JMM->allocateCodeSection(Size, Alignment, SectionID); + } + // Allocate ActualSize bytes, or more, for the named function. Return // a pointer to the allocated memory and update Size to reflect how much // memory was acutally allocated. diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index b017ebb2dcb..18827978d9a 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -25,6 +25,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; @@ -35,21 +36,22 @@ 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::iterator i = SymbolTable.begin(); - StringMap::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]); + } } //===----------------------------------------------------------------------===// @@ -109,8 +111,9 @@ 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); } StringRef RuntimeDyld::getErrorString() { diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 54cb350552d..5c45cf3f809 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -154,17 +154,31 @@ bool RuntimeDyldELF::loadObject(MemoryBuffer *InputBuffer) { 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::iterator i = SymbolTable.begin(); + StringMap::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::iterator ContainingFunc - = Functions.find(RE.Target); - assert(ContainingFunc != Functions.end() - && "Function for relocation not found"); - TargetAddr = reinterpret_cast(ContainingFunc->getValue().base()) + - RE.Offset; + StringMap::const_iterator Loc = SymbolTable.find(RE.Target); + assert(Loc != SymbolTable.end() && "Function for relocation not found"); + TargetAddr = + reinterpret_cast(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 assert(0 && ("Non-function relocation not implemented yet!")); @@ -209,12 +223,11 @@ void RuntimeDyldELF::resolveX86Relocation(StringRef Name, const RelocationEntry &RE) { uint8_t *TargetAddr; if (RE.IsFunctionRelative) { - StringMap::iterator ContainingFunc - = Functions.find(RE.Target); - assert(ContainingFunc != Functions.end() - && "Function for relocation not found"); - TargetAddr = reinterpret_cast( - ContainingFunc->getValue().base()) + RE.Offset; + StringMap::const_iterator Loc = SymbolTable.find(RE.Target); + assert(Loc != SymbolTable.end() && "Function for relocation not found"); + TargetAddr = + reinterpret_cast(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 assert(0 && ("Non-function relocation not implemented yet!")); @@ -266,7 +279,11 @@ void RuntimeDyldELF::resolveRelocation(StringRef Name, } void RuntimeDyldELF::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { - SymbolTable[Name] = 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) { @@ -275,6 +292,20 @@ void RuntimeDyldELF::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { } } +// 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; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index cff7cbdf28a..bbfef768c4c 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -16,6 +16,7 @@ #include "llvm/ExecutionEngine/RuntimeDyld.h" #include "llvm/Object/MachOObject.h" +#include "llvm/ADT/IndexedMap.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/ADT/SmallVector.h" @@ -40,17 +41,18 @@ 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 function, we have a MemoryBlock of it's instruction data. - StringMap Functions; + // For each section, we have a MemoryBlock of it's data. + // Indexed by SectionID. + SmallVector Sections; + // For each section, the address it will be considered to live at for + // relocations. The same as the pointer the above memory block for hosted + // JITs. Indexed by SectionID. + SmallVector SectionLoadAddress; // Master symbol table. As modules are loaded and external symbols are - // resolved, their addresses are stored here. - StringMap SymbolTable; + // resolved, their addresses are stored here as a SectionID/Offset pair. + typedef std::pair SymbolLoc; + StringMap SymbolTable; bool HasError; std::string ErrorStr; @@ -62,6 +64,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 +80,15 @@ 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 reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0; + virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0; // Is the linker in an error state? bool hasError() { return HasError; } @@ -128,6 +136,8 @@ class RuntimeDyldELF : public RuntimeDyldImpl { StringMap Relocations; unsigned Arch; + void resolveRelocations(); + void resolveX86_64Relocation(StringRef Name, uint8_t *Addr, const RelocationEntry &RE); @@ -150,6 +160,7 @@ public: bool loadObject(MemoryBuffer *InputBuffer); void reassignSymbolAddress(StringRef Name, uint8_t *Addr); + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const; }; @@ -160,30 +171,34 @@ 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 { - std::string Target; // Object this relocation is contained in. - uint64_t Offset; // Offset into the object for the relocation. + 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. - bool isResolved; // Has this relocation been resolved previously? + 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(StringRef t, uint64_t offset, uint32_t data, int64_t addend) - : Target(t), Offset(offset), Data(data), Addend(addend), - isResolved(false) {} + RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) + : SectionID(id), Offset(offset), Data(data), Addend(addend) {} }; typedef SmallVector RelocationList; - StringMap Relocations; + // 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 Relocations; + // Relocations to symbols that are not yet resolved. Must be external + // relocations by definition. Indexed by symbol name. + StringMap UnresolvedRelocations; - // 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 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); + unsigned Type, unsigned Size, int64_t Addend); bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size); + unsigned Type, unsigned Size, int64_t Addend); bool loadSegment32(const MachOObject *Obj, const MachOObject::LoadCommandInfo *SegmentLCI, @@ -191,13 +206,23 @@ class RuntimeDyldMachO : public RuntimeDyldImpl { bool loadSegment64(const MachOObject *Obj, const MachOObject::LoadCommandInfo *SegmentLCI, const InMemoryStruct &SymtabLC); + bool processSymbols32(const MachOObject *Obj, + SmallVectorImpl &SectionMap, + SmallVectorImpl &SymbolNames, + const InMemoryStruct &SymtabLC); + bool processSymbols64(const MachOObject *Obj, + SmallVectorImpl &SectionMap, + SmallVectorImpl &SymbolNames, + const InMemoryStruct &SymtabLC); + + void resolveSymbol(StringRef Name); public: RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} bool loadObject(MemoryBuffer *InputBuffer); - void reassignSymbolAddress(StringRef Name, uint8_t *Addr); + void reassignSectionAddress(unsigned SectionID, uint64_t Addr); static bool isKnownFormat(const MemoryBuffer *InputBuffer); diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 36235f1bc17..81b2d17f885 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -22,25 +22,24 @@ 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!"); 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) @@ -75,9 +74,9 @@ resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, 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) { @@ -135,84 +134,63 @@ bool RuntimeDyldMachO:: loadSegment32(const MachOObject *Obj, const MachOObject::LoadCommandInfo *SegmentLCI, const InMemoryStruct &SymtabLC) { + // FIXME: This should really be combined w/ loadSegment64. Templatized + // function on the 32/64 datatypes maybe? InMemoryStruct SegmentLC; Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); if (!SegmentLC) return Error("unable to load segment load command"); + + SmallVector SectionMap; for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { InMemoryStruct 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. + // Allocate memory via the MM for the section. + uint8_t *Buffer; + uint32_t SectionID = Sections.size(); if (Sect->Flags != 0x80000400) - continue; + Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID); + else + Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID); - // Address and names of symbols in the section. - typedef std::pair SymbolEntry; - SmallVector Symbols; - // Index of all the names, in this section or not. Used when we're - // dealing with relocation entries. - SmallVector SymbolNames; - for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { - InMemoryStruct 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); + DEBUG(dbgs() << "Loading " + << ((Sect->Flags == 0x80000400) ? "text" : "data") + << " (ID #" << SectionID << ")" + << " '" << Sect->SegmentName << "," + << Sect->Name << "' of size " << Sect->Size + << " to address " << Buffer << ".\n"); - // 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()); - - // If there weren't any functions (odd, but just in case...) - if (!Symbols.size()) - continue; - - // Extract the function data. + // Copy the payload from the object file into the allocated buffer. 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); + memcpy(Buffer, Base + Sect->Address, Sect->Size); - // Now extract the relocation information for each function and process it. + // Remember what got allocated for this SectionID. + Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); + + // By default, the load address of a section is its memory buffer. + SectionLoadAddress.push_back((uint64_t)Buffer); + + // Keep a map of object file section numbers to corresponding SectionIDs + // while processing the file. + SectionMap.push_back(SectionID); + } + + // Process the symbol table. + SmallVector 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 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 RE; Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); @@ -222,51 +200,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]; + 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"); - // FIXME: Get the relocation addend from the target address. + // 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. - 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"); + // 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 +260,56 @@ loadSegment64(const MachOObject *Obj, if (!Segment64LC) return Error("unable to load segment load command"); + + SmallVector SectionMap; for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { InMemoryStruct 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. + // Allocate memory via the MM for the section. + uint8_t *Buffer; + uint32_t SectionID = Sections.size(); if (Sect->Flags != 0x80000400) - continue; + Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID); + else + Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID); - // Address and names of symbols in the section. - typedef std::pair SymbolEntry; - SmallVector Symbols; - // Index of all the names, in this section or not. Used when we're - // dealing with relocation entries. - SmallVector SymbolNames; - for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { - InMemoryStruct 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); + DEBUG(dbgs() << "Loading " + << ((Sect->Flags == 0x80000400) ? "text" : "data") + << " (ID #" << SectionID << ")" + << " '" << Sect->SegmentName << "," + << Sect->Name << "' of size " << Sect->Size + << " to address " << Buffer << ".\n"); - // 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()); - - // If there weren't any functions (odd, but just in case...) - if (!Symbols.size()) - continue; - - // Extract the function data. + // Copy the payload from the object file into the allocated buffer. 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); + memcpy(Buffer, Base + Sect->Address, Sect->Size); - // Now extract the relocation information for each function and process it. + // Remember what got allocated for this SectionID. + Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size)); + + // By default, the load address of a section is its memory buffer. + SectionLoadAddress.push_back((uint64_t)Buffer); + + // Keep a map of object file section numbers to corresponding SectionIDs + // while processing the file. + SectionMap.push_back(SectionID); + } + + // Process the symbol table. + SmallVector 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 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 RE; Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); @@ -360,48 +319,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"); - // FIXME: Get the relocation addend from the target address. + // 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. - 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"); + // 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; } +bool RuntimeDyldMachO:: +processSymbols32(const MachOObject *Obj, + SmallVectorImpl &SectionMap, + SmallVectorImpl &SymbolNames, + const InMemoryStruct &SymtabLC) { + // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such. + for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { + InMemoryStruct 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 &SectionMap, + SmallVectorImpl &SymbolNames, + const InMemoryStruct &SymtabLC) { + for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { + InMemoryStruct 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::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()) @@ -486,31 +539,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; +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. - RelocationList &Relocs = Relocations[Name]; + 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/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index 0065880bcbb..9fec7c464c5 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -51,12 +51,30 @@ EntryPoint("entry", class TrivialMemoryManager : public RTDyldMemoryManager { public: SmallVector FunctionMemory; + SmallVector DataMemory; + + uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID); + uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID); uint8_t *startFunctionBody(const char *Name, uintptr_t &Size); void endFunctionBody(const char *Name, uint8_t *FunctionStart, uint8_t *FunctionEnd); }; +uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size, + unsigned Alignment, + unsigned SectionID) { + return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); +} + +uint8_t *TrivialMemoryManager::allocateDataSection(uintptr_t Size, + unsigned Alignment, + unsigned SectionID) { + return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); +} + uint8_t *TrivialMemoryManager::startFunctionBody(const char *Name, uintptr_t &Size) { return (uint8_t*)sys::Memory::AllocateRWX(Size, 0, 0).base(); diff --git a/unittests/ExecutionEngine/JIT/JITTest.cpp b/unittests/ExecutionEngine/JIT/JITTest.cpp index abdb74925d1..4d10ee63803 100644 --- a/unittests/ExecutionEngine/JIT/JITTest.cpp +++ b/unittests/ExecutionEngine/JIT/JITTest.cpp @@ -113,6 +113,14 @@ public: EndFunctionBodyCall(F, FunctionStart, FunctionEnd)); Base->endFunctionBody(F, FunctionStart, FunctionEnd); } + virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + return Base->allocateDataSection(Size, Alignment, SectionID); + } + virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, + unsigned SectionID) { + return Base->allocateCodeSection(Size, Alignment, SectionID); + } virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { return Base->allocateSpace(Size, Alignment); }