Magic.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 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. minidump, ///< Windows minidump file
  44. coff_cl_gl_object, ///< Microsoft cl.exe's intermediate code file
  45. coff_object, ///< COFF object file
  46. coff_import_library, ///< COFF import library
  47. pecoff_executable, ///< PECOFF executable file
  48. windows_resource, ///< Windows compiled resource file (.res)
  49. xcoff_object_32, ///< 32-bit XCOFF object file
  50. xcoff_object_64, ///< 64-bit XCOFF object file
  51. wasm_object, ///< WebAssembly Object file
  52. pdb, ///< Windows PDB debug info file
  53. tapi_file, ///< Text-based Dynamic Library Stub file
  54. };
  55. bool is_object() const { return V != unknown; }
  56. file_magic() = default;
  57. file_magic(Impl V) : V(V) {}
  58. operator Impl() const { return V; }
  59. private:
  60. Impl V = unknown;
  61. };
  62. /// Identify the type of a binary file based on how magical it is.
  63. file_magic identify_magic(StringRef magic);
  64. /// Get and identify \a path's type based on its content.
  65. ///
  66. /// @param path Input path.
  67. /// @param result Set to the type of file, or file_magic::unknown.
  68. /// @returns errc::success if result has been successfully set, otherwise a
  69. /// platform-specific error_code.
  70. std::error_code identify_magic(const Twine &path, file_magic &result);
  71. } // namespace llvm
  72. #endif
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif