SymbolicFile.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===- SymbolicFile.cpp - Interface that only provides symbols --*- 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 a file format independent SymbolicFile class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/IRObjectFile.h"
  14. #include "llvm/Object/ObjectFile.h"
  15. #include "llvm/Object/SymbolicFile.h"
  16. #include "llvm/Support/MemoryBuffer.h"
  17. using namespace llvm;
  18. using namespace object;
  19. SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
  20. : Binary(Type, Source) {}
  21. SymbolicFile::~SymbolicFile() {}
  22. ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
  23. MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
  24. StringRef Data = Object.getBuffer();
  25. if (Type == sys::fs::file_magic::unknown)
  26. Type = sys::fs::identify_magic(Data);
  27. switch (Type) {
  28. case sys::fs::file_magic::bitcode:
  29. if (Context)
  30. return IRObjectFile::create(Object, *Context);
  31. // Fallthrough
  32. case sys::fs::file_magic::unknown:
  33. case sys::fs::file_magic::archive:
  34. case sys::fs::file_magic::macho_universal_binary:
  35. case sys::fs::file_magic::windows_resource:
  36. return object_error::invalid_file_type;
  37. case sys::fs::file_magic::elf:
  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_executable:
  42. case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
  43. case sys::fs::file_magic::macho_core:
  44. case sys::fs::file_magic::macho_preload_executable:
  45. case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
  46. case sys::fs::file_magic::macho_dynamic_linker:
  47. case sys::fs::file_magic::macho_bundle:
  48. case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
  49. case sys::fs::file_magic::macho_dsym_companion:
  50. case sys::fs::file_magic::macho_kext_bundle:
  51. case sys::fs::file_magic::coff_import_library:
  52. case sys::fs::file_magic::pecoff_executable:
  53. return ObjectFile::createObjectFile(Object, Type);
  54. case sys::fs::file_magic::elf_relocatable:
  55. case sys::fs::file_magic::macho_object:
  56. case sys::fs::file_magic::coff_object: {
  57. ErrorOr<std::unique_ptr<ObjectFile>> Obj =
  58. ObjectFile::createObjectFile(Object, Type);
  59. if (!Obj || !Context)
  60. return std::move(Obj);
  61. ErrorOr<MemoryBufferRef> BCData =
  62. IRObjectFile::findBitcodeInObject(*Obj->get());
  63. if (!BCData)
  64. return std::move(Obj);
  65. return IRObjectFile::create(
  66. MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
  67. *Context);
  68. }
  69. }
  70. llvm_unreachable("Unexpected Binary File Type");
  71. }