Skip to content

Commit

Permalink
Extend RTDyld API to enable optionally precomputing the total amount …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
lhames committed Feb 12, 2014
1 parent 29a60e6 commit 061a739
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 66 deletions.
14 changes: 14 additions & 0 deletions include/llvm/ExecutionEngine/RTDyldMemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ class RTDyldMemoryManager {
uintptr_t Size, unsigned Alignment, unsigned SectionID,
StringRef SectionName, bool IsReadOnly) = 0;

/// Inform the memory manager about the total amount of memory required to
/// allocate all sections to be loaded:
/// \p CodeSize - the total size of all code sections
/// \p DataSizeRO - the total size of all read-only data sections
/// \p DataSizeRW - the total size of all read-write data sections
///
/// Note that by default the callback is disabled. To enable it
/// redefine the method needsToReserveAllocationSpace to return true.
virtual void reserveAllocationSpace(
uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { }

/// Override to return true to enable the reserveAllocationSpace callback.
virtual bool needsToReserveAllocationSpace() { return false; }

/// Register the EH frames with the runtime so that c++ exceptions work.
///
/// \p Addr parameter provides the local address of the EH frame section
Expand Down
9 changes: 9 additions & 0 deletions lib/ExecutionEngine/MCJIT/MCJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class LinkingMemoryManager : public RTDyldMemoryManager {
return ClientMM->allocateDataSection(Size, Alignment,
SectionID, SectionName, IsReadOnly);
}

virtual void reserveAllocationSpace(
uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) {
return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
}

virtual bool needsToReserveAllocationSpace() {
return ClientMM->needsToReserveAllocationSpace();
}

virtual void notifyObjectLoaded(ExecutionEngine *EE,
const ObjectImage *Obj) {
Expand Down
252 changes: 187 additions & 65 deletions lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -117,45 +124,45 @@ 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 ||
SymType == object::SymbolRef::ST_Unknown) {
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);
Expand All @@ -166,37 +173,177 @@ 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);
}
}

// 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,
Expand Down Expand Up @@ -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));
Expand All @@ -278,19 +403,16 @@ 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));
Check(Section.isZeroInit(IsZeroInit));
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
Expand Down
9 changes: 9 additions & 0 deletions lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ class RuntimeDyldImpl {
virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
virtual ObjectImage *createObjectImageFromFile(object::ObjectFile *InputObject);

// \brief Compute an upper bound of the memory that is required to load all sections
void computeTotalAllocSize(ObjectImage &Obj,
uint64_t& CodeSize,
uint64_t& DataSizeRO,
uint64_t& DataSizeRW);

// \brief Compute the stub buffer size required for a section
unsigned computeSectionStubBufSize(ObjectImage &Obj, const SectionRef &Section);

// This is the implementation for the two public overloads
ObjectImage *loadObject(ObjectImage *InputObject);

Expand Down
Loading

0 comments on commit 061a739

Please sign in to comment.