summaryrefslogtreecommitdiff
path: root/lib/ExecutionEngine
diff options
context:
space:
mode:
authorJim Grosbach <grosbach@apple.com>2012-01-16 22:26:39 +0000
committerJim Grosbach <grosbach@apple.com>2012-01-16 22:26:39 +0000
commit61425c0a7f4e3608a85f7bbf254cd052a15b7446 (patch)
tree54cde5d3f935fec86f9d6bd60ce2ccbea3376cf7 /lib/ExecutionEngine
parent27bf56056bed53d55d7ef0fd67d1851fa860b4f2 (diff)
downloadllvm-61425c0a7f4e3608a85f7bbf254cd052a15b7446.tar.gz
llvm-61425c0a7f4e3608a85f7bbf254cd052a15b7446.tar.bz2
llvm-61425c0a7f4e3608a85f7bbf254cd052a15b7446.tar.xz
MCJIT support for non-function sections.
Move to a by-section allocation and relocation scheme. This allows better support for sections which do not contain externally visible symbols. Flesh out the relocation address vs. local storage address separation a bit more as well. Remote process JITs use this to tell the relocation resolution code where the code will live when it executes. The startFunctionBody/endFunctionBody interfaces to the JIT and the memory manager are deprecated. They'll stick around for as long as the old JIT does, but the MCJIT doesn't use them anymore. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148258 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ExecutionEngine')
-rw-r--r--lib/ExecutionEngine/JIT/JITMemoryManager.cpp44
-rw-r--r--lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h10
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp23
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp57
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h85
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp494
6 files changed, 447 insertions, 266 deletions
diff --git a/lib/ExecutionEngine/JIT/JITMemoryManager.cpp b/lib/ExecutionEngine/JIT/JITMemoryManager.cpp
index eec23cec0a..efd570d7c5 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 bc7080d2d3..ac8c15579e 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 b017ebb2dc..18827978d9 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<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]);
+ }
}
//===----------------------------------------------------------------------===//
@@ -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 54cb350552..5c45cf3f80 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<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<sys::MemoryBlock>::iterator ContainingFunc
- = Functions.find(RE.Target);
- assert(ContainingFunc != Functions.end()
- && "Function for relocation not found");
- TargetAddr = reinterpret_cast<uint8_t*>(ContainingFunc->getValue().base()) +
- RE.Offset;
+ 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
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<sys::MemoryBlock>::iterator ContainingFunc
- = Functions.find(RE.Target);
- assert(ContainingFunc != Functions.end()
- && "Function for relocation not found");
- TargetAddr = reinterpret_cast<uint8_t*>(
- ContainingFunc->getValue().base()) + RE.Offset;
+ 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
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 cff7cbdf28..bbfef768c4 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<sys::MemoryBlock> Functions;
+ // 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 the above memory block for hosted
+ // JITs. Indexed by SectionID.
+ SmallVector<uint64_t, 32> SectionLoadAddress;
// 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 +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<RelocationList> 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<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);
+ // 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);
+ 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<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 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 36235f1bc1..81b2d17f88 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<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.
+ // 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);
+
+ 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));
- // 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 +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];
-
- // 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 +260,56 @@ 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.
+ // 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);
+
+ 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(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));
- // 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 +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");
+
+ // 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())
@@ -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;
-
- 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);
}
}