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