IRObjectFile.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- IRObjectFile.h - LLVM IR object file implementation ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the IRObjectFile template class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECT_IROBJECTFILE_H
  18. #define LLVM_OBJECT_IROBJECTFILE_H
  19. #include "llvm/ADT/PointerUnion.h"
  20. #include "llvm/Object/IRSymtab.h"
  21. #include "llvm/Object/ModuleSymbolTable.h"
  22. #include "llvm/Object/SymbolicFile.h"
  23. namespace llvm {
  24. class BitcodeModule;
  25. class Module;
  26. namespace object {
  27. class ObjectFile;
  28. class IRObjectFile : public SymbolicFile {
  29. std::vector<std::unique_ptr<Module>> Mods;
  30. ModuleSymbolTable SymTab;
  31. IRObjectFile(MemoryBufferRef Object,
  32. std::vector<std::unique_ptr<Module>> Mods);
  33. public:
  34. ~IRObjectFile() override;
  35. void moveSymbolNext(DataRefImpl &Symb) const override;
  36. Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override;
  37. Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
  38. basic_symbol_iterator symbol_begin() const override;
  39. basic_symbol_iterator symbol_end() const override;
  40. StringRef getTargetTriple() const;
  41. static bool classof(const Binary *v) {
  42. return v->isIR();
  43. }
  44. using module_iterator =
  45. pointee_iterator<std::vector<std::unique_ptr<Module>>::const_iterator,
  46. const Module>;
  47. module_iterator module_begin() const { return module_iterator(Mods.begin()); }
  48. module_iterator module_end() const { return module_iterator(Mods.end()); }
  49. iterator_range<module_iterator> modules() const {
  50. return make_range(module_begin(), module_end());
  51. }
  52. /// Finds and returns bitcode embedded in the given object file, or an
  53. /// error code if not found.
  54. static Expected<MemoryBufferRef> findBitcodeInObject(const ObjectFile &Obj);
  55. /// Finds and returns bitcode in the given memory buffer (which may
  56. /// be either a bitcode file or a native object file with embedded bitcode),
  57. /// or an error code if not found.
  58. static Expected<MemoryBufferRef>
  59. findBitcodeInMemBuffer(MemoryBufferRef Object);
  60. static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object,
  61. LLVMContext &Context);
  62. };
  63. /// The contents of a bitcode file and its irsymtab. Any underlying data
  64. /// for the irsymtab are owned by Symtab and Strtab.
  65. struct IRSymtabFile {
  66. std::vector<BitcodeModule> Mods;
  67. SmallVector<char, 0> Symtab, Strtab;
  68. irsymtab::Reader TheReader;
  69. };
  70. /// Reads a bitcode file, creating its irsymtab if necessary.
  71. Expected<IRSymtabFile> readIRSymtab(MemoryBufferRef MBRef);
  72. }
  73. }
  74. #endif
  75. #ifdef __GNUC__
  76. #pragma GCC diagnostic pop
  77. #endif