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