LLVM  10.0.0svn
Magic.h
Go to the documentation of this file.
1 //===- llvm/BinaryFormat/Magic.h - 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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_BINARYFORMAT_MAGIC_H
10 #define LLVM_BINARYFORMAT_MAGIC_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
14 
15 #include <system_error>
16 
17 namespace llvm {
18 /// file_magic - An "enum class" enumeration of file types based on magic (the
19 /// first N bytes of the file).
20 struct file_magic {
21  enum Impl {
22  unknown = 0, ///< Unrecognized file
23  bitcode, ///< Bitcode file
24  archive, ///< ar style archive file
25  elf, ///< ELF Unknown type
26  elf_relocatable, ///< ELF Relocatable object file
27  elf_executable, ///< ELF Executable image
28  elf_shared_object, ///< ELF dynamically linked shared lib
29  elf_core, ///< ELF core image
30  macho_object, ///< Mach-O Object file
31  macho_executable, ///< Mach-O Executable
32  macho_fixed_virtual_memory_shared_lib, ///< Mach-O Shared Lib, FVM
33  macho_core, ///< Mach-O Core File
34  macho_preload_executable, ///< Mach-O Preloaded Executable
35  macho_dynamically_linked_shared_lib, ///< Mach-O dynlinked shared lib
36  macho_dynamic_linker, ///< The Mach-O dynamic linker
37  macho_bundle, ///< Mach-O Bundle file
38  macho_dynamically_linked_shared_lib_stub, ///< Mach-O Shared lib stub
39  macho_dsym_companion, ///< Mach-O dSYM companion file
40  macho_kext_bundle, ///< Mach-O kext bundle file
41  macho_universal_binary, ///< Mach-O universal binary
42  minidump, ///< Windows minidump file
43  coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file
44  coff_object, ///< COFF object file
45  coff_import_library, ///< COFF import library
46  pecoff_executable, ///< PECOFF executable file
47  windows_resource, ///< Windows compiled resource file (.res)
48  xcoff_object_32, ///< 32-bit XCOFF object file
49  xcoff_object_64, ///< 64-bit XCOFF object file
50  wasm_object, ///< WebAssembly Object file
51  pdb, ///< Windows PDB debug info file
52  };
53 
54  bool is_object() const { return V != unknown; }
55 
56  file_magic() = default;
57  file_magic(Impl V) : V(V) {}
58  operator Impl() const { return V; }
59 
60 private:
61  Impl V = unknown;
62 };
63 
64 /// Identify the type of a binary file based on how magical it is.
66 
67 /// Get and identify \a path's type based on its content.
68 ///
69 /// @param path Input path.
70 /// @param result Set to the type of file, or file_magic::unknown.
71 /// @returns errc::success if result has been successfully set, otherwise a
72 /// platform-specific error_code.
73 std::error_code identify_magic(const Twine &path, file_magic &result);
74 } // namespace llvm
75 
76 #endif
32-bit XCOFF object file
Definition: Magic.h:48
Mach-O Object file.
Definition: Magic.h:30
ELF core image.
Definition: Magic.h:29
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Microsoft cl.exe&#39;s intermediate code file.
Definition: Magic.h:43
ELF dynamically linked shared lib.
Definition: Magic.h:28
Windows compiled resource file (.res)
Definition: Magic.h:47
bool is_object() const
Definition: Magic.h:54
Mach-O Bundle file.
Definition: Magic.h:37
Mach-O Preloaded Executable.
Definition: Magic.h:34
Bitcode file.
Definition: Magic.h:23
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
The Mach-O dynamic linker.
Definition: Magic.h:36
file_magic(Impl V)
Definition: Magic.h:57
64-bit XCOFF object file
Definition: Magic.h:49
ELF Relocatable object file.
Definition: Magic.h:26
Windows minidump file.
Definition: Magic.h:42
COFF import library.
Definition: Magic.h:45
Mach-O universal binary.
Definition: Magic.h:41
Mach-O Core File.
Definition: Magic.h:33
Mach-O dSYM companion file.
Definition: Magic.h:39
PECOFF executable file.
Definition: Magic.h:46
COFF object file.
Definition: Magic.h:44
Mach-O kext bundle file.
Definition: Magic.h:40
ar style archive file
Definition: Magic.h:24
ELF Unknown type.
Definition: Magic.h:25
ELF Executable image.
Definition: Magic.h:27
WebAssembly Object file.
Definition: Magic.h:50
file_magic()=default
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Mach-O Executable.
Definition: Magic.h:31
Unrecognized file.
Definition: Magic.h:22
Windows PDB debug info file.
Definition: Magic.h:51
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20