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