llvm-project
291 строка · 8.7 Кб
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
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"
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]) {
29return Magic.starts_with(StringRef(S, N - 1));
30}
31
32/// Identify the magic in magic.
33file_magic llvm::identify_magic(StringRef Magic) {
34if (Magic.size() < 4)
35return file_magic::unknown;
36switch ((unsigned char)Magic[0]) {
37case 0x00: {
38// COFF bigobj, CL.exe's LTO object file, or short import library file
39if (startswith(Magic, "\0\0\xFF\xFF")) {
40size_t MinSize =
41offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
42if (Magic.size() < MinSize)
43return file_magic::coff_import_library;
44
45const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
46if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
47return file_magic::coff_object;
48if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
49return file_magic::coff_cl_gl_object;
50return file_magic::coff_import_library;
51}
52// Windows resource file
53if (Magic.size() >= sizeof(COFF::WinResMagic) &&
54memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
55return file_magic::windows_resource;
56// 0x0000 = COFF unknown machine type
57if (Magic[1] == 0)
58return file_magic::coff_object;
59if (startswith(Magic, "\0asm"))
60return file_magic::wasm_object;
61break;
62}
63
64case 0x01:
65// XCOFF format
66if (startswith(Magic, "\x01\xDF"))
67return file_magic::xcoff_object_32;
68if (startswith(Magic, "\x01\xF7"))
69return file_magic::xcoff_object_64;
70break;
71
72case 0x03:
73if (startswith(Magic, "\x03\xF0\x00"))
74return file_magic::goff_object;
75// SPIR-V format in little-endian mode.
76if (startswith(Magic, "\x03\x02\x23\x07"))
77return file_magic::spirv_object;
78break;
79
80case 0x07: // SPIR-V format in big-endian mode.
81if (startswith(Magic, "\x07\x23\x02\x03"))
82return file_magic::spirv_object;
83break;
84
85case 0x10:
86if (startswith(Magic, "\x10\xFF\x10\xAD"))
87return file_magic::offload_binary;
88break;
89
90case 0xDE: // 0x0B17C0DE = BC wraper
91if (startswith(Magic, "\xDE\xC0\x17\x0B"))
92return file_magic::bitcode;
93break;
94case 'B':
95if (startswith(Magic, "BC\xC0\xDE"))
96return file_magic::bitcode;
97break;
98case 'C':
99if (startswith(Magic, "CCOB"))
100return file_magic::offload_bundle_compressed;
101if (startswith(Magic, "CPCH"))
102return file_magic::clang_ast;
103break;
104case '!':
105if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
106return file_magic::archive;
107break;
108case '<':
109if (startswith(Magic, "<bigaf>\n"))
110return file_magic::archive;
111break;
112case '\177':
113if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
114bool Data2MSB = Magic[5] == 2;
115unsigned high = Data2MSB ? 16 : 17;
116unsigned low = Data2MSB ? 17 : 16;
117if (Magic[high] == 0) {
118switch (Magic[low]) {
119default:
120return file_magic::elf;
121case 1:
122return file_magic::elf_relocatable;
123case 2:
124return file_magic::elf_executable;
125case 3:
126return file_magic::elf_shared_object;
127case 4:
128return file_magic::elf_core;
129}
130}
131// It's still some type of ELF file.
132return file_magic::elf;
133}
134break;
135
136case 0xCA:
137if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
138startswith(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.
141if (Magic.size() >= 8 && Magic[7] < 43)
142return file_magic::macho_universal_binary;
143}
144break;
145
146// The two magic numbers for mach-o are:
147// 0xfeedface - 32-bit mach-o
148// 0xfeedfacf - 64-bit mach-o
149case 0xFE:
150case 0xCE:
151case 0xCF: {
152uint16_t type = 0;
153if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
154startswith(Magic, "\xFE\xED\xFA\xCF")) {
155/* Native endian */
156size_t MinSize;
157if (Magic[3] == char(0xCE))
158MinSize = sizeof(MachO::mach_header);
159else
160MinSize = sizeof(MachO::mach_header_64);
161if (Magic.size() >= MinSize)
162type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
163} else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
164startswith(Magic, "\xCF\xFA\xED\xFE")) {
165/* Reverse endian */
166size_t MinSize;
167if (Magic[0] == char(0xCE))
168MinSize = sizeof(MachO::mach_header);
169else
170MinSize = sizeof(MachO::mach_header_64);
171if (Magic.size() >= MinSize)
172type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
173}
174switch (type) {
175default:
176break;
177case 1:
178return file_magic::macho_object;
179case 2:
180return file_magic::macho_executable;
181case 3:
182return file_magic::macho_fixed_virtual_memory_shared_lib;
183case 4:
184return file_magic::macho_core;
185case 5:
186return file_magic::macho_preload_executable;
187case 6:
188return file_magic::macho_dynamically_linked_shared_lib;
189case 7:
190return file_magic::macho_dynamic_linker;
191case 8:
192return file_magic::macho_bundle;
193case 9:
194return file_magic::macho_dynamically_linked_shared_lib_stub;
195case 10:
196return file_magic::macho_dsym_companion;
197case 11:
198return file_magic::macho_kext_bundle;
199case 12:
200return file_magic::macho_file_set;
201}
202break;
203}
204case 0xF0: // PowerPC Windows
205case 0x83: // Alpha 32-bit
206case 0x84: // Alpha 64-bit
207case 0x66: // MPS R4000 Windows
208case 0x50: // mc68K
209if (startswith(Magic, "\x50\xed\x55\xba"))
210return file_magic::cuda_fatbinary;
211[[fallthrough]];
212
213case 0x4c: // 80386 Windows
214case 0xc4: // ARMNT Windows
215if (Magic[1] == 0x01)
216return file_magic::coff_object;
217[[fallthrough]];
218
219case 0x90: // PA-RISC Windows
220case 0x68: // mc68K Windows
221if (Magic[1] == 0x02)
222return file_magic::coff_object;
223break;
224
225case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
226// Minidump file.
227if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
228uint32_t off = read32le(Magic.data() + 0x3c);
229// PE/COFF file, either EXE or DLL.
230if (Magic.substr(off).starts_with(
231StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
232return file_magic::pecoff_executable;
233}
234if (Magic.starts_with("Microsoft C/C++ MSF 7.00\r\n"))
235return file_magic::pdb;
236if (startswith(Magic, "MDMP"))
237return file_magic::minidump;
238break;
239
240case 0x64: // x86-64 or ARM64 Windows.
241if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
242return file_magic::coff_object;
243break;
244
245case 0x2d: // YAML '-' MachO TBD.
246if (startswith(Magic, "--- !tapi") || startswith(Magic, "---\narchs:"))
247return file_magic::tapi_file;
248break;
249case 0x7b: // JSON '{' MachO TBD.
250return file_magic::tapi_file;
251break;
252
253case 'D': // DirectX container file - DXBC
254if (startswith(Magic, "DXBC"))
255return file_magic::dxcontainer_object;
256break;
257
258case 0x41: // ARM64EC windows
259if (Magic[1] == char(0xA6))
260return file_magic::coff_object;
261break;
262
263case 0x4e: // ARM64X windows
264if (Magic[1] == char(0xA6))
265return file_magic::coff_object;
266break;
267
268case '_': {
269const char OBMagic[] = "__CLANG_OFFLOAD_BUNDLE__";
270if (Magic.size() >= sizeof(OBMagic) && startswith(Magic, OBMagic))
271return file_magic::offload_bundle;
272break;
273}
274
275default:
276break;
277}
278return file_magic::unknown;
279}
280
281std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
282auto FileOrError = MemoryBuffer::getFile(Path, /*IsText=*/false,
283/*RequiresNullTerminator=*/false);
284if (!FileOrError)
285return FileOrError.getError();
286
287std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
288Result = identify_magic(FileBuffer->getBuffer());
289
290return std::error_code();
291}
292