[lld] r238691 - COFF: Detect file type by file magic.

Rui Ueyama ruiu at google.com
Sun May 31 14:17:11 PDT 2015


Author: ruiu
Date: Sun May 31 16:17:10 2015
New Revision: 238691

URL: http://llvm.org/viewvc/llvm-project?rev=238691&view=rev
Log:
COFF: Detect file type by file magic.

Added:
    lld/trunk/test/COFF/filetype.test
Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h
    lld/trunk/COFF/DriverUtils.cpp

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=238691&r1=238690&r2=238691&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Sun May 31 16:17:10 2015
@@ -32,6 +32,8 @@ using llvm::COFF::IMAGE_SUBSYSTEM_UNKNOW
 using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI;
 using llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI;
 using llvm::sys::Process;
+using llvm::sys::fs::file_magic;
+using llvm::sys::fs::identify_magic;
 
 namespace lld {
 namespace coff {
@@ -62,14 +64,17 @@ static std::string getOutputPath(llvm::o
 
 // Opens a file. Path has to be resolved already.
 // Newly created memory buffers are owned by this driver.
-ErrorOr<std::unique_ptr<InputFile>> LinkerDriver::createFile(StringRef Path) {
+ErrorOr<std::unique_ptr<InputFile>> LinkerDriver::openFile(StringRef Path) {
   auto MBOrErr = MemoryBuffer::getFile(Path);
   if (auto EC = MBOrErr.getError())
     return EC;
   std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
   MemoryBufferRef MBRef = MB->getMemBufferRef();
   OwningMBs.push_back(std::move(MB)); // take ownership
-  if (StringRef(Path).endswith_lower(".lib"))
+
+  // File type is detected by contents, not by file extension.
+  file_magic Magic = identify_magic(MBRef.getBuffer());
+  if (Magic == file_magic::archive)
     return std::unique_ptr<InputFile>(new ArchiveFile(MBRef));
   return std::unique_ptr<InputFile>(new ObjectFile(MBRef));
 }
@@ -105,7 +110,7 @@ LinkerDriver::parseDirectives(StringRef
 
   for (auto *Arg : Args->filtered(OPT_defaultlib)) {
     if (Optional<StringRef> Path = findLib(Arg->getValue())) {
-      auto FileOrErr = createFile(*Path);
+      auto FileOrErr = openFile(*Path);
       if (auto EC = FileOrErr.getError())
         return EC;
       std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
@@ -303,7 +308,7 @@ bool LinkerDriver::link(int Argc, const
   // Parse all input files and put all symbols to the symbol table.
   // The symbol table will take care of name resolution.
   for (StringRef Path : Inputs) {
-    auto FileOrErr = createFile(Path);
+    auto FileOrErr = openFile(Path);
     if (auto EC = FileOrErr.getError()) {
       llvm::errs() << Path << ": " << EC.message() << "\n";
       return false;

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=238691&r1=238690&r2=238691&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Sun May 31 16:17:10 2015
@@ -48,7 +48,7 @@ private:
   StringAllocator Alloc;
 
   // Opens a file. Path has to be resolved already.
-  ErrorOr<std::unique_ptr<InputFile>> createFile(StringRef Path);
+  ErrorOr<std::unique_ptr<InputFile>> openFile(StringRef Path);
 
   // Searches a file from search paths.
   Optional<StringRef> findFile(StringRef Filename);

Modified: lld/trunk/COFF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DriverUtils.cpp?rev=238691&r1=238690&r2=238691&view=diff
==============================================================================
--- lld/trunk/COFF/DriverUtils.cpp (original)
+++ lld/trunk/COFF/DriverUtils.cpp Sun May 31 16:17:10 2015
@@ -33,8 +33,6 @@
 using namespace llvm::COFF;
 using namespace llvm;
 using llvm::sys::Process;
-using llvm::sys::fs::file_magic;
-using llvm::sys::fs::identify_magic;
 
 namespace lld {
 namespace coff {

Added: lld/trunk/test/COFF/filetype.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/filetype.test?rev=238691&view=auto
==============================================================================
--- lld/trunk/test/COFF/filetype.test (added)
+++ lld/trunk/test/COFF/filetype.test Sun May 31 16:17:10 2015
@@ -0,0 +1,4 @@
+# Make sure input file type is detected by file magic and not by extension.
+
+# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.lib
+# RUN: lld -flavor link2 /out:%t.exe %t.lib





More information about the llvm-commits mailing list