IRObjectFile.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===- IRObjectFile.cpp - IR object file implementation ---------*- 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. //
  9. // Part of the IRObjectFile class implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/IRObjectFile.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/PointerUnion.h"
  15. #include "llvm/BinaryFormat/Magic.h"
  16. #include "llvm/Bitcode/BitcodeReader.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Object/ObjectFile.h"
  19. using namespace llvm;
  20. using namespace object;
  21. namespace llvm {
  22. class LLVMContext;
  23. class raw_ostream;
  24. } // namespace llvm
  25. IRObjectFile::IRObjectFile(MemoryBufferRef Object,
  26. std::vector<std::unique_ptr<Module>> Mods)
  27. : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
  28. for (auto &M : this->Mods)
  29. SymTab.addModule(M.get());
  30. }
  31. IRObjectFile::~IRObjectFile() = default;
  32. static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
  33. return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
  34. }
  35. void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
  36. Symb.p += sizeof(ModuleSymbolTable::Symbol);
  37. }
  38. Error IRObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
  39. SymTab.printSymbolName(OS, getSym(Symb));
  40. return Error::success();
  41. }
  42. Expected<uint32_t> IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
  43. return SymTab.getSymbolFlags(getSym(Symb));
  44. }
  45. basic_symbol_iterator IRObjectFile::symbol_begin() const {
  46. DataRefImpl Ret;
  47. Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
  48. return basic_symbol_iterator(BasicSymbolRef(Ret, this));
  49. }
  50. basic_symbol_iterator IRObjectFile::symbol_end() const {
  51. DataRefImpl Ret;
  52. Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
  53. SymTab.symbols().size());
  54. return basic_symbol_iterator(BasicSymbolRef(Ret, this));
  55. }
  56. StringRef IRObjectFile::getTargetTriple() const {
  57. // Each module must have the same target triple, so we arbitrarily access the
  58. // first one.
  59. return Mods[0]->getTargetTriple();
  60. }
  61. Expected<MemoryBufferRef>
  62. IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
  63. for (const SectionRef &Sec : Obj.sections()) {
  64. if (Sec.isBitcode()) {
  65. Expected<StringRef> Contents = Sec.getContents();
  66. if (!Contents)
  67. return Contents.takeError();
  68. if (Contents->size() <= 1)
  69. return errorCodeToError(object_error::bitcode_section_not_found);
  70. return MemoryBufferRef(*Contents, Obj.getFileName());
  71. }
  72. }
  73. return errorCodeToError(object_error::bitcode_section_not_found);
  74. }
  75. Expected<MemoryBufferRef>
  76. IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
  77. file_magic Type = identify_magic(Object.getBuffer());
  78. switch (Type) {
  79. case file_magic::bitcode:
  80. return Object;
  81. case file_magic::elf_relocatable:
  82. case file_magic::macho_object:
  83. case file_magic::wasm_object:
  84. case file_magic::coff_object: {
  85. Expected<std::unique_ptr<ObjectFile>> ObjFile =
  86. ObjectFile::createObjectFile(Object, Type);
  87. if (!ObjFile)
  88. return ObjFile.takeError();
  89. return findBitcodeInObject(*ObjFile->get());
  90. }
  91. default:
  92. return errorCodeToError(object_error::invalid_file_type);
  93. }
  94. }
  95. Expected<std::unique_ptr<IRObjectFile>>
  96. IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
  97. Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
  98. if (!BCOrErr)
  99. return BCOrErr.takeError();
  100. Expected<std::vector<BitcodeModule>> BMsOrErr =
  101. getBitcodeModuleList(*BCOrErr);
  102. if (!BMsOrErr)
  103. return BMsOrErr.takeError();
  104. std::vector<std::unique_ptr<Module>> Mods;
  105. for (auto BM : *BMsOrErr) {
  106. Expected<std::unique_ptr<Module>> MOrErr =
  107. BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
  108. /*IsImporting*/ false);
  109. if (!MOrErr)
  110. return MOrErr.takeError();
  111. Mods.push_back(std::move(*MOrErr));
  112. }
  113. return std::unique_ptr<IRObjectFile>(
  114. new IRObjectFile(*BCOrErr, std::move(Mods)));
  115. }
  116. Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
  117. IRSymtabFile F;
  118. Expected<MemoryBufferRef> BCOrErr =
  119. IRObjectFile::findBitcodeInMemBuffer(MBRef);
  120. if (!BCOrErr)
  121. return BCOrErr.takeError();
  122. Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr);
  123. if (!BFCOrErr)
  124. return BFCOrErr.takeError();
  125. Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr);
  126. if (!FCOrErr)
  127. return FCOrErr.takeError();
  128. F.Mods = std::move(BFCOrErr->Mods);
  129. F.Symtab = std::move(FCOrErr->Symtab);
  130. F.Strtab = std::move(FCOrErr->Strtab);
  131. F.TheReader = std::move(FCOrErr->TheReader);
  132. return std::move(F);
  133. }