Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / BinaryFormat / Magic.cpp
blob5e9d6e73257a3c889fc8584c7aac86d4ea6f9fe6
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 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/Magic.h"
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"
16 #include "llvm/Support/MemoryBuffer.h"
18 #if !defined(_MSC_VER) && !defined(__MINGW32__)
19 #include <unistd.h>
20 #else
21 #include <io.h>
22 #endif
24 using namespace llvm;
25 using namespace llvm::support::endian;
26 using namespace llvm::sys::fs;
28 template <size_t N>
29 static bool startswith(StringRef Magic, const char (&S)[N]) {
30 return Magic.startswith(StringRef(S, N - 1));
33 /// Identify the magic in magic.
34 file_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;
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;
53 // Windows resource file
54 if (Magic.size() >= sizeof(COFF::WinResMagic) &&
55 memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
56 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;
64 case 0xDE: // 0x0B17C0DE = BC wraper
65 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
66 return file_magic::bitcode;
67 break;
68 case 'B':
69 if (startswith(Magic, "BC\xC0\xDE"))
70 return file_magic::bitcode;
71 break;
72 case '!':
73 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
74 return file_magic::archive;
75 break;
77 case '\177':
78 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
79 bool Data2MSB = Magic[5] == 2;
80 unsigned high = Data2MSB ? 16 : 17;
81 unsigned low = Data2MSB ? 17 : 16;
82 if (Magic[high] == 0) {
83 switch (Magic[low]) {
84 default:
85 return file_magic::elf;
86 case 1:
87 return file_magic::elf_relocatable;
88 case 2:
89 return file_magic::elf_executable;
90 case 3:
91 return file_magic::elf_shared_object;
92 case 4:
93 return file_magic::elf_core;
96 // It's still some type of ELF file.
97 return file_magic::elf;
99 break;
101 case 0xCA:
102 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
103 startswith(Magic, "\xCA\xFE\xBA\xBF")) {
104 // This is complicated by an overlap with Java class files.
105 // See the Mach-O section in /usr/share/file/magic for details.
106 if (Magic.size() >= 8 && Magic[7] < 43)
107 return file_magic::macho_universal_binary;
109 break;
111 // The two magic numbers for mach-o are:
112 // 0xfeedface - 32-bit mach-o
113 // 0xfeedfacf - 64-bit mach-o
114 case 0xFE:
115 case 0xCE:
116 case 0xCF: {
117 uint16_t type = 0;
118 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
119 startswith(Magic, "\xFE\xED\xFA\xCF")) {
120 /* Native endian */
121 size_t MinSize;
122 if (Magic[3] == char(0xCE))
123 MinSize = sizeof(MachO::mach_header);
124 else
125 MinSize = sizeof(MachO::mach_header_64);
126 if (Magic.size() >= MinSize)
127 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
128 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
129 startswith(Magic, "\xCF\xFA\xED\xFE")) {
130 /* Reverse endian */
131 size_t MinSize;
132 if (Magic[0] == char(0xCE))
133 MinSize = sizeof(MachO::mach_header);
134 else
135 MinSize = sizeof(MachO::mach_header_64);
136 if (Magic.size() >= MinSize)
137 type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
139 switch (type) {
140 default:
141 break;
142 case 1:
143 return file_magic::macho_object;
144 case 2:
145 return file_magic::macho_executable;
146 case 3:
147 return file_magic::macho_fixed_virtual_memory_shared_lib;
148 case 4:
149 return file_magic::macho_core;
150 case 5:
151 return file_magic::macho_preload_executable;
152 case 6:
153 return file_magic::macho_dynamically_linked_shared_lib;
154 case 7:
155 return file_magic::macho_dynamic_linker;
156 case 8:
157 return file_magic::macho_bundle;
158 case 9:
159 return file_magic::macho_dynamically_linked_shared_lib_stub;
160 case 10:
161 return file_magic::macho_dsym_companion;
162 case 11:
163 return file_magic::macho_kext_bundle;
165 break;
167 case 0xF0: // PowerPC Windows
168 case 0x83: // Alpha 32-bit
169 case 0x84: // Alpha 64-bit
170 case 0x66: // MPS R4000 Windows
171 case 0x50: // mc68K
172 case 0x4c: // 80386 Windows
173 case 0xc4: // ARMNT Windows
174 if (Magic[1] == 0x01)
175 return file_magic::coff_object;
176 LLVM_FALLTHROUGH;
178 case 0x90: // PA-RISC Windows
179 case 0x68: // mc68K Windows
180 if (Magic[1] == 0x02)
181 return file_magic::coff_object;
182 break;
184 case 'M': // Possible MS-DOS stub on Windows PE file or MSF/PDB file.
185 if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
186 uint32_t off = read32le(Magic.data() + 0x3c);
187 // PE/COFF file, either EXE or DLL.
188 if (Magic.substr(off).startswith(
189 StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
190 return file_magic::pecoff_executable;
192 if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
193 return file_magic::pdb;
194 break;
196 case 0x64: // x86-64 or ARM64 Windows.
197 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
198 return file_magic::coff_object;
199 break;
201 default:
202 break;
204 return file_magic::unknown;
207 std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
208 auto FileOrError = MemoryBuffer::getFile(Path, -1LL, false);
209 if (!FileOrError)
210 return FileOrError.getError();
212 std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
213 Result = identify_magic(FileBuffer->getBuffer());
215 return std::error_code();