Support: Use llvm::COFF::BigObjMagic
authorRui Ueyama <ruiu@google.com>
Thu, 11 Sep 2014 22:34:32 +0000 (22:34 +0000)
committerRui Ueyama <ruiu@google.com>
Thu, 11 Sep 2014 22:34:32 +0000 (22:34 +0000)
Use llvm::COFF::BigObjMagic insetad of the string literal.
Also checks the version number.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217633 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Path.cpp
unittests/Support/Path.cpp

index 4d69b25f8fc19593f04867f3c5f4ed6759d7b78a..781c17c18793427188b8db888f1fde0b56c0f4e1 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/COFF.h"
+#include "llvm/Support/Endian.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Path.h"
-#include "llvm/Support/Endian.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Process.h"
@@ -904,12 +905,19 @@ file_magic identify_magic(StringRef Magic) {
       // COFF bigobj or short import library file
       if (Magic[1] == (char)0x00 && Magic[2] == (char)0xff &&
           Magic[3] == (char)0xff) {
-         const char BigobjMagic[] =
-             "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
-         if (Magic.size() >= 28 &&
-             memcmp(Magic.data() + 12, BigobjMagic, sizeof(BigobjMagic)) == 0)
-           return file_magic::coff_object;
-         return file_magic::coff_import_library;
+        size_t MinSize = offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
+        if (Magic.size() < MinSize)
+          return file_magic::coff_import_library;
+
+        int BigObjVersion = *reinterpret_cast<const support::ulittle16_t*>(
+            Magic.data() + offsetof(COFF::BigObjHeader, Version));
+        if (BigObjVersion < COFF::BigObjHeader::MinBigObjectVersion)
+          return file_magic::coff_import_library;
+
+        const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
+        if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) != 0)
+          return file_magic::coff_import_library;
+        return file_magic::coff_object;
       }
       // Windows resource file
       const char Expected[] = { 0, 0, 0, 0, '\x20', 0, 0, 0, '\xff' };
index 54cf96f6761d16bd32dd6589b99e10d375ea7a72..a12e9b79211c7c94c458331bfafb7946f17df001 100644 (file)
@@ -483,7 +483,7 @@ TEST_F(FileSystemTest, DirectoryIteration) {
 const char archive[] = "!<arch>\x0A";
 const char bitcode[] = "\xde\xc0\x17\x0b";
 const char coff_object[] = "\x00\x00......";
-const char coff_bigobj[] = "\x00\x00\xff\xff........"
+const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
     "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
 const char coff_import_library[] = "\x00\x00\xff\xff....";
 const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,