Magic.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/BinaryFormat/Magic.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/Twine.h"
  11. #include "llvm/BinaryFormat/COFF.h"
  12. #include "llvm/BinaryFormat/MachO.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  16. #include <unistd.h>
  17. #else
  18. #include <io.h>
  19. #endif
  20. using namespace llvm;
  21. using namespace llvm::support::endian;
  22. using namespace llvm::sys::fs;
  23. template <size_t N>
  24. static bool startswith(StringRef Magic, const char (&S)[N]) {
  25. return Magic.startswith(StringRef(S, N - 1));
  26. }
  27. /// Identify the magic in magic.
  28. file_magic llvm::identify_magic(StringRef Magic) {
  29. if (Magic.size() < 4)
  30. return file_magic::unknown;
  31. switch ((unsigned char)Magic[0]) {
  32. case 0x00: {
  33. // COFF bigobj, CL.exe's LTO object file, or short import library file
  34. if (startswith(Magic, "\0\0\xFF\xFF")) {
  35. size_t MinSize =
  36. offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
  37. if (Magic.size() < MinSize)
  38. return file_magic::coff_import_library;
  39. const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
  40. if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
  41. return file_magic::coff_object;
  42. if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
  43. return file_magic::coff_cl_gl_object;
  44. return file_magic::coff_import_library;
  45. }
  46. // Windows resource file
  47. if (Magic.size() >= sizeof(COFF::WinResMagic) &&
  48. memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
  49. return file_magic::windows_resource;
  50. // 0x0000 = COFF unknown machine type
  51. if (Magic[1] == 0)
  52. return file_magic::coff_object;
  53. if (startswith(Magic, "\0asm"))
  54. return file_magic::wasm_object;
  55. break;
  56. }
  57. case 0x01:
  58. // XCOFF format
  59. if (startswith(Magic, "\x01\xDF"))
  60. return file_magic::xcoff_object_32;
  61. if (startswith(Magic, "\x01\xF7"))
  62. return file_magic::xcoff_object_64;
  63. break;
  64. case 0x03:
  65. if (startswith(Magic, "\x03\xF0\x00"))
  66. return file_magic::goff_object;
  67. break;
  68. case 0xDE: // 0x0B17C0DE = BC wraper
  69. if (startswith(Magic, "\xDE\xC0\x17\x0B"))
  70. return file_magic::bitcode;
  71. break;
  72. case 'B':
  73. if (startswith(Magic, "BC\xC0\xDE"))
  74. return file_magic::bitcode;
  75. break;
  76. case '!':
  77. if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
  78. return file_magic::archive;
  79. break;
  80. case '<':
  81. if (startswith(Magic, "<bigaf>\n"))
  82. return file_magic::archive;
  83. break;
  84. case '\177':
  85. if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
  86. bool Data2MSB = Magic[5] == 2;
  87. unsigned high = Data2MSB ? 16 : 17;
  88. unsigned low = Data2MSB ? 17 : 16;
  89. if (Magic[high] == 0) {
  90. switch (Magic[low]) {
  91. default:
  92. return file_magic::elf;
  93. case 1:
  94. return file_magic::elf_relocatable;
  95. case 2:
  96. return file_magic::elf_executable;
  97. case 3:
  98. return file_magic::elf_shared_object;
  99. case 4:
  100. return file_magic::elf_core;
  101. }
  102. }
  103. // It's still some type of ELF file.
  104. return file_magic::elf;
  105. }
  106. break;
  107. case 0xCA:
  108. if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
  109. startswith(Magic, "\xCA\xFE\xBA\xBF")) {
  110. // This is complicated by an overlap with Java class files.
  111. // See the Mach-O section in /usr/share/file/magic for details.
  112. if (Magic.size() >= 8 && Magic[7] < 43)
  113. return file_magic::macho_universal_binary;
  114. }
  115. break;
  116. // The two magic numbers for mach-o are:
  117. // 0xfeedface - 32-bit mach-o
  118. // 0xfeedfacf - 64-bit mach-o
  119. case 0xFE:
  120. case 0xCE:
  121. case 0xCF: {
  122. uint16_t type = 0;
  123. if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
  124. startswith(Magic, "\xFE\xED\xFA\xCF")) {
  125. /* Native endian */
  126. size_t MinSize;
  127. if (Magic[3] == char(0xCE))
  128. MinSize = sizeof(MachO::mach_header);
  129. else
  130. MinSize = sizeof(MachO::mach_header_64);
  131. if (Magic.size() >= MinSize)
  132. type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
  133. } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
  134. startswith(Magic, "\xCF\xFA\xED\xFE")) {
  135. /* Reverse endian */
  136. size_t MinSize;
  137. if (Magic[0] == char(0xCE))
  138. MinSize = sizeof(MachO::mach_header);
  139. else
  140. MinSize = sizeof(MachO::mach_header_64);
  141. if (Magic.size() >= MinSize)
  142. type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
  143. }
  144. switch (type) {
  145. default:
  146. break;
  147. case 1:
  148. return file_magic::macho_object;
  149. case 2:
  150. return file_magic::macho_executable;
  151. case 3:
  152. return file_magic::macho_fixed_virtual_memory_shared_lib;
  153. case 4:
  154. return file_magic::macho_core;
  155. case 5:
  156. return file_magic::macho_preload_executable;
  157. case 6:
  158. return file_magic::macho_dynamically_linked_shared_lib;
  159. case 7:
  160. return file_magic::macho_dynamic_linker;
  161. case 8:
  162. return file_magic::macho_bundle;
  163. case 9:
  164. return file_magic::macho_dynamically_linked_shared_lib_stub;
  165. case 10:
  166. return file_magic::macho_dsym_companion;
  167. case 11:
  168. return file_magic::macho_kext_bundle;
  169. }
  170. break;
  171. }
  172. case 0xF0: // PowerPC Windows
  173. case 0x83: // Alpha 32-bit
  174. case 0x84: // Alpha 64-bit
  175. case 0x66: // MPS R4000 Windows
  176. case 0x50: // mc68K
  177. case 0x4c: // 80386 Windows
  178. case 0xc4: // ARMNT Windows
  179. if (Magic[1] == 0x01)
  180. return file_magic::coff_object;
  181. LLVM_FALLTHROUGH;
  182. case 0x90: // PA-RISC Windows
  183. case 0x68: // mc68K Windows
  184. if (Magic[1] == 0x02)
  185. return file_magic::coff_object;
  186. break;
  187. case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
  188. // Minidump file.
  189. if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
  190. uint32_t off = read32le(Magic.data() + 0x3c);
  191. // PE/COFF file, either EXE or DLL.
  192. if (Magic.substr(off).startswith(
  193. StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
  194. return file_magic::pecoff_executable;
  195. }
  196. if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
  197. return file_magic::pdb;
  198. if (startswith(Magic, "MDMP"))
  199. return file_magic::minidump;
  200. break;
  201. case 0x64: // x86-64 or ARM64 Windows.
  202. if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
  203. return file_magic::coff_object;
  204. break;
  205. case 0x2d: // YAML '-'
  206. if (startswith(Magic, "--- !tapi") || startswith(Magic, "---\narchs:"))
  207. return file_magic::tapi_file;
  208. break;
  209. default:
  210. break;
  211. }
  212. return file_magic::unknown;
  213. }
  214. std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
  215. auto FileOrError = MemoryBuffer::getFile(Path, /*IsText=*/false,
  216. /*RequiresNullTerminator=*/false);
  217. if (!FileOrError)
  218. return FileOrError.getError();
  219. std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
  220. Result = identify_magic(FileBuffer->getBuffer());
  221. return std::error_code();
  222. }