LLVM: lib/BinaryFormat/Magic.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

16

17#if !defined(_MSC_VER) && !defined(__MINGW32__)

18#include <unistd.h>

19#else

20#include <io.h>

21#endif

22

23using namespace llvm;

26

27template <size_t N>

29 return Magic.starts_with(StringRef(S, N - 1));

30}

31

32

34 if (Magic.size() < 4)

36 switch ((unsigned char)Magic[0]) {

37 case 0x00: {

38

39 if (startswith(Magic, "\0\0\xFF\xFF")) {

40 size_t MinSize =

42 if (Magic.size() < MinSize)

44

51 }

52

56

57 if (Magic[1] == 0)

61 break;

62 }

63

64 case 0x01:

65

70 break;

71

72 case 0x03:

73 if (startswith(Magic, "\x03\xF0\x00"))

75

76 if (startswith(Magic, "\x03\x02\x23\x07"))

78 break;

79

80 case 0x07:

81 if (startswith(Magic, "\x07\x23\x02\x03"))

83 break;

84

85 case 0x10:

86 if (startswith(Magic, "\x10\xFF\x10\xAD"))

88 break;

89

90 case 0xDE:

91 if (startswith(Magic, "\xDE\xC0\x17\x0B"))

93 break;

94 case 'B':

97 break;

98 case 'C':

103 break;

104 case '!':

107 break;

108 case '<':

111 break;

112 case '\177':

113 if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {

114 bool Data2MSB = Magic[5] == 2;

115 unsigned high = Data2MSB ? 16 : 17;

116 unsigned low = Data2MSB ? 17 : 16;

117 if (Magic[high] == 0) {

118 switch (Magic[low]) {

119 default:

121 case 1:

123 case 2:

125 case 3:

127 case 4:

129 }

130 }

131

133 }

134 break;

135

136 case 0xCA:

137 if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||

138 startswith(Magic, "\xCA\xFE\xBA\xBF")) {

139

140

141 if (Magic.size() >= 8 && Magic[7] < 43)

143 }

144 break;

145

146

147

148

149 case 0xFE:

150 case 0xCE:

151 case 0xCF: {

153 if (startswith(Magic, "\xFE\xED\xFA\xCE") ||

154 startswith(Magic, "\xFE\xED\xFA\xCF")) {

155

156 size_t MinSize;

157 if (Magic[3] == char(0xCE))

159 else

161 if (Magic.size() >= MinSize)

162 type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];

163 } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||

164 startswith(Magic, "\xCF\xFA\xED\xFE")) {

165

166 size_t MinSize;

167 if (Magic[0] == char(0xCE))

169 else

171 if (Magic.size() >= MinSize)

172 type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];

173 }

174 switch (type) {

175 default:

176 break;

177 case 1:

179 case 2:

181 case 3:

183 case 4:

185 case 5:

187 case 6:

189 case 7:

191 case 8:

193 case 9:

195 case 10:

197 case 11:

199 case 12:

201 }

202 break;

203 }

204 case 0xF0:

205 case 0x83:

206 case 0x84:

207 case 0x66:

208 case 0x50:

209 if (startswith(Magic, "\x50\xed\x55\xba"))

211 [[fallthrough]];

212

213 case 0x4c:

214 case 0xc4:

215 if (Magic[1] == 0x01)

217 [[fallthrough]];

218

219 case 0x90:

220 case 0x68:

221 if (Magic[1] == 0x02)

223 break;

224

225 case 'M':

226

227 if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {

229

230 if (Magic.substr(off).starts_with(

233 }

234 if (Magic.starts_with("Microsoft C/C++ MSF 7.00\r\n"))

238 break;

239

240 case 0x64:

241 if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))

243 break;

244

245 case 0x2d:

248 break;

249 case 0x7b:

251 break;

252

253 case 'D':

256 break;

257

258 case 0x41:

259 if (Magic[1] == char(0xA6))

261 break;

262

263 case 0x4e:

264 if (Magic[1] == char(0xA6))

266 break;

267

268 case '_': {

269 const char OBMagic[] = "__CLANG_OFFLOAD_BUNDLE__";

270 if (Magic.size() >= sizeof(OBMagic) && startswith(Magic, OBMagic))

272 break;

273 }

274

275 default:

276 break;

277 }

279}

280

283 false);

284 if (!FileOrError)

285 return FileOrError.getError();

286

287 std::unique_ptr FileBuffer = std::move(*FileOrError);

289

290 return std::error_code();

291}

#define offsetof(TYPE, MEMBER)

static bool startswith(StringRef Magic, const char(&S)[N])

Merge contiguous icmps into a memcmp

std::pair< llvm::MachO::Target, std::string > UUID

static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)

Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...

StringRef - Represent a constant reference to a string, i.e.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

static const char ClGlObjMagic[]

static const char WinResMagic[]

static const char BigObjMagic[]

static const char PEMagic[]

uint32_t read32le(const void *P)

This is an optimization pass for GlobalISel generic memory operations.

file_magic identify_magic(StringRef magic)

Identify the type of a binary file based on how magical it is.

file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...

@ coff_import_library

COFF import library.

@ pdb

Windows PDB debug info file.

@ spirv_object

A binary SPIR-V file.

@ elf_relocatable

ELF Relocatable object file.

@ archive

ar style archive file

@ elf_shared_object

ELF dynamically linked shared lib.

@ goff_object

GOFF object file.

@ minidump

Windows minidump file.

@ macho_dynamically_linked_shared_lib

Mach-O dynlinked shared lib.

@ xcoff_object_64

64-bit XCOFF object file

@ elf_executable

ELF Executable image.

@ macho_dynamically_linked_shared_lib_stub

Mach-O Shared lib stub.

@ macho_preload_executable

Mach-O Preloaded Executable.

@ offload_bundle

Clang offload bundle file.

@ offload_bundle_compressed

Compressed clang offload bundle file.

@ macho_file_set

Mach-O file set binary.

@ dxcontainer_object

DirectX container file.

@ macho_kext_bundle

Mach-O kext bundle file.

@ pecoff_executable

PECOFF executable file.

@ offload_binary

LLVM offload object file.

@ macho_universal_binary

Mach-O universal binary.

@ macho_core

Mach-O Core File.

@ wasm_object

WebAssembly Object file.

@ xcoff_object_32

32-bit XCOFF object file

@ windows_resource

Windows compiled resource file (.res)

@ clang_ast

Clang PCH or PCM.

@ elf_core

ELF core image.

@ macho_object

Mach-O Object file.

@ coff_object

COFF object file.

@ macho_bundle

Mach-O Bundle file.

@ coff_cl_gl_object

Microsoft cl.exe's intermediate code file.

@ cuda_fatbinary

CUDA Fatbinary object file.

@ macho_executable

Mach-O Executable.

@ macho_dsym_companion

Mach-O dSYM companion file.

@ unknown

Unrecognized file.

@ macho_fixed_virtual_memory_shared_lib

Mach-O Shared Lib, FVM.

@ macho_dynamic_linker

The Mach-O dynamic linker.

@ tapi_file

Text-based Dynamic Library Stub file.