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