From 825fc31bd3a6e7604c4b69f28f32e90b5a882289 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 22 Jan 2014 00:14:49 +0000 Subject: Change createObjectFile to return an ErrorOr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199776 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ObjectFile.h | 4 ++-- lib/DebugInfo/DWARFUnit.cpp | 4 ++-- .../RuntimeDyld/ObjectImageCommon.h | 3 ++- lib/Object/Object.cpp | 4 +++- lib/Object/ObjectFile.cpp | 24 +++++++++------------- tools/lli/lli.cpp | 6 +++--- tools/llvm-ar/llvm-ar.cpp | 6 +++++- tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 7 ++++--- 8 files changed, 31 insertions(+), 27 deletions(-) diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 5ffa4293fb..4aa1e06676 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -376,8 +376,8 @@ public: /// @param ObjectPath The path to the object file. ObjectPath.isObject must /// return true. /// @brief Create ObjectFile from path. - static ObjectFile *createObjectFile(StringRef ObjectPath); - static ObjectFile *createObjectFile(MemoryBuffer *Object); + static ErrorOr createObjectFile(StringRef ObjectPath); + static ErrorOr createObjectFile(MemoryBuffer *Object); static inline bool classof(const Binary *v) { return v->isObject(); diff --git a/lib/DebugInfo/DWARFUnit.cpp b/lib/DebugInfo/DWARFUnit.cpp index 5167eb947c..732fb82e73 100644 --- a/lib/DebugInfo/DWARFUnit.cpp +++ b/lib/DebugInfo/DWARFUnit.cpp @@ -263,12 +263,12 @@ bool DWARFUnit::parseDWO() { sys::path::append(AbsolutePath, CompilationDir); } sys::path::append(AbsolutePath, DWOFileName); - object::ObjectFile *DWOFile = + ErrorOr DWOFile = object::ObjectFile::createObjectFile(AbsolutePath); if (!DWOFile) return false; // Reset DWOHolder. - DWO.reset(new DWOHolder(DWOFile)); + DWO.reset(new DWOHolder(DWOFile.get())); DWARFUnit *DWOCU = DWO->getUnit(); // Verify that compile unit in .dwo file is valid. if (DWOCU == 0 || DWOCU->getDWOId() != getDWOId()) { diff --git a/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h b/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h index 7666a869d6..2056f39b5a 100644 --- a/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h +++ b/lib/ExecutionEngine/RuntimeDyld/ObjectImageCommon.h @@ -44,7 +44,8 @@ public: ObjectImageCommon(ObjectBuffer* Input) : ObjectImage(Input) // saves Input as Buffer and takes ownership { - ObjFile = object::ObjectFile::createObjectFile(Buffer->getMemBuffer()); + ObjFile = + object::ObjectFile::createObjectFile(Buffer->getMemBuffer()).get(); } ObjectImageCommon(object::ObjectFile* Input) : ObjectImage(NULL), ObjFile(Input) {} diff --git a/lib/Object/Object.cpp b/lib/Object/Object.cpp index 1edc217178..e454d8eec7 100644 --- a/lib/Object/Object.cpp +++ b/lib/Object/Object.cpp @@ -59,7 +59,9 @@ wrap(const relocation_iterator *SI) { // ObjectFile creation LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { - return wrap(ObjectFile::createObjectFile(unwrap(MemBuf))); + ErrorOr ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf))); + ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : 0; + return wrap(Obj); } void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) { diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index 2397f4b515..fd2b024687 100644 --- a/lib/Object/ObjectFile.cpp +++ b/lib/Object/ObjectFile.cpp @@ -37,26 +37,22 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { return section_iterator(SectionRef(Sec, this)); } -ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { - if (Object->getBufferSize() < 64) { - delete Object; - return 0; - } - +ErrorOr ObjectFile::createObjectFile(MemoryBuffer *Object) { + OwningPtr ScopedObj(Object); sys::fs::file_magic Type = sys::fs::identify_magic(Object->getBuffer()); + switch (Type) { case sys::fs::file_magic::unknown: case sys::fs::file_magic::bitcode: case sys::fs::file_magic::archive: case sys::fs::file_magic::macho_universal_binary: case sys::fs::file_magic::windows_resource: - delete Object; - return 0; + return object_error::invalid_file_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: - return createELFObjectFile(Object).get(); + return createELFObjectFile(ScopedObj.take()); case sys::fs::file_magic::macho_object: case sys::fs::file_magic::macho_executable: case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: @@ -67,18 +63,18 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { 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: - return createMachOObjectFile(Object).get(); + return createMachOObjectFile(ScopedObj.take()); case sys::fs::file_magic::coff_object: case sys::fs::file_magic::coff_import_library: case sys::fs::file_magic::pecoff_executable: - return createCOFFObjectFile(Object).get(); + return createCOFFObjectFile(ScopedObj.take()); } llvm_unreachable("Unexpected Object File Type"); } -ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) { +ErrorOr ObjectFile::createObjectFile(StringRef ObjectPath) { OwningPtr File; - if (MemoryBuffer::getFile(ObjectPath, File)) - return NULL; + if (error_code EC = MemoryBuffer::getFile(ObjectPath, File)) + return EC; return createObjectFile(File.take()); } diff --git a/tools/lli/lli.cpp b/tools/lli/lli.cpp index 8e6669221b..2ee7979301 100644 --- a/tools/lli/lli.cpp +++ b/tools/lli/lli.cpp @@ -527,13 +527,13 @@ int main(int argc, char **argv, char * const *envp) { } for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) { - object::ObjectFile *Obj = object::ObjectFile::createObjectFile( - ExtraObjects[i]); + ErrorOr Obj = + object::ObjectFile::createObjectFile(ExtraObjects[i]); if (!Obj) { Err.print(argv[0], errs()); return 1; } - EE->addObjectFile(Obj); + EE->addObjectFile(Obj.get()); } for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp index 18f8776020..d4fe7d789c 100644 --- a/tools/llvm-ar/llvm-ar.cpp +++ b/tools/llvm-ar/llvm-ar.cpp @@ -670,7 +670,11 @@ static void writeSymbolTable( object::ObjectFile *Obj; if (I->isNewMember()) { const char *Filename = I->getNew(); - Obj = object::ObjectFile::createObjectFile(Filename); + if (ErrorOr ObjOrErr = + object::ObjectFile::createObjectFile(Filename)) + Obj = ObjOrErr.get(); + else + Obj = NULL; } else { object::Archive::child_iterator OldMember = I->getOld(); OwningPtr Binary; diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 7c6cc68fb9..f827909dc3 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -93,11 +93,12 @@ static void DumpInput(const StringRef &Filename) { return; } - OwningPtr Obj(ObjectFile::createObjectFile(Buff.take())); - if (!Obj) { - errs() << Filename << ": Unknown object file format\n"; + ErrorOr ObjOrErr(ObjectFile::createObjectFile(Buff.take())); + if (error_code EC = ObjOrErr.getError()) { + errs() << Filename << ": " << EC.message() << '\n'; return; } + OwningPtr Obj(ObjOrErr.get()); OwningPtr DICtx(DIContext::getDWARFContext(Obj.get())); -- cgit v1.2.3