Magic.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/BinaryFormat/Magic.h"
  10. #include "llvm/BinaryFormat/COFF.h"
  11. #include "llvm/BinaryFormat/ELF.h"
  12. #include "llvm/BinaryFormat/MachO.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/FileSystem.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. /// @brief 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 (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF"))
  48. return file_magic::windows_resource;
  49. // 0x0000 = COFF unknown machine type
  50. if (Magic[1] == 0)
  51. return file_magic::coff_object;
  52. if (startswith(Magic, "\0asm"))
  53. return file_magic::wasm_object;
  54. break;
  55. }
  56. case 0xDE: // 0x0B17C0DE = BC wraper
  57. if (startswith(Magic, "\xDE\xC0\x17\x0B"))
  58. return file_magic::bitcode;
  59. break;
  60. case 'B':
  61. if (startswith(Magic, "BC\xC0\xDE"))
  62. return file_magic::bitcode;
  63. break;
  64. case '!':
  65. if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
  66. return file_magic::archive;
  67. break;
  68. case '\177':
  69. if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
  70. bool Data2MSB = Magic[5] == 2;
  71. unsigned high = Data2MSB ? 16 : 17;
  72. unsigned low = Data2MSB ? 17 : 16;
  73. if (Magic[high] == 0) {
  74. switch (Magic[low]) {
  75. default:
  76. return file_magic::elf;
  77. case 1:
  78. return file_magic::elf_relocatable;
  79. case 2:
  80. return file_magic::elf_executable;
  81. case 3:
  82. return file_magic::elf_shared_object;
  83. case 4:
  84. return file_magic::elf_core;
  85. }
  86. }
  87. // It's still some type of ELF file.
  88. return file_magic::elf;
  89. }
  90. break;
  91. case 0xCA:
  92. if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
  93. startswith(Magic, "\xCA\xFE\xBA\xBF")) {
  94. // This is complicated by an overlap with Java class files.
  95. // See the Mach-O section in /usr/share/file/magic for details.
  96. if (Magic.size() >= 8 && Magic[7] < 43)
  97. return file_magic::macho_universal_binary;
  98. }
  99. break;
  100. // The two magic numbers for mach-o are:
  101. // 0xfeedface - 32-bit mach-o
  102. // 0xfeedfacf - 64-bit mach-o
  103. case 0xFE:
  104. case 0xCE:
  105. case 0xCF: {
  106. uint16_t type = 0;
  107. if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
  108. startswith(Magic, "\xFE\xED\xFA\xCF")) {
  109. /* Native endian */
  110. size_t MinSize;
  111. if (Magic[3] == char(0xCE))
  112. MinSize = sizeof(MachO::mach_header);
  113. else
  114. MinSize = sizeof(MachO::mach_header_64);
  115. if (Magic.size() >= MinSize)
  116. type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
  117. } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
  118. startswith(Magic, "\xCF\xFA\xED\xFE")) {
  119. /* Reverse endian */
  120. size_t MinSize;
  121. if (Magic[0] == char(0xCE))
  122. MinSize = sizeof(MachO::mach_header);
  123. else
  124. MinSize = sizeof(MachO::mach_header_64);
  125. if (Magic.size() >= MinSize)
  126. type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
  127. }
  128. switch (type) {
  129. default:
  130. break;
  131. case 1:
  132. return file_magic::macho_object;
  133. case 2:
  134. return file_magic::macho_executable;
  135. case 3:
  136. return file_magic::macho_fixed_virtual_memory_shared_lib;
  137. case 4:
  138. return file_magic::macho_core;
  139. case 5:
  140. return file_magic::macho_preload_executable;
  141. case 6:
  142. return file_magic::macho_dynamically_linked_shared_lib;
  143. case 7:
  144. return file_magic::macho_dynamic_linker;
  145. case 8:
  146. return file_magic::macho_bundle;
  147. case 9:
  148. return file_magic::macho_dynamically_linked_shared_lib_stub;
  149. case 10:
  150. return file_magic::macho_dsym_companion;
  151. case 11:
  152. return file_magic::macho_kext_bundle;
  153. }
  154. break;
  155. }
  156. case 0xF0: // PowerPC Windows
  157. case 0x83: // Alpha 32-bit
  158. case 0x84: // Alpha 64-bit
  159. case 0x66: // MPS R4000 Windows
  160. case 0x50: // mc68K
  161. case 0x4c: // 80386 Windows
  162. case 0xc4: // ARMNT Windows
  163. if (Magic[1] == 0x01)
  164. return file_magic::coff_object;
  165. LLVM_FALLTHROUGH;
  166. case 0x90: // PA-RISC Windows
  167. case 0x68: // mc68K Windows
  168. if (Magic[1] == 0x02)
  169. return file_magic::coff_object;
  170. break;
  171. case 'M': // Possible MS-DOS stub on Windows PE file
  172. if (startswith(Magic, "MZ")) {
  173. uint32_t off = read32le(Magic.data() + 0x3c);
  174. // PE/COFF file, either EXE or DLL.
  175. if (off < Magic.size() &&
  176. memcmp(Magic.data() + off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
  177. return file_magic::pecoff_executable;
  178. }
  179. break;
  180. case 0x64: // x86-64 or ARM64 Windows.
  181. if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
  182. return file_magic::coff_object;
  183. break;
  184. default:
  185. break;
  186. }
  187. return file_magic::unknown;
  188. }
  189. std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
  190. int FD;
  191. if (std::error_code EC = openFileForRead(Path, FD))
  192. return EC;
  193. char Buffer[32];
  194. int Length = read(FD, Buffer, sizeof(Buffer));
  195. if (close(FD) != 0 || Length < 0)
  196. return std::error_code(errno, std::generic_category());
  197. Result = identify_magic(StringRef(Buffer, Length));
  198. return std::error_code();
  199. }