Magic.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/BinaryFormat/Magic.h - File magic identification ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_BINARYFORMAT_MAGIC_H
  14. #define LLVM_BINARYFORMAT_MAGIC_H
  15. #include <system_error>
  16. namespace llvm {
  17. class StringRef;
  18. class Twine;
  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. goff_object, ///< GOFF object file
  32. macho_object, ///< Mach-O Object file
  33. macho_executable, ///< Mach-O Executable
  34. macho_fixed_virtual_memory_shared_lib, ///< Mach-O Shared Lib, FVM
  35. macho_core, ///< Mach-O Core File
  36. macho_preload_executable, ///< Mach-O Preloaded Executable
  37. macho_dynamically_linked_shared_lib, ///< Mach-O dynlinked shared lib
  38. macho_dynamic_linker, ///< The Mach-O dynamic linker
  39. macho_bundle, ///< Mach-O Bundle file
  40. macho_dynamically_linked_shared_lib_stub, ///< Mach-O Shared lib stub
  41. macho_dsym_companion, ///< Mach-O dSYM companion file
  42. macho_kext_bundle, ///< Mach-O kext bundle file
  43. macho_universal_binary, ///< Mach-O universal binary
  44. macho_file_set, ///< Mach-O file set binary
  45. minidump, ///< Windows minidump file
  46. coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file
  47. coff_object, ///< COFF object file
  48. coff_import_library, ///< COFF import library
  49. pecoff_executable, ///< PECOFF executable file
  50. windows_resource, ///< Windows compiled resource file (.res)
  51. xcoff_object_32, ///< 32-bit XCOFF object file
  52. xcoff_object_64, ///< 64-bit XCOFF object file
  53. wasm_object, ///< WebAssembly Object file
  54. pdb, ///< Windows PDB debug info file
  55. tapi_file, ///< Text-based Dynamic Library Stub file
  56. cuda_fatbinary, ///< CUDA Fatbinary object file
  57. offload_binary, ///< LLVM offload object file
  58. dxcontainer_object, ///< DirectX container file
  59. };
  60. bool is_object() const { return V != unknown; }
  61. file_magic() = default;
  62. file_magic(Impl V) : V(V) {}
  63. operator Impl() const { return V; }
  64. private:
  65. Impl V = unknown;
  66. };
  67. /// Identify the type of a binary file based on how magical it is.
  68. file_magic identify_magic(StringRef magic);
  69. /// Get and identify \a path's type based on its content.
  70. ///
  71. /// @param path Input path.
  72. /// @param result Set to the type of file, or file_magic::unknown.
  73. /// @returns errc::success if result has been successfully set, otherwise a
  74. /// platform-specific error_code.
  75. std::error_code identify_magic(const Twine &path, file_magic &result);
  76. } // namespace llvm
  77. #endif
  78. #ifdef __GNUC__
  79. #pragma GCC diagnostic pop
  80. #endif