Path: Recognize Windows compiled resource file.
authorRui Ueyama <ruiu@google.com>
Tue, 15 Oct 2013 22:45:38 +0000 (22:45 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 15 Oct 2013 22:45:38 +0000 (22:45 +0000)
Some background: One can pass compiled resource files (.res files) directly
to the linker on Windows. If a resource file is given, the linker will run
"cvtres" command in background to convert the resource file to a COFF file
to link it.

What I'm trying to do with this patch is to make the linker to recognize
the resource file by file magic, so that it can run cvtres command.

Differential Revision: http://llvm-reviews.chandlerc.com/D1943

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

include/llvm/Support/FileSystem.h
lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
lib/Object/Binary.cpp
lib/Object/ObjectFile.cpp
lib/Support/Path.cpp

index 9d72a665125759f19a5096125b576445aa1e87a0..24bb8ea63e0c1b3afe8edf8507b1964dff53b1ec 100644 (file)
@@ -238,7 +238,8 @@ struct file_magic {
     macho_dsym_companion,     ///< Mach-O dSYM companion file
     macho_universal_binary,   ///< Mach-O universal binary
     coff_object,              ///< COFF object file
-    pecoff_executable         ///< PECOFF executable file
+    pecoff_executable,        ///< PECOFF executable file
+    windows_resource,         ///< Windows compiled resource file (.rc)
   };
 
   bool is_object() const {
index f04f8c29f666436e21387249df7670ec7017d389..bda32d4154c317081a6c3134d0e8c4f9ea758876 100644 (file)
@@ -553,6 +553,7 @@ ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
     case sys::fs::file_magic::coff_object:
     case sys::fs::file_magic::pecoff_executable:
     case sys::fs::file_magic::macho_universal_binary:
+    case sys::fs::file_magic::windows_resource:
       report_fatal_error("Incompatible object format!");
     }
   } else {
index fd9d3b4bd75fd21fd2f00d22f2ca15bc4b56776f..d0a70091444be889e681defc3521796614f43417 100644 (file)
@@ -100,7 +100,8 @@ error_code object::createBinary(MemoryBuffer *Source,
       return object_error::success;
     }
     case sys::fs::file_magic::unknown:
-    case sys::fs::file_magic::bitcode: {
+    case sys::fs::file_magic::bitcode:
+    case sys::fs::file_magic::windows_resource: {
       // Unrecognized object file format.
       return object_error::invalid_file_type;
     }
index 1d1dafdc8f42c9d93d3973165de7918ddd70c38e..59395c640ff00aae9e59ac1ed478951d724e6041 100644 (file)
@@ -49,6 +49,7 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
   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;
   case sys::fs::file_magic::elf_relocatable:
index 8d707aedded617ce90ad1237b9ebcb398fc66cc8..390614b9a5b95bdfa5b49223a582e675a1f87395 100644 (file)
@@ -847,6 +847,14 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
   if (Magic.size() < 4)
     return file_magic::unknown;
   switch ((unsigned char)Magic[0]) {
+    case 0x00: {
+      // Windows resource file
+      const char Expected[] = "\0\0\0\0\x20\0\0\0\xff";
+      if (Magic.size() >= sizeof(Expected) &&
+          memcmp(Magic.data(), Expected, sizeof(Expected)) == 0)
+        return file_magic::windows_resource;
+      break;
+    }
     case 0xDE:  // 0x0B17C0DE = BC wraper
       if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
           Magic[3] == (char)0x0B)