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