1 //===- IRObjectFile.h - LLVM 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 // This file declares the IRObjectFile template class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_OBJECT_IROBJECTFILE_H 14 #define LLVM_OBJECT_IROBJECTFILE_H 15 16 #include "llvm/Bitcode/BitcodeReader.h" 17 #include "llvm/Object/IRSymtab.h" 18 #include "llvm/Object/ModuleSymbolTable.h" 19 #include "llvm/Object/SymbolicFile.h" 20 #include "llvm/Support/Compiler.h" 21 22 namespace llvm { 23 class Module; 24 25 namespace object { 26 class ObjectFile; 27 28 class LLVM_ABI 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 34 public: 35 ~IRObjectFile() override; 36 void moveSymbolNext(DataRefImpl &Symb) const override; 37 Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override; 38 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override; 39 basic_symbol_iterator symbol_begin() const override; 40 basic_symbol_iterator symbol_end() const override; is64Bit()41 bool is64Bit() const override { 42 return Triple(getTargetTriple()).isArch64Bit(); 43 } 44 StringRef getTargetTriple() const; 45 classof(const Binary * v)46 static bool classof(const Binary *v) { 47 return v->isIR(); 48 } 49 50 using module_iterator = 51 pointee_iterator<std::vector<std::unique_ptr<Module>>::const_iterator, 52 const Module>; 53 module_begin()54 module_iterator module_begin() const { return module_iterator(Mods.begin()); } module_end()55 module_iterator module_end() const { return module_iterator(Mods.end()); } 56 modules()57 iterator_range<module_iterator> modules() const { 58 return make_range(module_begin(), module_end()); 59 } 60 61 /// Finds and returns bitcode embedded in the given object file, or an 62 /// error code if not found. 63 static Expected<MemoryBufferRef> findBitcodeInObject(const ObjectFile &Obj); 64 65 /// Finds and returns bitcode in the given memory buffer (which may 66 /// be either a bitcode file or a native object file with embedded bitcode), 67 /// or an error code if not found. 68 static Expected<MemoryBufferRef> 69 findBitcodeInMemBuffer(MemoryBufferRef Object); 70 71 static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object, 72 LLVMContext &Context); 73 }; 74 75 /// The contents of a bitcode file and its irsymtab. Any underlying data 76 /// for the irsymtab are owned by Symtab and Strtab. 77 struct IRSymtabFile { 78 std::vector<BitcodeModule> Mods; 79 SmallVector<char, 0> Symtab, Strtab; 80 irsymtab::Reader TheReader; 81 }; 82 83 /// Reads a bitcode file, creating its irsymtab if necessary. 84 LLVM_ABI Expected<IRSymtabFile> readIRSymtab(MemoryBufferRef MBRef); 85 } 86 87 } // namespace llvm 88 89 #endif 90