Magic.h 3.1 KB

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