1 //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/BinaryFormat/Magic.h"
11
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"
18
19 #if !defined(_MSC_VER) && !defined(__MINGW32__)
20 #include <unistd.h>
21 #else
22 #include <io.h>
23 #endif
24
25 using namespace llvm;
26 using namespace llvm::support::endian;
27 using namespace llvm::sys::fs;
28
29 template <size_t N>
startswith(StringRef Magic,const char (& S)[N])30 static bool startswith(StringRef Magic, const char (&S)[N]) {
31 return Magic.startswith(StringRef(S, N - 1));
32 }
33
34 /// Identify the magic in magic.
identify_magic(StringRef Magic)35 file_magic llvm::identify_magic(StringRef Magic) {
36 if (Magic.size() < 4)
37 return file_magic::unknown;
38 switch ((unsigned char)Magic[0]) {
39 case 0x00: {
40 // COFF bigobj, CL.exe's LTO object file, or short import library file
41 if (startswith(Magic, "\0\0\xFF\xFF")) {
42 size_t MinSize =
43 offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
44 if (Magic.size() < MinSize)
45 return file_magic::coff_import_library;
46
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;
53 }
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
59 if (Magic[1] == 0)
60 return file_magic::coff_object;
61 if (startswith(Magic, "\0asm"))
62 return file_magic::wasm_object;
63 break;
64 }
65 case 0xDE: // 0x0B17C0DE = BC wraper
66 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
67 return file_magic::bitcode;
68 break;
69 case 'B':
70 if (startswith(Magic, "BC\xC0\xDE"))
71 return file_magic::bitcode;
72 break;
73 case '!':
74 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
75 return file_magic::archive;
76 break;
77
78 case '\177':
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) {
84 switch (Magic[low]) {
85 default:
86 return file_magic::elf;
87 case 1:
88 return file_magic::elf_relocatable;
89 case 2:
90 return file_magic::elf_executable;
91 case 3:
92 return file_magic::elf_shared_object;
93 case 4:
94 return file_magic::elf_core;
95 }
96 }
97 // It's still some type of ELF file.
98 return file_magic::elf;
99 }
100 break;
101
102 case 0xCA:
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;
109 }
110 break;
111
112 // The two magic numbers for mach-o are:
113 // 0xfeedface - 32-bit mach-o
114 // 0xfeedfacf - 64-bit mach-o
115 case 0xFE:
116 case 0xCE:
117 case 0xCF: {
118 uint16_t type = 0;
119 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
120 startswith(Magic, "\xFE\xED\xFA\xCF")) {
121 /* Native endian */
122 size_t MinSize;
123 if (Magic[3] == char(0xCE))
124 MinSize = sizeof(MachO::mach_header);
125 else
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")) {
131 /* Reverse endian */
132 size_t MinSize;
133 if (Magic[0] == char(0xCE))
134 MinSize = sizeof(MachO::mach_header);
135 else
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];
139 }
140 switch (type) {
141 default:
142 break;
143 case 1:
144 return file_magic::macho_object;
145 case 2:
146 return file_magic::macho_executable;
147 case 3:
148 return file_magic::macho_fixed_virtual_memory_shared_lib;
149 case 4:
150 return file_magic::macho_core;
151 case 5:
152 return file_magic::macho_preload_executable;
153 case 6:
154 return file_magic::macho_dynamically_linked_shared_lib;
155 case 7:
156 return file_magic::macho_dynamic_linker;
157 case 8:
158 return file_magic::macho_bundle;
159 case 9:
160 return file_magic::macho_dynamically_linked_shared_lib_stub;
161 case 10:
162 return file_magic::macho_dsym_companion;
163 case 11:
164 return file_magic::macho_kext_bundle;
165 }
166 break;
167 }
168 case 0xF0: // PowerPC Windows
169 case 0x83: // Alpha 32-bit
170 case 0x84: // Alpha 64-bit
171 case 0x66: // MPS R4000 Windows
172 case 0x50: // mc68K
173 case 0x4c: // 80386 Windows
174 case 0xc4: // ARMNT Windows
175 if (Magic[1] == 0x01)
176 return file_magic::coff_object;
177 LLVM_FALLTHROUGH;
178
179 case 0x90: // PA-RISC Windows
180 case 0x68: // mc68K Windows
181 if (Magic[1] == 0x02)
182 return file_magic::coff_object;
183 break;
184
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;
192 }
193 if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
194 return file_magic::pdb;
195 break;
196
197 case 0x64: // x86-64 or ARM64 Windows.
198 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
199 return file_magic::coff_object;
200 break;
201
202 default:
203 break;
204 }
205 return file_magic::unknown;
206 }
207
identify_magic(const Twine & Path,file_magic & Result)208 std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
209 auto FileOrError = MemoryBuffer::getFile(Path);
210 if (!FileOrError)
211 return FileOrError.getError();
212
213 std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
214 Result = identify_magic(FileBuffer->getBuffer());
215
216 return std::error_code();
217 }
218