summaryrefslogtreecommitdiff
path: root/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2014-02-12 21:30:07 +0000
committerLang Hames <lhames@gmail.com>2014-02-12 21:30:07 +0000
commit061a739395facc05c5e7dfb7b991dd6cd69ba503 (patch)
tree725ac0d40f51f672a50c6ec0314d6442ddb1537a /lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
parent29a60e6deba1f4f77d7f688017af66c90f5d6580 (diff)
downloadllvm-061a739395facc05c5e7dfb7b991dd6cd69ba503.tar.gz
llvm-061a739395facc05c5e7dfb7b991dd6cd69ba503.tar.bz2
llvm-061a739395facc05c5e7dfb7b991dd6cd69ba503.tar.xz
Extend RTDyld API to enable optionally precomputing the total amount of memory
required for all sections in a module. This can be useful when targets or code-models place strict requirements on how sections must be laid out in memory. If RTDyldMemoryManger::needsToReserveAllocationSpace() is overridden to return true then the JIT will call the following method on the memory manager, which can be used to preallocate the necessary memory. void RTDyldMemoryManager::reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) Patch by Vaidas Gasiunas. Thanks very much Viadas! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201259 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp')
-rw-r--r--lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp252
1 files changed, 187 insertions, 65 deletions
diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
index 7a1c4d7690..3c88aea7a2 100644
--- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
+++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
@@ -19,7 +19,6 @@
#include "RuntimeDyldImpl.h"
#include "RuntimeDyldMachO.h"
#include "llvm/Object/ELF.h"
-#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MutexGuard.h"
@@ -97,14 +96,22 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
MutexGuard locked(lock);
- OwningPtr<ObjectImage> obj(InputObject);
- if (!obj)
+ OwningPtr<ObjectImage> Obj(InputObject);
+ if (!Obj)
return NULL;
// Save information about our target
- Arch = (Triple::ArchType)obj->getArch();
- IsTargetLittleEndian = obj->getObjectFile()->isLittleEndian();
-
+ Arch = (Triple::ArchType)Obj->getArch();
+ IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
+
+ // Compute the memory size required to load all sections to be loaded
+ // and pass this information to the memory manager
+ if (MemMgr->needsToReserveAllocationSpace()) {
+ uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
+ computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
+ MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
+ }
+
// Symbols found in this object
StringMap<SymbolLoc> LocalSymbols;
// Used sections from the object file
@@ -117,24 +124,24 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
// Parse symbols
DEBUG(dbgs() << "Parse symbols:\n");
- for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); i != e;
- ++i) {
+ for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
+ ++I) {
object::SymbolRef::Type SymType;
StringRef Name;
- Check(i->getType(SymType));
- Check(i->getName(Name));
+ Check(I->getType(SymType));
+ Check(I->getName(Name));
- uint32_t flags = i->getFlags();
+ uint32_t Flags = I->getFlags();
- bool isCommon = flags & SymbolRef::SF_Common;
- if (isCommon) {
+ bool IsCommon = Flags & SymbolRef::SF_Common;
+ if (IsCommon) {
// Add the common symbols to a list. We'll allocate them all below.
uint32_t Align;
- Check(i->getAlignment(Align));
+ Check(I->getAlignment(Align));
uint64_t Size = 0;
- Check(i->getSize(Size));
+ Check(I->getSize(Size));
CommonSize += Size + Align;
- CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
+ CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
} else {
if (SymType == object::SymbolRef::ST_Function ||
SymType == object::SymbolRef::ST_Data ||
@@ -142,20 +149,20 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
uint64_t FileOffset;
StringRef SectionData;
bool IsCode;
- section_iterator si = obj->end_sections();
- Check(i->getFileOffset(FileOffset));
- Check(i->getSection(si));
- if (si == obj->end_sections()) continue;
- Check(si->getContents(SectionData));
- Check(si->isText(IsCode));
+ section_iterator SI = Obj->end_sections();
+ Check(I->getFileOffset(FileOffset));
+ Check(I->getSection(SI));
+ if (SI == Obj->end_sections()) continue;
+ Check(SI->getContents(SectionData));
+ Check(SI->isText(IsCode));
const uint8_t* SymPtr = (const uint8_t*)InputObject->getData().data() +
(uintptr_t)FileOffset;
uintptr_t SectOffset = (uintptr_t)(SymPtr -
(const uint8_t*)SectionData.begin());
- unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
+ unsigned SectionID = findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
- << " flags: " << flags
+ << " flags: " << Flags
<< " SID: " << SectionID
<< " Offset: " << format("%p", SectOffset));
GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
@@ -166,29 +173,31 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
// Allocate common symbols
if (CommonSize != 0)
- emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
+ emitCommonSymbols(*Obj, CommonSymbols, CommonSize, LocalSymbols);
// Parse and process relocations
DEBUG(dbgs() << "Parse relocations:\n");
- for (section_iterator si = obj->begin_sections(), se = obj->end_sections();
- si != se; ++si) {
- bool isFirstRelocation = true;
+ for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
+ SI != SE; ++SI) {
+ bool IsFirstRelocation = true;
unsigned SectionID = 0;
StubMap Stubs;
- section_iterator RelocatedSection = si->getRelocatedSection();
+ section_iterator RelocatedSection = SI->getRelocatedSection();
- for (relocation_iterator i = si->relocation_begin(),
- e = si->relocation_end();
- i != e; ++i) {
+ for (relocation_iterator I = SI->relocation_begin(),
+ E = SI->relocation_end();
+ I != E; ++I) {
// If it's the first relocation in this section, find its SectionID
- if (isFirstRelocation) {
+ if (IsFirstRelocation) {
+ bool IsCode = false;
+ Check(RelocatedSection->isText(IsCode));
SectionID =
- findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
+ findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
- isFirstRelocation = false;
+ IsFirstRelocation = false;
}
- processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
+ processRelocationRef(SectionID, *I, *Obj, LocalSections, LocalSymbols,
Stubs);
}
}
@@ -196,7 +205,145 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
// Give the subclasses a chance to tie-up any loose ends.
finalizeLoad(LocalSections);
- return obj.take();
+ return Obj.take();
+}
+
+// A helper method for computeTotalAllocSize.
+// Computes the memory size required to allocate sections with the given sizes,
+// assuming that all sections are allocated with the given alignment
+static uint64_t computeAllocationSizeForSections(std::vector<uint64_t>& SectionSizes,
+ uint64_t Alignment) {
+ uint64_t TotalSize = 0;
+ for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
+ uint64_t AlignedSize = (SectionSizes[Idx] + Alignment - 1) /
+ Alignment * Alignment;
+ TotalSize += AlignedSize;
+ }
+ return TotalSize;
+}
+
+// Compute an upper bound of the memory size that is required to load all sections
+void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
+ uint64_t& CodeSize, uint64_t& DataSizeRO, uint64_t& DataSizeRW) {
+ // Compute the size of all sections required for execution
+ std::vector<uint64_t> CodeSectionSizes;
+ std::vector<uint64_t> ROSectionSizes;
+ std::vector<uint64_t> RWSectionSizes;
+ uint64_t MaxAlignment = sizeof(void*);
+
+ // Collect sizes of all sections to be loaded;
+ // also determine the max alignment of all sections
+ for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
+ SI != SE; ++SI) {
+ const SectionRef &Section = *SI;
+
+ bool IsRequired;
+ Check(Section.isRequiredForExecution(IsRequired));
+
+ // Consider only the sections that are required to be loaded for execution
+ if (IsRequired) {
+ uint64_t DataSize = 0;
+ uint64_t Alignment64 = 0;
+ bool IsCode = false;
+ bool IsReadOnly = false;
+ StringRef Name;
+ Check(Section.getSize(DataSize));
+ Check(Section.getAlignment(Alignment64));
+ Check(Section.isText(IsCode));
+ Check(Section.isReadOnlyData(IsReadOnly));
+ Check(Section.getName(Name));
+ unsigned Alignment = (unsigned) Alignment64 & 0xffffffffL;
+
+ uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
+ uint64_t SectionSize = DataSize + StubBufSize;
+
+ // The .eh_frame section (at least on Linux) needs an extra four bytes padded
+ // with zeroes added at the end. For MachO objects, this section has a
+ // slightly different name, so this won't have any effect for MachO objects.
+ if (Name == ".eh_frame")
+ SectionSize += 4;
+
+ if (SectionSize > 0) {
+ // save the total size of the section
+ if (IsCode) {
+ CodeSectionSizes.push_back(SectionSize);
+ } else if (IsReadOnly) {
+ ROSectionSizes.push_back(SectionSize);
+ } else {
+ RWSectionSizes.push_back(SectionSize);
+ }
+ // update the max alignment
+ if (Alignment > MaxAlignment) {
+ MaxAlignment = Alignment;
+ }
+ }
+ }
+ }
+
+ // Compute the size of all common symbols
+ uint64_t CommonSize = 0;
+ for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols();
+ I != E; ++I) {
+ uint32_t Flags = I->getFlags();
+ if (Flags & SymbolRef::SF_Common) {
+ // Add the common symbols to a list. We'll allocate them all below.
+ uint64_t Size = 0;
+ Check(I->getSize(Size));
+ CommonSize += Size;
+ }
+ }
+ if (CommonSize != 0) {
+ RWSectionSizes.push_back(CommonSize);
+ }
+
+ // Compute the required allocation space for each different type of sections
+ // (code, read-only data, read-write data) assuming that all sections are
+ // allocated with the max alignment. Note that we cannot compute with the
+ // individual alignments of the sections, because then the required size
+ // depends on the order, in which the sections are allocated.
+ CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
+ DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
+ DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
+}
+
+// compute stub buffer size for the given section
+unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
+ const SectionRef &Section) {
+ unsigned StubSize = getMaxStubSize();
+ if (StubSize == 0) {
+ return 0;
+ }
+ // FIXME: this is an inefficient way to handle this. We should computed the
+ // necessary section allocation size in loadObject by walking all the sections
+ // once.
+ unsigned StubBufSize = 0;
+ for (section_iterator SI = Obj.begin_sections(),
+ SE = Obj.end_sections();
+ SI != SE; ++SI) {
+ section_iterator RelSecI = SI->getRelocatedSection();
+ if (!(RelSecI == Section))
+ continue;
+
+ for (relocation_iterator I = SI->relocation_begin(),
+ E = SI->relocation_end();
+ I != E; ++I) {
+ StubBufSize += StubSize;
+ }
+ }
+
+ // Get section data size and alignment
+ uint64_t Alignment64;
+ uint64_t DataSize;
+ Check(Section.getSize(DataSize));
+ Check(Section.getAlignment(Alignment64));
+
+ // Add stubbuf size alignment
+ unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
+ unsigned StubAlignment = getStubAlignment();
+ unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
+ if (StubAlignment > EndAlignment)
+ StubBufSize += StubAlignment - EndAlignment;
+ return StubBufSize;
}
void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
@@ -244,28 +391,6 @@ unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
const SectionRef &Section,
bool IsCode) {
- unsigned StubBufSize = 0,
- StubSize = getMaxStubSize();
- const ObjectFile *ObjFile = Obj.getObjectFile();
- // FIXME: this is an inefficient way to handle this. We should computed the
- // necessary section allocation size in loadObject by walking all the sections
- // once.
- if (StubSize > 0) {
- for (section_iterator SI = ObjFile->section_begin(),
- SE = ObjFile->section_end();
- SI != SE; ++SI) {
- section_iterator RelSecI = SI->getRelocatedSection();
- if (!(RelSecI == Section))
- continue;
-
- for (relocation_iterator I = SI->relocation_begin(),
- E = SI->relocation_end();
- I != E; ++I) {
- StubBufSize += StubSize;
- }
- }
- }
-
StringRef data;
uint64_t Alignment64;
Check(Section.getContents(data));
@@ -278,6 +403,7 @@ unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
bool IsReadOnly;
uint64_t DataSize;
unsigned PaddingSize = 0;
+ unsigned StubBufSize = 0;
StringRef Name;
Check(Section.isRequiredForExecution(IsRequired));
Check(Section.isVirtual(IsVirtual));
@@ -285,12 +411,8 @@ unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
Check(Section.isReadOnlyData(IsReadOnly));
Check(Section.getSize(DataSize));
Check(Section.getName(Name));
- if (StubSize > 0) {
- unsigned StubAlignment = getStubAlignment();
- unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
- if (StubAlignment > EndAlignment)
- StubBufSize += StubAlignment - EndAlignment;
- }
+
+ StubBufSize = computeSectionStubBufSize(Obj, Section);
// The .eh_frame section (at least on Linux) needs an extra four bytes padded
// with zeroes added at the end. For MachO objects, this section has a