1 //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/Magic.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/BinaryFormat/COFF.h"
13 #include "llvm/BinaryFormat/MachO.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/MemoryBuffer.h"
17 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 using namespace llvm::support::endian
;
25 using namespace llvm::sys::fs
;
28 static bool startswith(StringRef Magic
, const char (&S
)[N
]) {
29 return Magic
.starts_with(StringRef(S
, N
- 1));
32 /// Identify the magic in magic.
33 file_magic
llvm::identify_magic(StringRef Magic
) {
35 return file_magic::unknown
;
36 switch ((unsigned char)Magic
[0]) {
38 // COFF bigobj, CL.exe's LTO object file, or short import library file
39 if (startswith(Magic
, "\0\0\xFF\xFF")) {
41 offsetof(COFF::BigObjHeader
, UUID
) + sizeof(COFF::BigObjMagic
);
42 if (Magic
.size() < MinSize
)
43 return file_magic::coff_import_library
;
45 const char *Start
= Magic
.data() + offsetof(COFF::BigObjHeader
, UUID
);
46 if (memcmp(Start
, COFF::BigObjMagic
, sizeof(COFF::BigObjMagic
)) == 0)
47 return file_magic::coff_object
;
48 if (memcmp(Start
, COFF::ClGlObjMagic
, sizeof(COFF::BigObjMagic
)) == 0)
49 return file_magic::coff_cl_gl_object
;
50 return file_magic::coff_import_library
;
52 // Windows resource file
53 if (Magic
.size() >= sizeof(COFF::WinResMagic
) &&
54 memcmp(Magic
.data(), COFF::WinResMagic
, sizeof(COFF::WinResMagic
)) == 0)
55 return file_magic::windows_resource
;
56 // 0x0000 = COFF unknown machine type
58 return file_magic::coff_object
;
59 if (startswith(Magic
, "\0asm"))
60 return file_magic::wasm_object
;
66 if (startswith(Magic
, "\x01\xDF"))
67 return file_magic::xcoff_object_32
;
68 if (startswith(Magic
, "\x01\xF7"))
69 return file_magic::xcoff_object_64
;
73 if (startswith(Magic
, "\x03\xF0\x00"))
74 return file_magic::goff_object
;
75 // SPIR-V format in little-endian mode.
76 if (startswith(Magic
, "\x03\x02\x23\x07"))
77 return file_magic::spirv_object
;
80 case 0x07: // SPIR-V format in big-endian mode.
81 if (startswith(Magic
, "\x07\x23\x02\x03"))
82 return file_magic::spirv_object
;
86 if (startswith(Magic
, "\x10\xFF\x10\xAD"))
87 return file_magic::offload_binary
;
90 case 0xDE: // 0x0B17C0DE = BC wraper
91 if (startswith(Magic
, "\xDE\xC0\x17\x0B"))
92 return file_magic::bitcode
;
95 if (startswith(Magic
, "BC\xC0\xDE"))
96 return file_magic::bitcode
;
99 if (startswith(Magic
, "CCOB"))
100 return file_magic::offload_bundle_compressed
;
101 if (startswith(Magic
, "CPCH"))
102 return file_magic::clang_ast
;
105 if (startswith(Magic
, "!<arch>\n") || startswith(Magic
, "!<thin>\n"))
106 return file_magic::archive
;
109 if (startswith(Magic
, "<bigaf>\n"))
110 return file_magic::archive
;
113 if (startswith(Magic
, "\177ELF") && Magic
.size() >= 18) {
114 bool Data2MSB
= Magic
[5] == 2;
115 unsigned high
= Data2MSB
? 16 : 17;
116 unsigned low
= Data2MSB
? 17 : 16;
117 if (Magic
[high
] == 0) {
118 switch (Magic
[low
]) {
120 return file_magic::elf
;
122 return file_magic::elf_relocatable
;
124 return file_magic::elf_executable
;
126 return file_magic::elf_shared_object
;
128 return file_magic::elf_core
;
131 // It's still some type of ELF file.
132 return file_magic::elf
;
137 if (startswith(Magic
, "\xCA\xFE\xBA\xBE") ||
138 startswith(Magic
, "\xCA\xFE\xBA\xBF")) {
139 // This is complicated by an overlap with Java class files.
140 // See the Mach-O section in /usr/share/file/magic for details.
141 if (Magic
.size() >= 8 && Magic
[7] < 43)
142 return file_magic::macho_universal_binary
;
146 // The two magic numbers for mach-o are:
147 // 0xfeedface - 32-bit mach-o
148 // 0xfeedfacf - 64-bit mach-o
153 if (startswith(Magic
, "\xFE\xED\xFA\xCE") ||
154 startswith(Magic
, "\xFE\xED\xFA\xCF")) {
157 if (Magic
[3] == char(0xCE))
158 MinSize
= sizeof(MachO::mach_header
);
160 MinSize
= sizeof(MachO::mach_header_64
);
161 if (Magic
.size() >= MinSize
)
162 type
= Magic
[12] << 24 | Magic
[13] << 12 | Magic
[14] << 8 | Magic
[15];
163 } else if (startswith(Magic
, "\xCE\xFA\xED\xFE") ||
164 startswith(Magic
, "\xCF\xFA\xED\xFE")) {
167 if (Magic
[0] == char(0xCE))
168 MinSize
= sizeof(MachO::mach_header
);
170 MinSize
= sizeof(MachO::mach_header_64
);
171 if (Magic
.size() >= MinSize
)
172 type
= Magic
[15] << 24 | Magic
[14] << 12 | Magic
[13] << 8 | Magic
[12];
178 return file_magic::macho_object
;
180 return file_magic::macho_executable
;
182 return file_magic::macho_fixed_virtual_memory_shared_lib
;
184 return file_magic::macho_core
;
186 return file_magic::macho_preload_executable
;
188 return file_magic::macho_dynamically_linked_shared_lib
;
190 return file_magic::macho_dynamic_linker
;
192 return file_magic::macho_bundle
;
194 return file_magic::macho_dynamically_linked_shared_lib_stub
;
196 return file_magic::macho_dsym_companion
;
198 return file_magic::macho_kext_bundle
;
200 return file_magic::macho_file_set
;
204 case 0xF0: // PowerPC Windows
205 case 0x83: // Alpha 32-bit
206 case 0x84: // Alpha 64-bit
207 case 0x66: // MPS R4000 Windows
209 if (startswith(Magic
, "\x50\xed\x55\xba"))
210 return file_magic::cuda_fatbinary
;
213 case 0x4c: // 80386 Windows
214 case 0xc4: // ARMNT Windows
215 if (Magic
[1] == 0x01)
216 return file_magic::coff_object
;
219 case 0x90: // PA-RISC Windows
220 case 0x68: // mc68K Windows
221 if (Magic
[1] == 0x02)
222 return file_magic::coff_object
;
225 case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
227 if (startswith(Magic
, "MZ") && Magic
.size() >= 0x3c + 4) {
228 uint32_t off
= read32le(Magic
.data() + 0x3c);
229 // PE/COFF file, either EXE or DLL.
230 if (Magic
.substr(off
).starts_with(
231 StringRef(COFF::PEMagic
, sizeof(COFF::PEMagic
))))
232 return file_magic::pecoff_executable
;
234 if (Magic
.starts_with("Microsoft C/C++ MSF 7.00\r\n"))
235 return file_magic::pdb
;
236 if (startswith(Magic
, "MDMP"))
237 return file_magic::minidump
;
240 case 0x64: // x86-64 or ARM64 Windows.
241 if (Magic
[1] == char(0x86) || Magic
[1] == char(0xaa))
242 return file_magic::coff_object
;
245 case 0x2d: // YAML '-' MachO TBD.
246 if (startswith(Magic
, "--- !tapi") || startswith(Magic
, "---\narchs:"))
247 return file_magic::tapi_file
;
249 case 0x7b: // JSON '{' MachO TBD.
250 return file_magic::tapi_file
;
253 case 'D': // DirectX container file - DXBC
254 if (startswith(Magic
, "DXBC"))
255 return file_magic::dxcontainer_object
;
258 case 0x41: // ARM64EC windows
259 if (Magic
[1] == char(0xA6))
260 return file_magic::coff_object
;
263 case 0x4e: // ARM64X windows
264 if (Magic
[1] == char(0xA6))
265 return file_magic::coff_object
;
269 const char OBMagic
[] = "__CLANG_OFFLOAD_BUNDLE__";
270 if (Magic
.size() >= sizeof(OBMagic
) && startswith(Magic
, OBMagic
))
271 return file_magic::offload_bundle
;
278 return file_magic::unknown
;
281 std::error_code
llvm::identify_magic(const Twine
&Path
, file_magic
&Result
) {
282 auto FileOrError
= MemoryBuffer::getFile(Path
, /*IsText=*/false,
283 /*RequiresNullTerminator=*/false);
285 return FileOrError
.getError();
287 std::unique_ptr
<MemoryBuffer
> FileBuffer
= std::move(*FileOrError
);
288 Result
= identify_magic(FileBuffer
->getBuffer());
290 return std::error_code();