From 17bece31af57c221bcd247a08de05fa67c8e41da Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sun, 7 Apr 2013 16:58:48 +0000 Subject: [PATCH] Construct MachOObject in MachOObjectFile's constructor. llvm-svn: 178988 --- llvm/include/llvm/Object/MachO.h | 2 +- llvm/lib/Object/MachOObjectFile.cpp | 36 ++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h index f3b47a0..9bd591a 100644 --- a/llvm/include/llvm/Object/MachO.h +++ b/llvm/include/llvm/Object/MachO.h @@ -126,7 +126,7 @@ typedef MachOObject::LoadCommandInfo LoadCommandInfo; class MachOObjectFile : public ObjectFile { public: - MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec); + MachOObjectFile(MemoryBuffer *Object, error_code &ec); virtual symbol_iterator begin_symbols() const; virtual symbol_iterator end_symbols() const; diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 6f888fa..c817942 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -27,10 +27,23 @@ using namespace object; namespace llvm { namespace object { -MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, - error_code &ec) - : ObjectFile(Binary::ID_MachO, Object), - MachOObj(MOO) { +MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, error_code &ec) + : ObjectFile(Binary::ID_MachO, Object) { + // MachOObject takes ownership of the Buffer we passed to it, and + // MachOObjectFile does, too, so we need to make sure they don't get the + // same object. A MemoryBuffer is cheap (it's just a reference to memory, + // not a copy of the memory itself), so just make a new copy here for + // the MachOObjectFile. + MemoryBuffer *NewBuffer = + MemoryBuffer::getMemBuffer(Object->getBuffer(), + Object->getBufferIdentifier(), false); + std::string ErrorStr; + MachOObj.reset(MachOObject::LoadFromBuffer(NewBuffer, &ErrorStr)); + if (!MachOObj) { + ec = object_error::parse_failed; + return; + } + DataRefImpl DRI; moveToNextSection(DRI); uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; @@ -61,19 +74,10 @@ const macho::Header &MachOObjectFile::getHeader() const { ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { error_code ec; - std::string Err; - MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err); - if (!MachOObj) + ObjectFile *Ret = new MachOObjectFile(Buffer, ec); + if (ec) return NULL; - // MachOObject takes ownership of the Buffer we passed to it, and - // MachOObjectFile does, too, so we need to make sure they don't get the - // same object. A MemoryBuffer is cheap (it's just a reference to memory, - // not a copy of the memory itself), so just make a new copy here for - // the MachOObjectFile. - MemoryBuffer *NewBuffer = - MemoryBuffer::getMemBuffer(Buffer->getBuffer(), - Buffer->getBufferIdentifier(), false); - return new MachOObjectFile(NewBuffer, MachOObj, ec); + return Ret; } /*===-- Symbols -----------------------------------------------------------===*/ -- 2.7.4