LLVM 18.0.0git
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 <system_error>
13
14namespace llvm {
15class StringRef;
16class Twine;
17
18/// file_magic - An "enum class" enumeration of file types based on magic (the
19/// first N bytes of the file).
20struct 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 goff_object, ///< GOFF object file
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 macho_file_set, ///< Mach-O file set binary
44 minidump, ///< Windows minidump file
45 coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file
46 coff_object, ///< COFF object file
47 coff_import_library, ///< COFF import library
48 pecoff_executable, ///< PECOFF executable file
49 windows_resource, ///< Windows compiled resource file (.res)
50 xcoff_object_32, ///< 32-bit XCOFF object file
51 xcoff_object_64, ///< 64-bit XCOFF object file
52 wasm_object, ///< WebAssembly Object file
53 pdb, ///< Windows PDB debug info file
54 tapi_file, ///< Text-based Dynamic Library Stub file
55 cuda_fatbinary, ///< CUDA Fatbinary object file
56 offload_binary, ///< LLVM offload object file
57 dxcontainer_object, ///< DirectX container file
58 };
59
60 bool is_object() const { return V != unknown; }
61
62 file_magic() = default;
63 file_magic(Impl V) : V(V) {}
64 operator Impl() const { return V; }
65
66private:
67 Impl V = unknown;
68};
69
70/// Identify the type of a binary file based on how magical it is.
71file_magic identify_magic(StringRef magic);
72
73/// Get and identify \a path's type based on its content.
74///
75/// @param path Input path.
76/// @param result Set to the type of file, or file_magic::unknown.
77/// @returns errc::success if result has been successfully set, otherwise a
78/// platform-specific error_code.
79std::error_code identify_magic(const Twine &path, file_magic &result);
80} // namespace llvm
81
82#endif
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
bool is_object() const
Definition: Magic.h:60
@ coff_import_library
COFF import library.
Definition: Magic.h:47
@ pdb
Windows PDB debug info file.
Definition: Magic.h:53
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
@ archive
ar style archive file
Definition: Magic.h:24
@ elf_shared_object
ELF dynamically linked shared lib.
Definition: Magic.h:28
@ goff_object
GOFF object file.
Definition: Magic.h:30
@ minidump
Windows minidump file.
Definition: Magic.h:44
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:36
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:51
@ elf_executable
ELF Executable image.
Definition: Magic.h:27
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition: Magic.h:39
@ macho_preload_executable
Mach-O Preloaded Executable.
Definition: Magic.h:35
@ macho_file_set
Mach-O file set binary.
Definition: Magic.h:43
@ dxcontainer_object
DirectX container file.
Definition: Magic.h:57
@ macho_kext_bundle
Mach-O kext bundle file.
Definition: Magic.h:41
@ pecoff_executable
PECOFF executable file.
Definition: Magic.h:48
@ offload_binary
LLVM offload object file.
Definition: Magic.h:56
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:42
@ bitcode
Bitcode file.
Definition: Magic.h:23
@ macho_core
Mach-O Core File.
Definition: Magic.h:34
@ wasm_object
WebAssembly Object file.
Definition: Magic.h:52
@ xcoff_object_32
32-bit XCOFF object file
Definition: Magic.h:50
@ windows_resource
Windows compiled resource file (.res)
Definition: Magic.h:49
@ elf_core
ELF core image.
Definition: Magic.h:29
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
@ coff_object
COFF object file.
Definition: Magic.h:46
@ elf
ELF Unknown type.
Definition: Magic.h:25
@ macho_bundle
Mach-O Bundle file.
Definition: Magic.h:38
@ coff_cl_gl_object
Microsoft cl.exe's intermediate code file.
Definition: Magic.h:45
@ cuda_fatbinary
CUDA Fatbinary object file.
Definition: Magic.h:55
@ macho_executable
Mach-O Executable.
Definition: Magic.h:32
@ macho_dsym_companion
Mach-O dSYM companion file.
Definition: Magic.h:40
@ unknown
Unrecognized file.
Definition: Magic.h:22
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition: Magic.h:33
@ macho_dynamic_linker
The Mach-O dynamic linker.
Definition: Magic.h:37
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:54
file_magic(Impl V)
Definition: Magic.h:63
file_magic()=default