Magic.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 0x10:
  69. if (startswith(Magic, "\x10\xFF\x10\xAD"))
  70. return file_magic::offload_binary;
  71. break;
  72. case 0xDE: // 0x0B17C0DE = BC wraper
  73. if (startswith(Magic, "\xDE\xC0\x17\x0B"))
  74. return file_magic::bitcode;
  75. break;
  76. case 'B':
  77. if (startswith(Magic, "BC\xC0\xDE"))
  78. return file_magic::bitcode;
  79. break;
  80. case '!':
  81. if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
  82. return file_magic::archive;
  83. break;
  84. case '<':
  85. if (startswith(Magic, "<bigaf>\n"))
  86. return file_magic::archive;
  87. break;
  88. case '\177':
  89. if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
  90. bool Data2MSB = Magic[5] == 2;
  91. unsigned high = Data2MSB ? 16 : 17;
  92. unsigned low = Data2MSB ? 17 : 16;
  93. if (Magic[high] == 0) {
  94. switch (Magic[low]) {
  95. default:
  96. return file_magic::elf;
  97. case 1:
  98. return file_magic::elf_relocatable;
  99. case 2:
  100. return file_magic::elf_executable;
  101. case 3:
  102. return file_magic::elf_shared_object;
  103. case 4:
  104. return file_magic::elf_core;
  105. }
  106. }
  107. // It's still some type of ELF file.
  108. return file_magic::elf;
  109. }
  110. break;
  111. case 0xCA:
  112. if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
  113. startswith(Magic, "\xCA\xFE\xBA\xBF")) {
  114. // This is complicated by an overlap with Java class files.
  115. // See the Mach-O section in /usr/share/file/magic for details.
  116. if (Magic.size() >= 8 && Magic[7] < 43)
  117. return file_magic::macho_universal_binary;
  118. }
  119. break;
  120. // The two magic numbers for mach-o are:
  121. // 0xfeedface - 32-bit mach-o
  122. // 0xfeedfacf - 64-bit mach-o
  123. case 0xFE:
  124. case 0xCE:
  125. case 0xCF: {
  126. uint16_t type = 0;
  127. if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
  128. startswith(Magic, "\xFE\xED\xFA\xCF")) {
  129. /* Native endian */
  130. size_t MinSize;
  131. if (Magic[3] == char(0xCE))
  132. MinSize = sizeof(MachO::mach_header);
  133. else
  134. MinSize = sizeof(MachO::mach_header_64);
  135. if (Magic.size() >= MinSize)
  136. type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
  137. } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
  138. startswith(Magic, "\xCF\xFA\xED\xFE")) {
  139. /* Reverse endian */
  140. size_t MinSize;
  141. if (Magic[0] == char(0xCE))
  142. MinSize = sizeof(MachO::mach_header);
  143. else
  144. MinSize = sizeof(MachO::mach_header_64);
  145. if (Magic.size() >= MinSize)
  146. type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
  147. }
  148. switch (type) {
  149. default:
  150. break;
  151. case 1:
  152. return file_magic::macho_object;
  153. case 2:
  154. return file_magic::macho_executable;
  155. case 3:
  156. return file_magic::macho_fixed_virtual_memory_shared_lib;
  157. case 4:
  158. return file_magic::macho_core;
  159. case 5:
  160. return file_magic::macho_preload_executable;
  161. case 6:
  162. return file_magic::macho_dynamically_linked_shared_lib;
  163. case 7:
  164. return file_magic::macho_dynamic_linker;
  165. case 8:
  166. return file_magic::macho_bundle;
  167. case 9:
  168. return file_magic::macho_dynamically_linked_shared_lib_stub;
  169. case 10:
  170. return file_magic::macho_dsym_companion;
  171. case 11:
  172. return file_magic::macho_kext_bundle;
  173. case 12:
  174. return file_magic::macho_file_set;
  175. }
  176. break;
  177. }
  178. case 0xF0: // PowerPC Windows
  179. case 0x83: // Alpha 32-bit
  180. case 0x84: // Alpha 64-bit
  181. case 0x66: // MPS R4000 Windows
  182. case 0x50: // mc68K
  183. if (startswith(Magic, "\x50\xed\x55\xba"))
  184. return file_magic::cuda_fatbinary;
  185. [[fallthrough]];
  186. case 0x4c: // 80386 Windows
  187. case 0xc4: // ARMNT Windows
  188. if (Magic[1] == 0x01)
  189. return file_magic::coff_object;
  190. [[fallthrough]];
  191. case 0x90: // PA-RISC Windows
  192. case 0x68: // mc68K Windows
  193. if (Magic[1] == 0x02)
  194. return file_magic::coff_object;
  195. break;
  196. case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
  197. // Minidump file.
  198. if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
  199. uint32_t off = read32le(Magic.data() + 0x3c);
  200. // PE/COFF file, either EXE or DLL.
  201. if (Magic.substr(off).startswith(
  202. StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
  203. return file_magic::pecoff_executable;
  204. }
  205. if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
  206. return file_magic::pdb;
  207. if (startswith(Magic, "MDMP"))
  208. return file_magic::minidump;
  209. break;
  210. case 0x64: // x86-64 or ARM64 Windows.
  211. if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
  212. return file_magic::coff_object;
  213. break;
  214. case 0x2d: // YAML '-'
  215. if (startswith(Magic, "--- !tapi") || startswith(Magic, "---\narchs:"))
  216. return file_magic::tapi_file;
  217. break;
  218. case 'D': // DirectX container file - DXBC
  219. if (startswith(Magic, "DXBC"))
  220. return file_magic::dxcontainer_object;
  221. break;
  222. case 0x41: // ARM64EC windows
  223. if (Magic[1] == char(0xA6))
  224. return file_magic::coff_object;
  225. break;
  226. default:
  227. break;
  228. }
  229. return file_magic::unknown;
  230. }
  231. std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
  232. auto FileOrError = MemoryBuffer::getFile(Path, /*IsText=*/false,
  233. /*RequiresNullTerminator=*/false);
  234. if (!FileOrError)
  235. return FileOrError.getError();
  236. std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
  237. Result = identify_magic(FileBuffer->getBuffer());
  238. return std::error_code();
  239. }