SymbolicFile.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
  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. //
  9. // This file defines a file format independent SymbolicFile class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/SymbolicFile.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/BinaryFormat/Magic.h"
  15. #include "llvm/Object/COFFImportFile.h"
  16. #include "llvm/Object/Error.h"
  17. #include "llvm/Object/IRObjectFile.h"
  18. #include "llvm/Object/ObjectFile.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include <memory>
  22. using namespace llvm;
  23. using namespace object;
  24. namespace llvm {
  25. class LLVMContext;
  26. }
  27. SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
  28. : Binary(Type, Source) {}
  29. SymbolicFile::~SymbolicFile() = default;
  30. Expected<std::unique_ptr<SymbolicFile>>
  31. SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type,
  32. LLVMContext *Context, bool InitContent) {
  33. StringRef Data = Object.getBuffer();
  34. if (Type == file_magic::unknown)
  35. Type = identify_magic(Data);
  36. if (!isSymbolicFile(Type, Context))
  37. return errorCodeToError(object_error::invalid_file_type);
  38. switch (Type) {
  39. case file_magic::bitcode:
  40. // Context is guaranteed to be non-null here, because bitcode magic only
  41. // indicates a symbolic file when Context is non-null.
  42. return IRObjectFile::create(Object, *Context);
  43. case file_magic::elf:
  44. case file_magic::elf_executable:
  45. case file_magic::elf_shared_object:
  46. case file_magic::elf_core:
  47. case file_magic::goff_object:
  48. case file_magic::macho_executable:
  49. case file_magic::macho_fixed_virtual_memory_shared_lib:
  50. case file_magic::macho_core:
  51. case file_magic::macho_preload_executable:
  52. case file_magic::macho_dynamically_linked_shared_lib:
  53. case file_magic::macho_dynamic_linker:
  54. case file_magic::macho_bundle:
  55. case file_magic::macho_dynamically_linked_shared_lib_stub:
  56. case file_magic::macho_dsym_companion:
  57. case file_magic::macho_kext_bundle:
  58. case file_magic::macho_file_set:
  59. case file_magic::pecoff_executable:
  60. case file_magic::xcoff_object_32:
  61. case file_magic::xcoff_object_64:
  62. case file_magic::wasm_object:
  63. return ObjectFile::createObjectFile(Object, Type, InitContent);
  64. case file_magic::coff_import_library:
  65. return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
  66. case file_magic::elf_relocatable:
  67. case file_magic::macho_object:
  68. case file_magic::coff_object: {
  69. Expected<std::unique_ptr<ObjectFile>> Obj =
  70. ObjectFile::createObjectFile(Object, Type, InitContent);
  71. if (!Obj || !Context)
  72. return std::move(Obj);
  73. Expected<MemoryBufferRef> BCData =
  74. IRObjectFile::findBitcodeInObject(*Obj->get());
  75. if (!BCData) {
  76. consumeError(BCData.takeError());
  77. return std::move(Obj);
  78. }
  79. return IRObjectFile::create(
  80. MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
  81. *Context);
  82. }
  83. default:
  84. llvm_unreachable("Unexpected Binary File Type");
  85. }
  86. }
  87. bool SymbolicFile::isSymbolicFile(file_magic Type, const LLVMContext *Context) {
  88. switch (Type) {
  89. case file_magic::bitcode:
  90. return Context != nullptr;
  91. case file_magic::elf:
  92. case file_magic::elf_executable:
  93. case file_magic::elf_shared_object:
  94. case file_magic::elf_core:
  95. case file_magic::goff_object:
  96. case file_magic::macho_executable:
  97. case file_magic::macho_fixed_virtual_memory_shared_lib:
  98. case file_magic::macho_core:
  99. case file_magic::macho_preload_executable:
  100. case file_magic::macho_dynamically_linked_shared_lib:
  101. case file_magic::macho_dynamic_linker:
  102. case file_magic::macho_bundle:
  103. case file_magic::macho_dynamically_linked_shared_lib_stub:
  104. case file_magic::macho_dsym_companion:
  105. case file_magic::macho_kext_bundle:
  106. case file_magic::macho_file_set:
  107. case file_magic::pecoff_executable:
  108. case file_magic::xcoff_object_32:
  109. case file_magic::xcoff_object_64:
  110. case file_magic::wasm_object:
  111. case file_magic::coff_import_library:
  112. case file_magic::elf_relocatable:
  113. case file_magic::macho_object:
  114. case file_magic::coff_object:
  115. return true;
  116. default:
  117. return false;
  118. }
  119. }