blob: 6063187a546f1ec98c5156e2a7dbd843082fe30a [file] [log] [blame]
Zachary Turner264b5d92017-06-07 03:48:56 +00001//===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner264b5d92017-06-07 03:48:56 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/BinaryFormat/Magic.h"
10
11#include "llvm/BinaryFormat/COFF.h"
12#include "llvm/BinaryFormat/ELF.h"
13#include "llvm/BinaryFormat/MachO.h"
14#include "llvm/Support/Endian.h"
15#include "llvm/Support/FileSystem.h"
Zachary Turner9899b5f2018-03-08 19:45:20 +000016#include "llvm/Support/MemoryBuffer.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017
18#if !defined(_MSC_VER) && !defined(__MINGW32__)
19#include <unistd.h>
20#else
21#include <io.h>
22#endif
23
24using namespace llvm;
25using namespace llvm::support::endian;
26using namespace llvm::sys::fs;
27
28template <size_t N>
29static bool startswith(StringRef Magic, const char (&S)[N]) {
30 return Magic.startswith(StringRef(S, N - 1));
31}
32
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +000033/// Identify the magic in magic.
Zachary Turner264b5d92017-06-07 03:48:56 +000034file_magic llvm::identify_magic(StringRef Magic) {
35 if (Magic.size() < 4)
36 return file_magic::unknown;
37 switch ((unsigned char)Magic[0]) {
38 case 0x00: {
39 // COFF bigobj, CL.exe's LTO object file, or short import library file
40 if (startswith(Magic, "\0\0\xFF\xFF")) {
41 size_t MinSize =
42 offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
43 if (Magic.size() < MinSize)
44 return file_magic::coff_import_library;
45
46 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
47 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
48 return file_magic::coff_object;
49 if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
50 return file_magic::coff_cl_gl_object;
51 return file_magic::coff_import_library;
52 }
53 // Windows resource file
Eric Beckmannc8dba242017-07-08 03:06:10 +000054 if (Magic.size() >= sizeof(COFF::WinResMagic) &&
55 memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
Zachary Turner264b5d92017-06-07 03:48:56 +000056 return file_magic::windows_resource;
57 // 0x0000 = COFF unknown machine type
58 if (Magic[1] == 0)
59 return file_magic::coff_object;
60 if (startswith(Magic, "\0asm"))
61 return file_magic::wasm_object;
62 break;
63 }
Hubert Tongab2eb2b2019-04-04 00:53:21 +000064
65 case 0x01:
66 // XCOFF format
67 if (startswith(Magic, "\x01\xDF"))
68 return file_magic::xcoff_object_32;
69 break;
70
Zachary Turner264b5d92017-06-07 03:48:56 +000071 case 0xDE: // 0x0B17C0DE = BC wraper
72 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
73 return file_magic::bitcode;
74 break;
75 case 'B':
76 if (startswith(Magic, "BC\xC0\xDE"))
77 return file_magic::bitcode;
78 break;
79 case '!':
80 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
81 return file_magic::archive;
82 break;
83
84 case '\177':
85 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
86 bool Data2MSB = Magic[5] == 2;
87 unsigned high = Data2MSB ? 16 : 17;
88 unsigned low = Data2MSB ? 17 : 16;
89 if (Magic[high] == 0) {
90 switch (Magic[low]) {
91 default:
92 return file_magic::elf;
93 case 1:
94 return file_magic::elf_relocatable;
95 case 2:
96 return file_magic::elf_executable;
97 case 3:
98 return file_magic::elf_shared_object;
99 case 4:
100 return file_magic::elf_core;
101 }
102 }
103 // It's still some type of ELF file.
104 return file_magic::elf;
105 }
106 break;
107
108 case 0xCA:
109 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
110 startswith(Magic, "\xCA\xFE\xBA\xBF")) {
111 // This is complicated by an overlap with Java class files.
112 // See the Mach-O section in /usr/share/file/magic for details.
113 if (Magic.size() >= 8 && Magic[7] < 43)
114 return file_magic::macho_universal_binary;
115 }
116 break;
117
118 // The two magic numbers for mach-o are:
119 // 0xfeedface - 32-bit mach-o
120 // 0xfeedfacf - 64-bit mach-o
121 case 0xFE:
122 case 0xCE:
123 case 0xCF: {
124 uint16_t type = 0;
125 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
126 startswith(Magic, "\xFE\xED\xFA\xCF")) {
127 /* Native endian */
128 size_t MinSize;
129 if (Magic[3] == char(0xCE))
130 MinSize = sizeof(MachO::mach_header);
131 else
132 MinSize = sizeof(MachO::mach_header_64);
133 if (Magic.size() >= MinSize)
134 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
135 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
136 startswith(Magic, "\xCF\xFA\xED\xFE")) {
137 /* Reverse endian */
138 size_t MinSize;
139 if (Magic[0] == char(0xCE))
140 MinSize = sizeof(MachO::mach_header);
141 else
142 MinSize = sizeof(MachO::mach_header_64);
143 if (Magic.size() >= MinSize)
144 type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
145 }
146 switch (type) {
147 default:
148 break;
149 case 1:
150 return file_magic::macho_object;
151 case 2:
152 return file_magic::macho_executable;
153 case 3:
154 return file_magic::macho_fixed_virtual_memory_shared_lib;
155 case 4:
156 return file_magic::macho_core;
157 case 5:
158 return file_magic::macho_preload_executable;
159 case 6:
160 return file_magic::macho_dynamically_linked_shared_lib;
161 case 7:
162 return file_magic::macho_dynamic_linker;
163 case 8:
164 return file_magic::macho_bundle;
165 case 9:
166 return file_magic::macho_dynamically_linked_shared_lib_stub;
167 case 10:
168 return file_magic::macho_dsym_companion;
169 case 11:
170 return file_magic::macho_kext_bundle;
171 }
172 break;
173 }
174 case 0xF0: // PowerPC Windows
175 case 0x83: // Alpha 32-bit
176 case 0x84: // Alpha 64-bit
177 case 0x66: // MPS R4000 Windows
178 case 0x50: // mc68K
179 case 0x4c: // 80386 Windows
180 case 0xc4: // ARMNT Windows
181 if (Magic[1] == 0x01)
182 return file_magic::coff_object;
183 LLVM_FALLTHROUGH;
184
185 case 0x90: // PA-RISC Windows
186 case 0x68: // mc68K Windows
187 if (Magic[1] == 0x02)
188 return file_magic::coff_object;
189 break;
190
Pavel Labath581d79a2019-03-21 09:18:59 +0000191 case 'M': // Possible MS-DOS stub on Windows PE file, MSF/PDB file or a
192 // Minidump file.
Benjamin Kramer80df6422017-08-31 12:50:42 +0000193 if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000194 uint32_t off = read32le(Magic.data() + 0x3c);
195 // PE/COFF file, either EXE or DLL.
Rafael Espindoladeaba382017-10-19 01:32:18 +0000196 if (Magic.substr(off).startswith(
197 StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
Zachary Turner264b5d92017-06-07 03:48:56 +0000198 return file_magic::pecoff_executable;
199 }
Zachary Turnerd860fa62018-03-07 18:40:41 +0000200 if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
201 return file_magic::pdb;
Pavel Labath581d79a2019-03-21 09:18:59 +0000202 if (startswith(Magic, "MDMP"))
203 return file_magic::minidump;
Zachary Turner264b5d92017-06-07 03:48:56 +0000204 break;
205
Martin Storsjo3fa12132017-06-29 06:30:56 +0000206 case 0x64: // x86-64 or ARM64 Windows.
207 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
Zachary Turner264b5d92017-06-07 03:48:56 +0000208 return file_magic::coff_object;
209 break;
210
211 default:
212 break;
213 }
214 return file_magic::unknown;
215}
216
217std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
Zachary Turnerb07298e2018-12-01 00:22:39 +0000218 auto FileOrError = MemoryBuffer::getFile(Path, -1LL, false);
Zachary Turner9899b5f2018-03-08 19:45:20 +0000219 if (!FileOrError)
220 return FileOrError.getError();
Zachary Turner264b5d92017-06-07 03:48:56 +0000221
Zachary Turner9899b5f2018-03-08 19:45:20 +0000222 std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
223 Result = identify_magic(FileBuffer->getBuffer());
Zachary Turner264b5d92017-06-07 03:48:56 +0000224
Zachary Turner264b5d92017-06-07 03:48:56 +0000225 return std::error_code();
226}