From 2edc3a6b8df2962f4bcc769fd2606e4f6c36fb06 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 22 Jan 2014 16:04:52 +0000 Subject: [PATCH] Pass the computed magic to createBinary and createObjectFile if available. identify_magic is not free, so we should avoid calling it twice. The argument also makes it cheap for createBinary to just forward to createObjectFile. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199813 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/Binary.h | 5 ++++- include/llvm/Object/ObjectFile.h | 5 ++++- lib/Object/Binary.cpp | 20 ++++++++++---------- lib/Object/ObjectFile.cpp | 6 ++++-- tools/llvm-nm/llvm-nm.cpp | 6 +++--- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/include/llvm/Object/Binary.h b/include/llvm/Object/Binary.h index 6a73e0a5b1e..7742f83cd08 100644 --- a/include/llvm/Object/Binary.h +++ b/include/llvm/Object/Binary.h @@ -16,6 +16,7 @@ #include "llvm/Object/Error.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/FileSystem.h" namespace llvm { @@ -115,7 +116,9 @@ public: /// @param Source The data to create the Binary from. Ownership is transferred /// to the Binary if successful. If an error is returned, /// Source is destroyed by createBinary before returning. -ErrorOr createBinary(MemoryBuffer *Source); +ErrorOr createBinary(MemoryBuffer *Source, + sys::fs::file_magic Type = + sys::fs::file_magic::unknown); ErrorOr createBinary(StringRef Path); } diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 4aa1e066763..77833cbf940 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -18,6 +18,7 @@ #include "llvm/Object/Binary.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include #include @@ -377,7 +378,9 @@ public: /// return true. /// @brief Create ObjectFile from path. static ErrorOr createObjectFile(StringRef ObjectPath); - static ErrorOr createObjectFile(MemoryBuffer *Object); + static ErrorOr + createObjectFile(MemoryBuffer *Object, + sys::fs::file_magic Type = sys::fs::file_magic::unknown); static inline bool classof(const Binary *v) { return v->isObject(); diff --git a/lib/Object/Binary.cpp b/lib/Object/Binary.cpp index 4f35d975262..a0c412a64b2 100644 --- a/lib/Object/Binary.cpp +++ b/lib/Object/Binary.cpp @@ -41,17 +41,19 @@ StringRef Binary::getFileName() const { return Data->getBufferIdentifier(); } -ErrorOr object::createBinary(MemoryBuffer *Source) { +ErrorOr object::createBinary(MemoryBuffer *Source, + sys::fs::file_magic Type) { OwningPtr scopedSource(Source); - sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer()); - switch (type) { + if (Type == sys::fs::file_magic::unknown) + Type = sys::fs::identify_magic(Source->getBuffer()); + + switch (Type) { case sys::fs::file_magic::archive: return Archive::create(scopedSource.take()); 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 ObjectFile::createELFObjectFile(scopedSource.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: @@ -62,19 +64,17 @@ ErrorOr object::createBinary(MemoryBuffer *Source) { 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 ObjectFile::createMachOObjectFile(scopedSource.take()); - case sys::fs::file_magic::macho_universal_binary: - return MachOUniversalBinary::create(scopedSource.take()); case sys::fs::file_magic::coff_object: case sys::fs::file_magic::coff_import_library: case sys::fs::file_magic::pecoff_executable: - return ObjectFile::createCOFFObjectFile(scopedSource.take()); + return ObjectFile::createObjectFile(scopedSource.take(), Type); + case sys::fs::file_magic::macho_universal_binary: + return MachOUniversalBinary::create(scopedSource.take()); case sys::fs::file_magic::unknown: case sys::fs::file_magic::bitcode: - case sys::fs::file_magic::windows_resource: { + case sys::fs::file_magic::windows_resource: // Unrecognized object file format. return object_error::invalid_file_type; - } } llvm_unreachable("Unexpected Binary File Type"); } diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index fd2b024687c..6b14e78ff3d 100644 --- a/lib/Object/ObjectFile.cpp +++ b/lib/Object/ObjectFile.cpp @@ -37,9 +37,11 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { return section_iterator(SectionRef(Sec, this)); } -ErrorOr ObjectFile::createObjectFile(MemoryBuffer *Object) { +ErrorOr ObjectFile::createObjectFile(MemoryBuffer *Object, + sys::fs::file_magic Type) { OwningPtr ScopedObj(Object); - sys::fs::file_magic Type = sys::fs::identify_magic(Object->getBuffer()); + if (Type == sys::fs::file_magic::unknown) + Type = sys::fs::identify_magic(Object->getBuffer()); switch (Type) { case sys::fs::file_magic::unknown: diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 71f5046fca2..e4e62091d54 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -580,7 +580,7 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { delete Result; } } else if (magic == sys::fs::file_magic::archive) { - ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + ErrorOr BinaryOrErr = object::createBinary(Buffer.take(), magic); if (error(BinaryOrErr.getError(), Filename)) return; OwningPtr arch(BinaryOrErr.get()); @@ -631,7 +631,7 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { } } } else if (magic == sys::fs::file_magic::macho_universal_binary) { - ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + ErrorOr BinaryOrErr = object::createBinary(Buffer.take(), magic); if (error(BinaryOrErr.getError(), Filename)) return; OwningPtr Bin(BinaryOrErr.get()); @@ -649,7 +649,7 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { } } } else if (magic.is_object()) { - ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + ErrorOr BinaryOrErr = object::createBinary(Buffer.take(), magic); if (error(BinaryOrErr.getError(), Filename)) return; OwningPtr obj(BinaryOrErr.get()); -- 2.34.1