unique_ptrify a bunch of stuff through RuntimeDyld::loadObject

llvm-svn: 217065
This commit is contained in:
David Blaikie 2014-09-03 19:48:09 +00:00
parent 3205b51673
commit ed9709d928
11 changed files with 71 additions and 68 deletions

View File

@ -29,7 +29,7 @@ class ObjectBuffer {
virtual void anchor();
public:
ObjectBuffer() {}
ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}
ObjectBuffer(std::unique_ptr<MemoryBuffer> Buf) : Buffer(std::move(Buf)) {}
virtual ~ObjectBuffer() {}
MemoryBufferRef getMemBuffer() const { return Buffer->getMemBufferRef(); }

View File

@ -31,7 +31,7 @@ protected:
std::unique_ptr<ObjectBuffer> Buffer;
public:
ObjectImage(ObjectBuffer *Input) : Buffer(Input) {}
ObjectImage(std::unique_ptr<ObjectBuffer> Input) : Buffer(std::move(Input)) {}
virtual ~ObjectImage() {}
virtual object::symbol_iterator begin_symbols() const = 0;

View File

@ -53,13 +53,15 @@ public:
/// Ownership of the input buffer is transferred to the ObjectImage
/// instance returned from this function if successful. In the case of load
/// failure, the input buffer will be deleted.
ObjectImage *loadObject(ObjectBuffer *InputBuffer);
std::unique_ptr<ObjectImage>
loadObject(std::unique_ptr<ObjectBuffer> InputBuffer);
/// Prepare the referenced object file for execution.
/// Ownership of the input object is transferred to the ObjectImage
/// instance returned from this function if successful. In the case of load
/// failure, the input object will be deleted.
ObjectImage *loadObject(std::unique_ptr<object::ObjectFile> InputObject);
std::unique_ptr<ObjectImage>
loadObject(std::unique_ptr<object::ObjectFile> InputObject);
/// Get the address of our local copy of the symbol. This may or may not
/// be the address used for relocation (clients can copy the data around

View File

@ -82,15 +82,9 @@ MCJIT::~MCJIT() {
Dyld.deregisterEHFrames();
LoadedObjectList::iterator it, end;
for (it = LoadedObjects.begin(), end = LoadedObjects.end(); it != end; ++it) {
ObjectImage *Obj = *it;
if (Obj) {
for (auto &Obj : LoadedObjects)
if (Obj)
NotifyFreeingObject(*Obj);
delete Obj;
}
}
LoadedObjects.clear();
Archives.clear();
}
@ -106,11 +100,11 @@ bool MCJIT::removeModule(Module *M) {
}
void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
ObjectImage *LoadedObject = Dyld.loadObject(std::move(Obj));
std::unique_ptr<ObjectImage> LoadedObject = Dyld.loadObject(std::move(Obj));
if (!LoadedObject || Dyld.hasError())
report_fatal_error(Dyld.getErrorString());
LoadedObjects.push_back(LoadedObject);
LoadedObjects.push_back(std::move(LoadedObject));
NotifyObjectEmitted(*LoadedObject);
}
@ -183,9 +177,10 @@ void MCJIT::generateCodeForModule(Module *M) {
std::unique_ptr<ObjectBuffer> ObjectToLoad;
// Try to load the pre-compiled object from cache if possible
if (ObjCache) {
std::unique_ptr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
if (PreCompiledObject.get())
ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.release()));
if (std::unique_ptr<MemoryBuffer> PreCompiledObject =
ObjCache->getObject(M))
ObjectToLoad =
llvm::make_unique<ObjectBuffer>(std::move(PreCompiledObject));
}
// If the cache did not contain a suitable object, compile the object
@ -196,8 +191,8 @@ void MCJIT::generateCodeForModule(Module *M) {
// Load the object into the dynamic linker.
// MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.release());
LoadedObjects.push_back(LoadedObject);
std::unique_ptr<ObjectImage> LoadedObject =
Dyld.loadObject(std::move(ObjectToLoad));
if (!LoadedObject)
report_fatal_error(Dyld.getErrorString());
@ -206,6 +201,8 @@ void MCJIT::generateCodeForModule(Module *M) {
NotifyObjectEmitted(*LoadedObject);
LoadedObjects.push_back(std::move(LoadedObject));
OwnedModules.markModuleAsLoaded(M);
}
@ -563,10 +560,8 @@ void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
}
void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
MutexGuard locked(lock);
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
JITEventListener *L = EventListeners[I];
for (JITEventListener *L : EventListeners)
L->NotifyFreeingObject(Obj);
}
}
uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {

View File

@ -219,8 +219,7 @@ class MCJIT : public ExecutionEngine {
SmallVector<object::OwningBinary<object::Archive>, 2> Archives;
SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers;
typedef SmallVector<ObjectImage *, 2> LoadedObjectList;
LoadedObjectList LoadedObjects;
SmallVector<std::unique_ptr<ObjectImage>, 2> LoadedObjects;
// An optional ObjectCache to be notified of compiled objects and used to
// perform lookup of pre-compiled code to avoid re-compilation.

View File

@ -36,16 +36,13 @@ protected:
// This form of the constructor allows subclasses to use
// format-specific subclasses of ObjectFile directly
ObjectImageCommon(ObjectBuffer *Input, std::unique_ptr<object::ObjectFile> Obj)
: ObjectImage(Input), // saves Input as Buffer and takes ownership
ObjFile(std::move(Obj))
{
}
ObjectImageCommon(std::unique_ptr<ObjectBuffer> Input,
std::unique_ptr<object::ObjectFile> Obj)
: ObjectImage(std::move(Input)), ObjFile(std::move(Obj)) {}
public:
ObjectImageCommon(ObjectBuffer* Input)
: ObjectImage(Input) // saves Input as Buffer and takes ownership
{
ObjectImageCommon(std::unique_ptr<ObjectBuffer> Input)
: ObjectImage(std::move(Input)) {
// FIXME: error checking? createObjectFile returns an ErrorOr<ObjectFile*>
// and should probably be checked for failure.
MemoryBufferRef Buf = Buffer->getMemBuffer();

View File

@ -794,7 +794,8 @@ createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
return Dyld;
}
ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
std::unique_ptr<ObjectImage>
RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
std::unique_ptr<ObjectImage> InputImage;
ObjectFile &Obj = *InputObject;
@ -816,19 +817,21 @@ ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
report_fatal_error("Incompatible object format!");
Dyld->loadObject(InputImage.get());
return InputImage.release();
return InputImage;
}
ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
std::unique_ptr<ObjectImage>
RuntimeDyld::loadObject(std::unique_ptr<ObjectBuffer> InputBuffer) {
std::unique_ptr<ObjectImage> InputImage;
sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
auto *InputBufferPtr = InputBuffer.get();
switch (Type) {
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
case sys::fs::file_magic::elf_core:
InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
InputImage = RuntimeDyldELF::createObjectImage(std::move(InputBuffer));
if (!Dyld)
Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
break;
@ -842,7 +845,7 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
case sys::fs::file_magic::macho_bundle:
case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
case sys::fs::file_magic::macho_dsym_companion:
InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
InputImage = RuntimeDyldMachO::createObjectImage(std::move(InputBuffer));
if (!Dyld)
Dyld = createRuntimeDyldMachO(
static_cast<Triple::ArchType>(InputImage->getArch()),
@ -859,11 +862,11 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
report_fatal_error("Incompatible object format!");
}
if (!Dyld->isCompatibleFormat(InputBuffer))
if (!Dyld->isCompatibleFormat(InputBufferPtr))
report_fatal_error("Incompatible object format!");
Dyld->loadObject(InputImage.get());
return InputImage.release();
return InputImage;
}
void *RuntimeDyld::getSymbolAddress(StringRef Name) {

View File

@ -77,8 +77,10 @@ template <class ELFT> class ELFObjectImage : public ObjectImageCommon {
bool Registered;
public:
ELFObjectImage(ObjectBuffer *Input, std::unique_ptr<DyldELFObject<ELFT>> Obj)
: ObjectImageCommon(Input, std::move(Obj)), Registered(false) {}
ELFObjectImage(std::unique_ptr<ObjectBuffer> Input,
std::unique_ptr<DyldELFObject<ELFT>> Obj)
: ObjectImageCommon(std::move(Input), std::move(Obj)), Registered(false) {
}
virtual ~ELFObjectImage() {
if (Registered)
@ -212,7 +214,8 @@ RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> Ob
llvm_unreachable("Unexpected ELF format");
}
ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
std::unique_ptr<ObjectImage>
RuntimeDyldELF::createObjectImage(std::unique_ptr<ObjectBuffer> Buffer) {
if (Buffer->getBufferSize() < ELF::EI_NIDENT)
llvm_unreachable("Unexpected ELF object size");
std::pair<unsigned char, unsigned char> Ident =
@ -226,28 +229,30 @@ ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
auto Obj =
llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>(
Buf, ec);
return new ELFObjectImage<ELFType<support::little, 4, false>>(
Buffer, std::move(Obj));
} else if (Ident.first == ELF::ELFCLASS32 &&
Ident.second == ELF::ELFDATA2MSB) {
return llvm::make_unique<
ELFObjectImage<ELFType<support::little, 4, false>>>(std::move(Buffer),
std::move(Obj));
}
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
auto Obj =
llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(Buf,
ec);
return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer,
std::move(Obj));
} else if (Ident.first == ELF::ELFCLASS64 &&
Ident.second == ELF::ELFDATA2MSB) {
return llvm::make_unique<ELFObjectImage<ELFType<support::big, 4, false>>>(
std::move(Buffer), std::move(Obj));
}
if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>(
Buf, ec);
return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, std::move(Obj));
} else if (Ident.first == ELF::ELFCLASS64 &&
Ident.second == ELF::ELFDATA2LSB) {
auto Obj =
llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(Buf,
ec);
return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, std::move(Obj));
} else
llvm_unreachable("Unexpected ELF format");
return llvm::make_unique<ELFObjectImage<ELFType<support::big, 8, true>>>(
std::move(Buffer), std::move(Obj));
}
assert(Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB &&
"Unexpected ELF format");
auto Obj =
llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(Buf,
ec);
return llvm::make_unique<ELFObjectImage<ELFType<support::little, 8, true>>>(
std::move(Buffer), std::move(Obj));
}
RuntimeDyldELF::~RuntimeDyldELF() {}

View File

@ -119,7 +119,8 @@ public:
ObjSectionToIDMap &SectionMap) override;
virtual ~RuntimeDyldELF();
static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
static std::unique_ptr<ObjectImage>
createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer);
static ObjectImage *createObjectImageFromFile(std::unique_ptr<object::ObjectFile> Obj);
};

View File

@ -103,8 +103,9 @@ protected:
public:
/// Create an ObjectImage from the given ObjectBuffer.
static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
return new ObjectImageCommon(InputBuffer);
static std::unique_ptr<ObjectImage>
createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer) {
return llvm::make_unique<ObjectImageCommon>(std::move(InputBuffer));
}
/// Create an ObjectImage from the given ObjectFile.

View File

@ -203,8 +203,8 @@ static int printLineInfoForInput() {
std::unique_ptr<ObjectImage> LoadedObject;
// Load the object file
LoadedObject.reset(
Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
LoadedObject = Dyld.loadObject(
llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
if (!LoadedObject) {
return Error(Dyld.getErrorString());
}
@ -264,8 +264,8 @@ static int executeInput() {
return Error("unable to read input: '" + EC.message() + "'");
std::unique_ptr<ObjectImage> LoadedObject;
// Load the object file
LoadedObject.reset(
Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
LoadedObject = Dyld.loadObject(
llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
if (!LoadedObject) {
return Error(Dyld.getErrorString());
}
@ -427,8 +427,8 @@ static int linkAndVerify() {
std::unique_ptr<ObjectImage> LoadedObject;
// Load the object file
LoadedObject.reset(
Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release())));
LoadedObject = Dyld.loadObject(
llvm::make_unique<ObjectBuffer>(std::move(*InputBuffer)));
if (!LoadedObject) {
return Error(Dyld.getErrorString());
}