annotate lib/BinaryFormat/Magic.cpp @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "llvm/BinaryFormat/Magic.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 #include "llvm/BinaryFormat/COFF.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 #include "llvm/BinaryFormat/ELF.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #include "llvm/BinaryFormat/MachO.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "llvm/Support/Endian.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "llvm/Support/FileSystem.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #if !defined(_MSC_VER) && !defined(__MINGW32__)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include <unistd.h>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include <io.h>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #endif
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 using namespace llvm::support::endian;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 using namespace llvm::sys::fs;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 template <size_t N>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 static bool startswith(StringRef Magic, const char (&S)[N]) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 return Magic.startswith(StringRef(S, N - 1));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 /// @brief Identify the magic in magic.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 file_magic llvm::identify_magic(StringRef Magic) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 if (Magic.size() < 4)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 return file_magic::unknown;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 switch ((unsigned char)Magic[0]) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 case 0x00: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 // COFF bigobj, CL.exe's LTO object file, or short import library file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 if (startswith(Magic, "\0\0\xFF\xFF")) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 size_t MinSize =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 if (Magic.size() < MinSize)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 return file_magic::coff_import_library;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 return file_magic::coff_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 return file_magic::coff_cl_gl_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 return file_magic::coff_import_library;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 // Windows resource file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 if (Magic.size() >= sizeof(COFF::WinResMagic) &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 return file_magic::windows_resource;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 // 0x0000 = COFF unknown machine type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 if (Magic[1] == 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 return file_magic::coff_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 if (startswith(Magic, "\0asm"))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 return file_magic::wasm_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 case 0xDE: // 0x0B17C0DE = BC wraper
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 if (startswith(Magic, "\xDE\xC0\x17\x0B"))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 return file_magic::bitcode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 case 'B':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 if (startswith(Magic, "BC\xC0\xDE"))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 return file_magic::bitcode;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 case '!':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 return file_magic::archive;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 case '\177':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 bool Data2MSB = Magic[5] == 2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 unsigned high = Data2MSB ? 16 : 17;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 unsigned low = Data2MSB ? 17 : 16;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 if (Magic[high] == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 switch (Magic[low]) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 return file_magic::elf;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 case 1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 return file_magic::elf_relocatable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 case 2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 return file_magic::elf_executable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 case 3:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 return file_magic::elf_shared_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 case 4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 return file_magic::elf_core;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 // It's still some type of ELF file.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 return file_magic::elf;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 case 0xCA:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 startswith(Magic, "\xCA\xFE\xBA\xBF")) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 // This is complicated by an overlap with Java class files.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 // See the Mach-O section in /usr/share/file/magic for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 if (Magic.size() >= 8 && Magic[7] < 43)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 return file_magic::macho_universal_binary;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 // The two magic numbers for mach-o are:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 // 0xfeedface - 32-bit mach-o
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 // 0xfeedfacf - 64-bit mach-o
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 case 0xFE:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 case 0xCE:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 case 0xCF: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 uint16_t type = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 startswith(Magic, "\xFE\xED\xFA\xCF")) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 /* Native endian */
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 size_t MinSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 if (Magic[3] == char(0xCE))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 MinSize = sizeof(MachO::mach_header);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 MinSize = sizeof(MachO::mach_header_64);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 if (Magic.size() >= MinSize)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 startswith(Magic, "\xCF\xFA\xED\xFE")) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 /* Reverse endian */
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 size_t MinSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 if (Magic[0] == char(0xCE))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 MinSize = sizeof(MachO::mach_header);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 MinSize = sizeof(MachO::mach_header_64);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 if (Magic.size() >= MinSize)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 switch (type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 case 1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 return file_magic::macho_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 case 2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 return file_magic::macho_executable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 case 3:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 return file_magic::macho_fixed_virtual_memory_shared_lib;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 case 4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 return file_magic::macho_core;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 case 5:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 return file_magic::macho_preload_executable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 case 6:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 return file_magic::macho_dynamically_linked_shared_lib;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 case 7:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 return file_magic::macho_dynamic_linker;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 case 8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 return file_magic::macho_bundle;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 case 9:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 return file_magic::macho_dynamically_linked_shared_lib_stub;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 case 10:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 return file_magic::macho_dsym_companion;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 case 11:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 return file_magic::macho_kext_bundle;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 case 0xF0: // PowerPC Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 case 0x83: // Alpha 32-bit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 case 0x84: // Alpha 64-bit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 case 0x66: // MPS R4000 Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 case 0x50: // mc68K
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 case 0x4c: // 80386 Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 case 0xc4: // ARMNT Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 if (Magic[1] == 0x01)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 return file_magic::coff_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 LLVM_FALLTHROUGH;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 case 0x90: // PA-RISC Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 case 0x68: // mc68K Windows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 if (Magic[1] == 0x02)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 return file_magic::coff_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 case 'M': // Possible MS-DOS stub on Windows PE file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 uint32_t off = read32le(Magic.data() + 0x3c);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 // PE/COFF file, either EXE or DLL.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 if (Magic.substr(off).startswith(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 return file_magic::pecoff_executable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 case 0x64: // x86-64 or ARM64 Windows.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 return file_magic::coff_object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 return file_magic::unknown;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 int FD;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 if (std::error_code EC = openFileForRead(Path, FD))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 return EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 char Buffer[32];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 int Length = read(FD, Buffer, sizeof(Buffer));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 if (close(FD) != 0 || Length < 0)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 return std::error_code(errno, std::generic_category());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 Result = identify_magic(StringRef(Buffer, Length));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 return std::error_code();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 }