Binary.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the Binary class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/Binary.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Support/FileSystem.h"
  16. #include "llvm/Support/MemoryBuffer.h"
  17. #include "llvm/Support/Path.h"
  18. // Include headers for createBinary.
  19. #include "llvm/Object/Archive.h"
  20. #include "llvm/Object/MachOUniversal.h"
  21. #include "llvm/Object/ObjectFile.h"
  22. using namespace llvm;
  23. using namespace object;
  24. Binary::~Binary() {}
  25. Binary::Binary(unsigned int Type, MemoryBufferRef Source)
  26. : TypeID(Type), Data(Source) {}
  27. StringRef Binary::getData() const { return Data.getBuffer(); }
  28. StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
  29. MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
  30. ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
  31. LLVMContext *Context) {
  32. sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
  33. switch (Type) {
  34. case sys::fs::file_magic::archive:
  35. return Archive::create(Buffer);
  36. case sys::fs::file_magic::elf:
  37. case sys::fs::file_magic::elf_relocatable:
  38. case sys::fs::file_magic::elf_executable:
  39. case sys::fs::file_magic::elf_shared_object:
  40. case sys::fs::file_magic::elf_core:
  41. case sys::fs::file_magic::macho_object:
  42. case sys::fs::file_magic::macho_executable:
  43. case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
  44. case sys::fs::file_magic::macho_core:
  45. case sys::fs::file_magic::macho_preload_executable:
  46. case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
  47. case sys::fs::file_magic::macho_dynamic_linker:
  48. case sys::fs::file_magic::macho_bundle:
  49. case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
  50. case sys::fs::file_magic::macho_dsym_companion:
  51. case sys::fs::file_magic::macho_kext_bundle:
  52. case sys::fs::file_magic::coff_object:
  53. case sys::fs::file_magic::coff_import_library:
  54. case sys::fs::file_magic::pecoff_executable:
  55. case sys::fs::file_magic::bitcode:
  56. return ObjectFile::createSymbolicFile(Buffer, Type, Context);
  57. case sys::fs::file_magic::macho_universal_binary:
  58. // return MachOUniversalBinary::create(Buffer); // HLSL Change - remove support for MachO files
  59. case sys::fs::file_magic::unknown:
  60. case sys::fs::file_magic::windows_resource:
  61. // Unrecognized object file format.
  62. return object_error::invalid_file_type;
  63. }
  64. llvm_unreachable("Unexpected Binary File Type");
  65. }
  66. ErrorOr<OwningBinary<Binary>> object::createBinary(StringRef Path) {
  67. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  68. MemoryBuffer::getFileOrSTDIN(Path);
  69. if (std::error_code EC = FileOrErr.getError())
  70. return EC;
  71. std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
  72. ErrorOr<std::unique_ptr<Binary>> BinOrErr =
  73. createBinary(Buffer->getMemBufferRef());
  74. if (std::error_code EC = BinOrErr.getError())
  75. return EC;
  76. std::unique_ptr<Binary> &Bin = BinOrErr.get();
  77. return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
  78. }