1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
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 LTOModule class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LTO_LTOMODULE_H
15 #define LLVM_LTO_LTOMODULE_H
16 
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/Object/IRObjectFile.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <string>
26 #include <vector>
27 
28 // Forward references to llvm classes.
29 namespace llvm {
30   class Function;
31   class GlobalValue;
32   class MemoryBuffer;
33   class TargetOptions;
34   class Value;
35 
36 //===----------------------------------------------------------------------===//
37 /// C++ class which implements the opaque lto_module_t type.
38 ///
39 struct LTOModule {
40 private:
41   struct NameAndAttributes {
42     const char        *name;
43     uint32_t           attributes;
44     bool               isFunction;
45     const GlobalValue *symbol;
46   };
47 
48   std::unique_ptr<LLVMContext> OwnedContext;
49 
50   std::unique_ptr<object::IRObjectFile> IRFile;
51   std::unique_ptr<TargetMachine> _target;
52   StringSet<>                             _linkeropt_strings;
53   std::vector<const char *>               _deplibs;
54   std::vector<const char *>               _linkeropts;
55   std::vector<NameAndAttributes>          _symbols;
56 
57   // _defines and _undefines only needed to disambiguate tentative definitions
58   StringSet<>                             _defines;
59   StringMap<NameAndAttributes> _undefines;
60   std::vector<const char*>                _asm_undefines;
61 
62   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
63   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
64             std::unique_ptr<LLVMContext> Context);
65 
66 public:
67   ~LTOModule();
68 
69   /// Returns 'true' if the file or memory contents is LLVM bitcode.
70   static bool isBitcodeFile(const void *mem, size_t length);
71   static bool isBitcodeFile(const char *path);
72 
73   /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
74   /// triple.
75   static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
76                                  StringRef triplePrefix);
77 
78   /// Create a MemoryBuffer from a memory range with an optional name.
79   static std::unique_ptr<MemoryBuffer>
80   makeBuffer(const void *mem, size_t length, StringRef name = "");
81 
82   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
83   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
84   /// and the AsmParsers by calling:
85   ///
86   /// InitializeAllTargets();
87   /// InitializeAllTargetMCs();
88   /// InitializeAllAsmPrinters();
89   /// InitializeAllAsmParsers();
90   static LTOModule *createFromFile(const char *path, TargetOptions options,
91                                    std::string &errMsg);
92   static LTOModule *createFromOpenFile(int fd, const char *path, size_t size,
93                                        TargetOptions options,
94                                        std::string &errMsg);
95   static LTOModule *createFromOpenFileSlice(int fd, const char *path,
96                                             size_t map_size, off_t offset,
97                                             TargetOptions options,
98                                             std::string &errMsg);
99   static LTOModule *createFromBuffer(const void *mem, size_t length,
100                                      TargetOptions options, std::string &errMsg,
101                                      StringRef path = "");
102 
103   static LTOModule *createInLocalContext(const void *mem, size_t length,
104                                          TargetOptions options,
105                                          std::string &errMsg, StringRef path);
106   static LTOModule *createInContext(const void *mem, size_t length,
107                                     TargetOptions options, std::string &errMsg,
108                                     StringRef path, LLVMContext *Context);
109 
getModuleLTOModule110   const Module &getModule() const {
111     return const_cast<LTOModule*>(this)->getModule();
112   }
getModuleLTOModule113   Module &getModule() {
114     return IRFile->getModule();
115   }
116 
117   /// Return the Module's target triple.
getTargetTripleLTOModule118   const std::string &getTargetTriple() {
119     return getModule().getTargetTriple();
120   }
121 
122   /// Set the Module's target triple.
setTargetTripleLTOModule123   void setTargetTriple(StringRef Triple) {
124     getModule().setTargetTriple(Triple);
125   }
126 
127   /// Get the number of symbols
getSymbolCountLTOModule128   uint32_t getSymbolCount() {
129     return _symbols.size();
130   }
131 
132   /// Get the attributes for a symbol at the specified index.
getSymbolAttributesLTOModule133   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
134     if (index < _symbols.size())
135       return lto_symbol_attributes(_symbols[index].attributes);
136     return lto_symbol_attributes(0);
137   }
138 
139   /// Get the name of the symbol at the specified index.
getSymbolNameLTOModule140   const char *getSymbolName(uint32_t index) {
141     if (index < _symbols.size())
142       return _symbols[index].name;
143     return nullptr;
144   }
145 
146   /// Get the number of dependent libraries
getDependentLibraryCountLTOModule147   uint32_t getDependentLibraryCount() {
148     return _deplibs.size();
149   }
150 
151   /// Get the dependent library at the specified index.
getDependentLibraryLTOModule152   const char *getDependentLibrary(uint32_t index) {
153     if (index < _deplibs.size())
154       return _deplibs[index];
155     return nullptr;
156   }
157 
158   /// Get the number of linker options
getLinkerOptCountLTOModule159   uint32_t getLinkerOptCount() {
160     return _linkeropts.size();
161   }
162 
163   /// Get the linker option at the specified index.
getLinkerOptLTOModule164   const char *getLinkerOpt(uint32_t index) {
165     if (index < _linkeropts.size())
166       return _linkeropts[index];
167     return nullptr;
168   }
169 
getAsmUndefinedRefsLTOModule170   const std::vector<const char*> &getAsmUndefinedRefs() {
171     return _asm_undefines;
172   }
173 
174 private:
175   /// Parse metadata from the module
176   // FIXME: it only parses "Linker Options" metadata at the moment
177   void parseMetadata();
178 
179   /// Parse the symbols from the module and model-level ASM and add them to
180   /// either the defined or undefined lists.
181   bool parseSymbols(std::string &errMsg);
182 
183   /// Add a symbol which isn't defined just yet to a list to be resolved later.
184   void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
185                                    bool isFunc);
186 
187   /// Add a defined symbol to the list.
188   void addDefinedSymbol(const char *Name, const GlobalValue *def,
189                         bool isFunction);
190 
191   /// Add a data symbol as defined to the list.
192   void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
193   void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
194 
195   /// Add a function symbol as defined to the list.
196   void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
197   void addDefinedFunctionSymbol(const char *Name, const Function *F);
198 
199   /// Add a global symbol from module-level ASM to the defined list.
200   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
201 
202   /// Add a global symbol from module-level ASM to the undefined list.
203   void addAsmGlobalSymbolUndef(const char *);
204 
205   /// Parse i386/ppc ObjC class data structure.
206   void addObjCClass(const GlobalVariable *clgv);
207 
208   /// Parse i386/ppc ObjC category data structure.
209   void addObjCCategory(const GlobalVariable *clgv);
210 
211   /// Parse i386/ppc ObjC class list data structure.
212   void addObjCClassRef(const GlobalVariable *clgv);
213 
214   /// Get string that the data pointer points to.
215   bool objcClassNameFromExpression(const Constant *c, std::string &name);
216 
217   /// Create an LTOModule (private version).
218   static LTOModule *makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
219                                   std::string &errMsg, LLVMContext *Context);
220 };
221 }
222 #endif
223