1 //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/BinaryFormat/Magic.h"
12 #include "llvm/BinaryFormat/COFF.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/BinaryFormat/MachO.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
26 using namespace llvm::support::endian
;
27 using namespace llvm::sys::fs
;
30 static bool startswith(StringRef Magic
, const char (&S
)[N
]) {
31 return Magic
.startswith(StringRef(S
, N
- 1));
34 /// Identify the magic in magic.
35 file_magic
llvm::identify_magic(StringRef Magic
) {
37 return file_magic::unknown
;
38 switch ((unsigned char)Magic
[0]) {
40 // COFF bigobj, CL.exe's LTO object file, or short import library file
41 if (startswith(Magic
, "\0\0\xFF\xFF")) {
43 offsetof(COFF::BigObjHeader
, UUID
) + sizeof(COFF::BigObjMagic
);
44 if (Magic
.size() < MinSize
)
45 return file_magic::coff_import_library
;
47 const char *Start
= Magic
.data() + offsetof(COFF::BigObjHeader
, UUID
);
48 if (memcmp(Start
, COFF::BigObjMagic
, sizeof(COFF::BigObjMagic
)) == 0)
49 return file_magic::coff_object
;
50 if (memcmp(Start
, COFF::ClGlObjMagic
, sizeof(COFF::BigObjMagic
)) == 0)
51 return file_magic::coff_cl_gl_object
;
52 return file_magic::coff_import_library
;
54 // Windows resource file
55 if (Magic
.size() >= sizeof(COFF::WinResMagic
) &&
56 memcmp(Magic
.data(), COFF::WinResMagic
, sizeof(COFF::WinResMagic
)) == 0)
57 return file_magic::windows_resource
;
58 // 0x0000 = COFF unknown machine type
60 return file_magic::coff_object
;
61 if (startswith(Magic
, "\0asm"))
62 return file_magic::wasm_object
;
65 case 0xDE: // 0x0B17C0DE = BC wraper
66 if (startswith(Magic
, "\xDE\xC0\x17\x0B"))
67 return file_magic::bitcode
;
70 if (startswith(Magic
, "BC\xC0\xDE"))
71 return file_magic::bitcode
;
74 if (startswith(Magic
, "!<arch>\n") || startswith(Magic
, "!<thin>\n"))
75 return file_magic::archive
;
79 if (startswith(Magic
, "\177ELF") && Magic
.size() >= 18) {
80 bool Data2MSB
= Magic
[5] == 2;
81 unsigned high
= Data2MSB
? 16 : 17;
82 unsigned low
= Data2MSB
? 17 : 16;
83 if (Magic
[high
] == 0) {
86 return file_magic::elf
;
88 return file_magic::elf_relocatable
;
90 return file_magic::elf_executable
;
92 return file_magic::elf_shared_object
;
94 return file_magic::elf_core
;
97 // It's still some type of ELF file.
98 return file_magic::elf
;
103 if (startswith(Magic
, "\xCA\xFE\xBA\xBE") ||
104 startswith(Magic
, "\xCA\xFE\xBA\xBF")) {
105 // This is complicated by an overlap with Java class files.
106 // See the Mach-O section in /usr/share/file/magic for details.
107 if (Magic
.size() >= 8 && Magic
[7] < 43)
108 return file_magic::macho_universal_binary
;
112 // The two magic numbers for mach-o are:
113 // 0xfeedface - 32-bit mach-o
114 // 0xfeedfacf - 64-bit mach-o
119 if (startswith(Magic
, "\xFE\xED\xFA\xCE") ||
120 startswith(Magic
, "\xFE\xED\xFA\xCF")) {
123 if (Magic
[3] == char(0xCE))
124 MinSize
= sizeof(MachO::mach_header
);
126 MinSize
= sizeof(MachO::mach_header_64
);
127 if (Magic
.size() >= MinSize
)
128 type
= Magic
[12] << 24 | Magic
[13] << 12 | Magic
[14] << 8 | Magic
[15];
129 } else if (startswith(Magic
, "\xCE\xFA\xED\xFE") ||
130 startswith(Magic
, "\xCF\xFA\xED\xFE")) {
133 if (Magic
[0] == char(0xCE))
134 MinSize
= sizeof(MachO::mach_header
);
136 MinSize
= sizeof(MachO::mach_header_64
);
137 if (Magic
.size() >= MinSize
)
138 type
= Magic
[15] << 24 | Magic
[14] << 12 | Magic
[13] << 8 | Magic
[12];
144 return file_magic::macho_object
;
146 return file_magic::macho_executable
;
148 return file_magic::macho_fixed_virtual_memory_shared_lib
;
150 return file_magic::macho_core
;
152 return file_magic::macho_preload_executable
;
154 return file_magic::macho_dynamically_linked_shared_lib
;
156 return file_magic::macho_dynamic_linker
;
158 return file_magic::macho_bundle
;
160 return file_magic::macho_dynamically_linked_shared_lib_stub
;
162 return file_magic::macho_dsym_companion
;
164 return file_magic::macho_kext_bundle
;
168 case 0xF0: // PowerPC Windows
169 case 0x83: // Alpha 32-bit
170 case 0x84: // Alpha 64-bit
171 case 0x66: // MPS R4000 Windows
173 case 0x4c: // 80386 Windows
174 case 0xc4: // ARMNT Windows
175 if (Magic
[1] == 0x01)
176 return file_magic::coff_object
;
179 case 0x90: // PA-RISC Windows
180 case 0x68: // mc68K Windows
181 if (Magic
[1] == 0x02)
182 return file_magic::coff_object
;
185 case 'M': // Possible MS-DOS stub on Windows PE file or MSF/PDB file.
186 if (startswith(Magic
, "MZ") && Magic
.size() >= 0x3c + 4) {
187 uint32_t off
= read32le(Magic
.data() + 0x3c);
188 // PE/COFF file, either EXE or DLL.
189 if (Magic
.substr(off
).startswith(
190 StringRef(COFF::PEMagic
, sizeof(COFF::PEMagic
))))
191 return file_magic::pecoff_executable
;
193 if (Magic
.startswith("Microsoft C/C++ MSF 7.00\r\n"))
194 return file_magic::pdb
;
197 case 0x64: // x86-64 or ARM64 Windows.
198 if (Magic
[1] == char(0x86) || Magic
[1] == char(0xaa))
199 return file_magic::coff_object
;
205 return file_magic::unknown
;
208 std::error_code
llvm::identify_magic(const Twine
&Path
, file_magic
&Result
) {
209 auto FileOrError
= MemoryBuffer::getFile(Path
);
211 return FileOrError
.getError();
213 std::unique_ptr
<MemoryBuffer
> FileBuffer
= std::move(*FileOrError
);
214 Result
= identify_magic(FileBuffer
->getBuffer());
216 return std::error_code();