LLVM 20.0.0git
Magic.cpp
Go to the documentation of this file.
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
10#include "llvm/ADT/StringRef.h"
11#include "llvm/ADT/Twine.h"
14#include "llvm/Support/Endian.h"
16
17#if !defined(_MSC_VER) && !defined(__MINGW32__)
18#include <unistd.h>
19#else
20#include <io.h>
21#endif
22
23using namespace llvm;
24using namespace llvm::support::endian;
25using namespace llvm::sys::fs;
26
27template <size_t N>
28static bool startswith(StringRef Magic, const char (&S)[N]) {
29 return Magic.starts_with(StringRef(S, N - 1));
30}
31
32/// Identify the magic in magic.
34 if (Magic.size() < 4)
36 switch ((unsigned char)Magic[0]) {
37 case 0x00: {
38 // COFF bigobj, CL.exe's LTO object file, or short import library file
39 if (startswith(Magic, "\0\0\xFF\xFF")) {
40 size_t MinSize =
42 if (Magic.size() < MinSize)
44
45 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
46 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
48 if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
51 }
52 // Windows resource file
53 if (Magic.size() >= sizeof(COFF::WinResMagic) &&
54 memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
56 // 0x0000 = COFF unknown machine type
57 if (Magic[1] == 0)
59 if (startswith(Magic, "\0asm"))
61 break;
62 }
63
64 case 0x01:
65 // XCOFF format
66 if (startswith(Magic, "\x01\xDF"))
68 if (startswith(Magic, "\x01\xF7"))
70 break;
71
72 case 0x03:
73 if (startswith(Magic, "\x03\xF0\x00"))
75 // SPIR-V format in little-endian mode.
76 if (startswith(Magic, "\x03\x02\x23\x07"))
78 break;
79
80 case 0x07: // SPIR-V format in big-endian mode.
81 if (startswith(Magic, "\x07\x23\x02\x03"))
83 break;
84
85 case 0x10:
86 if (startswith(Magic, "\x10\xFF\x10\xAD"))
88 break;
89
90 case 0xDE: // 0x0B17C0DE = BC wraper
91 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
93 break;
94 case 'B':
95 if (startswith(Magic, "BC\xC0\xDE"))
97 break;
98 case 'C':
99 if (startswith(Magic, "CCOB"))
101 if (startswith(Magic, "CPCH"))
103 break;
104 case '!':
105 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
106 return file_magic::archive;
107 break;
108 case '<':
109 if (startswith(Magic, "<bigaf>\n"))
110 return file_magic::archive;
111 break;
112 case '\177':
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]) {
119 default:
120 return file_magic::elf;
121 case 1:
123 case 2:
125 case 3:
127 case 4:
129 }
130 }
131 // It's still some type of ELF file.
132 return file_magic::elf;
133 }
134 break;
135
136 case 0xCA:
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)
143 }
144 break;
145
146 // The two magic numbers for mach-o are:
147 // 0xfeedface - 32-bit mach-o
148 // 0xfeedfacf - 64-bit mach-o
149 case 0xFE:
150 case 0xCE:
151 case 0xCF: {
152 uint16_t type = 0;
153 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
154 startswith(Magic, "\xFE\xED\xFA\xCF")) {
155 /* Native endian */
156 size_t MinSize;
157 if (Magic[3] == char(0xCE))
158 MinSize = sizeof(MachO::mach_header);
159 else
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")) {
165 /* Reverse endian */
166 size_t MinSize;
167 if (Magic[0] == char(0xCE))
168 MinSize = sizeof(MachO::mach_header);
169 else
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];
173 }
174 switch (type) {
175 default:
176 break;
177 case 1:
179 case 2:
181 case 3:
183 case 4:
185 case 5:
187 case 6:
189 case 7:
191 case 8:
193 case 9:
195 case 10:
197 case 11:
199 case 12:
201 }
202 break;
203 }
204 case 0xF0: // PowerPC Windows
205 case 0x83: // Alpha 32-bit
206 case 0x84: // Alpha 64-bit
207 case 0x66: // MPS R4000 Windows
208 case 0x50: // mc68K
209 if (startswith(Magic, "\x50\xed\x55\xba"))
211 [[fallthrough]];
212
213 case 0x4c: // 80386 Windows
214 case 0xc4: // ARMNT Windows
215 if (Magic[1] == 0x01)
217 [[fallthrough]];
218
219 case 0x90: // PA-RISC Windows
220 case 0x68: // mc68K Windows
221 if (Magic[1] == 0x02)
223 break;
224
225 case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
226 // Minidump file.
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(
233 }
234 if (Magic.starts_with("Microsoft C/C++ MSF 7.00\r\n"))
235 return file_magic::pdb;
236 if (startswith(Magic, "MDMP"))
238 break;
239
240 case 0x64: // x86-64 or ARM64 Windows.
241 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
243 break;
244
245 case 0x2d: // YAML '-' MachO TBD.
246 if (startswith(Magic, "--- !tapi") || startswith(Magic, "---\narchs:"))
248 break;
249 case 0x7b: // JSON '{' MachO TBD.
251 break;
252
253 case 'D': // DirectX container file - DXBC
254 if (startswith(Magic, "DXBC"))
256 break;
257
258 case 0x41: // ARM64EC windows
259 if (Magic[1] == char(0xA6))
261 break;
262
263 case 0x4e: // ARM64X windows
264 if (Magic[1] == char(0xA6))
266 break;
267
268 case '_': {
269 const char OBMagic[] = "__CLANG_OFFLOAD_BUNDLE__";
270 if (Magic.size() >= sizeof(OBMagic) && startswith(Magic, OBMagic))
272 break;
273 }
274
275 default:
276 break;
277 }
278 return file_magic::unknown;
279}
280
281std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
282 auto FileOrError = MemoryBuffer::getFile(Path, /*IsText=*/false,
283 /*RequiresNullTerminator=*/false);
284 if (!FileOrError)
285 return FileOrError.getError();
286
287 std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
288 Result = identify_magic(FileBuffer->getBuffer());
289
290 return std::error_code();
291}
#define offsetof(TYPE, MEMBER)
static bool startswith(StringRef Magic, const char(&S)[N])
Definition: Magic.cpp:28
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:911
std::pair< llvm::MachO::Target, std::string > UUID
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static const char ClGlObjMagic[]
Definition: COFF.h:42
static const char WinResMagic[]
Definition: COFF.h:48
static const char BigObjMagic[]
Definition: COFF.h:37
static const char PEMagic[]
Definition: COFF.h:35
uint32_t read32le(const void *P)
Definition: Endian.h:425
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
#define N
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
@ coff_import_library
COFF import library.
Definition: Magic.h:48
@ pdb
Windows PDB debug info file.
Definition: Magic.h:54
@ spirv_object
A binary SPIR-V file.
Definition: Magic.h:61
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:27
@ archive
ar style archive file
Definition: Magic.h:25
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:29
@ goff_object
GOFF object file.
Definition: Magic.h:31
@ minidump
Windows minidump file.
Definition: Magic.h:45
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:37
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:52
@ elf_executable
ELF Executable image.
Definition: Magic.h:28
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:40
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:36
@ offload_bundle
Clang offload bundle file.
Definition: Magic.h:59
@ offload_bundle_compressed
Compressed clang offload bundle file.
Definition: Magic.h:60
@ macho_file_set
Mach-O file set binary.
Definition: Magic.h:44
@ dxcontainer_object
DirectX container file.
Definition: Magic.h:58
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:42
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:49
@ offload_binary
LLVM offload object file.
Definition: Magic.h:57
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:43
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ macho_core
Mach-O Core File.
Definition: Magic.h:35
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:53
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:51
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:50
@ clang_ast
Clang PCH or PCM.
Definition: Magic.h:24
@ elf_core
ELF core image.
Definition: Magic.h:30
@ macho_object
Mach-O Object file.
Definition: Magic.h:32
@ coff_object
COFF object file.
Definition: Magic.h:47
@ elf
ELF Unknown type.
Definition: Magic.h:26
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:39
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:46
@ cuda_fatbinary
CUDA Fatbinary object file.
Definition: Magic.h:56
@ macho_executable
Mach-O Executable.
Definition: Magic.h:33
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:41
@ unknown
Unrecognized file.
Definition: Magic.h:22
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:34
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:38
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:55