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/Object/IRSymtab.h"
17 #include "llvm/Object/ModuleSymbolTable.h"
18 #include "llvm/Object/SymbolicFile.h"
19 
20 namespace llvm {
21 class BitcodeModule;
22 class Module;
23 
24 namespace object {
25 class ObjectFile;
26 
27 class IRObjectFile : public SymbolicFile {
28   std::vector<std::unique_ptr<Module>> Mods;
29   ModuleSymbolTable SymTab;
30   IRObjectFile(MemoryBufferRef Object,
31                std::vector<std::unique_ptr<Module>> Mods);
32 
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 
41   StringRef getTargetTriple() const;
42 
43   static bool classof(const Binary *v) {
44     return v->isIR();
45   }
46 
47   using module_iterator =
48       pointee_iterator<std::vector<std::unique_ptr<Module>>::const_iterator,
49                        const Module>;
50 
51   module_iterator module_begin() const { return module_iterator(Mods.begin()); }
52   module_iterator module_end() const { return module_iterator(Mods.end()); }
53 
54   iterator_range<module_iterator> modules() const {
55     return make_range(module_begin(), module_end());
56   }
57 
58   /// Finds and returns bitcode embedded in the given object file, or an
59   /// error code if not found.
60   static Expected<MemoryBufferRef> findBitcodeInObject(const ObjectFile &Obj);
61 
62   /// Finds and returns bitcode in the given memory buffer (which may
63   /// be either a bitcode file or a native object file with embedded bitcode),
64   /// or an error code if not found.
65   static Expected<MemoryBufferRef>
66   findBitcodeInMemBuffer(MemoryBufferRef Object);
67 
68   static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object,
69                                                         LLVMContext &Context);
70 };
71 
72 /// The contents of a bitcode file and its irsymtab. Any underlying data
73 /// for the irsymtab are owned by Symtab and Strtab.
74 struct IRSymtabFile {
75   std::vector<BitcodeModule> Mods;
76   SmallVector<char, 0> Symtab, Strtab;
77   irsymtab::Reader TheReader;
78 };
79 
80 /// Reads a bitcode file, creating its irsymtab if necessary.
81 Expected<IRSymtabFile> readIRSymtab(MemoryBufferRef MBRef);
82 
83 }
84 
85 }
86 
87 #endif
88