1 //===- IRObjectFile.h - LLVM IR object file implementation ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the IRObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_IROBJECTFILE_H
15 #define LLVM_OBJECT_IROBJECTFILE_H
16 
17 #include "llvm/Object/SymbolicFile.h"
18 
19 namespace llvm {
20 class Mangler;
21 class Module;
22 class GlobalValue;
23 class Triple;
24 
25 namespace object {
26 class ObjectFile;
27 
28 class IRObjectFile : public SymbolicFile {
29   std::unique_ptr<Module> M;
30   std::unique_ptr<Mangler> Mang;
31   std::vector<std::pair<std::string, uint32_t>> AsmSymbols;
32 
33 public:
34   IRObjectFile(MemoryBufferRef Object, std::unique_ptr<Module> M);
35   ~IRObjectFile() override;
36   void moveSymbolNext(DataRefImpl &Symb) const override;
37   std::error_code printSymbolName(raw_ostream &OS,
38                                   DataRefImpl Symb) const override;
39   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
40   GlobalValue *getSymbolGV(DataRefImpl Symb);
getSymbolGV(DataRefImpl Symb)41   const GlobalValue *getSymbolGV(DataRefImpl Symb) const {
42     return const_cast<IRObjectFile *>(this)->getSymbolGV(Symb);
43   }
44   basic_symbol_iterator symbol_begin_impl() const override;
45   basic_symbol_iterator symbol_end_impl() const override;
46 
getModule()47   const Module &getModule() const {
48     return const_cast<IRObjectFile*>(this)->getModule();
49   }
getModule()50   Module &getModule() {
51     return *M;
52   }
53   std::unique_ptr<Module> takeModule();
54 
classof(const Binary * v)55   static inline bool classof(const Binary *v) {
56     return v->isIR();
57   }
58 
59   /// \brief Finds and returns bitcode embedded in the given object file, or an
60   /// error code if not found.
61   static ErrorOr<MemoryBufferRef> findBitcodeInObject(const ObjectFile &Obj);
62 
63   /// Parse inline ASM and collect the symbols that are not defined in
64   /// the current module.
65   ///
66   /// For each found symbol, call \p AsmUndefinedRefs with the name of the
67   /// symbol found and the associated flags.
68   static void CollectAsmUndefinedRefs(
69       const Triple &TheTriple, StringRef InlineAsm,
70       function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmUndefinedRefs);
71 
72   /// \brief Finds and returns bitcode in the given memory buffer (which may
73   /// be either a bitcode file or a native object file with embedded bitcode),
74   /// or an error code if not found.
75   static ErrorOr<MemoryBufferRef>
76   findBitcodeInMemBuffer(MemoryBufferRef Object);
77 
78   static ErrorOr<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object,
79                                                        LLVMContext &Context);
80 };
81 }
82 }
83 
84 #endif
85