Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 1 | //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===// |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOModule.h" |
| 16 | |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/OwningPtr.h" |
Viktor Kutuzov | e823db8 | 2009-11-18 20:20:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SystemUtils.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Host.h" |
| 27 | #include "llvm/Support/Path.h" |
| 28 | #include "llvm/Support/Process.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SourceMgr.h" |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 30 | #include "llvm/Support/system_error.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 31 | #include "llvm/Target/Mangler.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCExpr.h" |
| 35 | #include "llvm/MC/MCInst.h" |
| 36 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 37 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 40 | #include "llvm/MC/SubtargetFeature.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetAsmParser.h" |
Daniel Dunbar | ff9834a | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame^] | 43 | #include "llvm/Target/TargetRegisterInfo.h" |
Daniel Dunbar | ff9834a | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetRegistry.h" |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSelect.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 46 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 49 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { |
| 50 | return llvm::sys::IdentifyFileType((char*)mem, length) |
| 51 | == llvm::sys::Bitcode_FileType; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 54 | bool LTOModule::isBitcodeFile(const char *path) { |
| 55 | return llvm::sys::Path(path).isBitcodeFile(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 58 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, |
| 59 | const char *triplePrefix) { |
| 60 | MemoryBuffer *buffer = makeBuffer(mem, length); |
| 61 | if (!buffer) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 62 | return false; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 63 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 66 | |
| 67 | bool LTOModule::isBitcodeFileForTarget(const char *path, |
| 68 | const char *triplePrefix) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 69 | OwningPtr<MemoryBuffer> buffer; |
| 70 | if (MemoryBuffer::getFile(path, buffer)) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 71 | return false; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 72 | return isTargetMatch(buffer.take(), triplePrefix); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Takes ownership of buffer. |
| 76 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { |
Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 77 | std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); |
| 78 | delete buffer; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 79 | return (strncmp(Triple.c_str(), triplePrefix, |
Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 80 | strlen(triplePrefix)) == 0); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | LTOModule::LTOModule(Module *m, TargetMachine *t) |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 85 | : _module(m), _target(t) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 86 | { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | LTOModule *LTOModule::makeLTOModule(const char *path, |
| 90 | std::string &errMsg) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 91 | OwningPtr<MemoryBuffer> buffer; |
| 92 | if (error_code ec = MemoryBuffer::getFile(path, buffer)) { |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 93 | errMsg = ec.message(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 94 | return NULL; |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 95 | } |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 96 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 99 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 100 | size_t size, |
| 101 | std::string &errMsg) { |
| 102 | return makeLTOModule(fd, path, size, size, 0, errMsg); |
| 103 | } |
| 104 | |
| 105 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
| 106 | size_t file_size, |
| 107 | size_t map_size, |
| 108 | off_t offset, |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 109 | std::string &errMsg) { |
| 110 | OwningPtr<MemoryBuffer> buffer; |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 111 | if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size, |
| 112 | map_size, offset, false)) { |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 113 | errMsg = ec.message(); |
| 114 | return NULL; |
| 115 | } |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 116 | return makeLTOModule(buffer.take(), errMsg); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Rafael Espindola | 9916d2a | 2011-03-17 22:18:42 +0000 | [diff] [blame] | 119 | /// makeBuffer - Create a MemoryBuffer from a memory range. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 120 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { |
| 121 | const char *startPtr = (char*)mem; |
Rafael Espindola | 9916d2a | 2011-03-17 22:18:42 +0000 | [diff] [blame] | 122 | return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, |
| 127 | std::string &errMsg) { |
| 128 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
| 129 | if (!buffer) |
| 130 | return NULL; |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 131 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, |
| 135 | std::string &errMsg) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 136 | static bool Initialized = false; |
| 137 | if (!Initialized) { |
| 138 | InitializeAllTargets(); |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 139 | InitializeAllMCAsmInfos(); |
Cameron Zwarich | bf843e6 | 2011-07-11 22:19:51 +0000 | [diff] [blame] | 140 | InitializeAllMCSubtargetInfos(); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 141 | InitializeAllAsmParsers(); |
| 142 | Initialized = true; |
| 143 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 144 | |
| 145 | // parse bitcode buffer |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 146 | OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(), |
| 147 | &errMsg)); |
| 148 | if (!m) { |
| 149 | delete buffer; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 150 | return NULL; |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 151 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 152 | |
| 153 | std::string Triple = m->getTargetTriple(); |
| 154 | if (Triple.empty()) |
| 155 | Triple = sys::getHostTriple(); |
| 156 | |
| 157 | // find machine architecture for this module |
| 158 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
| 159 | if (!march) |
| 160 | return NULL; |
| 161 | |
Nick Lewycky | 333ed45 | 2011-04-21 01:54:08 +0000 | [diff] [blame] | 162 | // construct LTOModule, hand over ownership of module and target |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 163 | SubtargetFeatures Features; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 164 | Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 165 | std::string FeatureStr = Features.getString(); |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 166 | std::string CPU; |
| 167 | TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 168 | LTOModule *Ret = new LTOModule(m.take(), target); |
| 169 | bool Err = Ret->ParseSymbols(); |
| 170 | if (Err) { |
| 171 | delete Ret; |
| 172 | return NULL; |
| 173 | } |
| 174 | return Ret; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
| 178 | const char *LTOModule::getTargetTriple() { |
| 179 | return _module->getTargetTriple().c_str(); |
| 180 | } |
| 181 | |
| 182 | void LTOModule::setTargetTriple(const char *triple) { |
| 183 | _module->setTargetTriple(triple); |
| 184 | } |
| 185 | |
| 186 | void LTOModule::addDefinedFunctionSymbol(Function *f, Mangler &mangler) { |
| 187 | // add to list of defined symbols |
| 188 | addDefinedSymbol(f, mangler, true); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 191 | // Get string that data pointer points to. |
| 192 | bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { |
| 193 | if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { |
| 194 | Constant *op = ce->getOperand(0); |
| 195 | if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { |
| 196 | Constant *cn = gvn->getInitializer(); |
| 197 | if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) { |
| 198 | if (ca->isCString()) { |
Jay Foad | 4f91054 | 2011-06-28 08:24:19 +0000 | [diff] [blame] | 199 | name = ".objc_class_name_" + ca->getAsCString(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 200 | return true; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 201 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 202 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 203 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | // Parse i386/ppc ObjC class data structure. |
| 209 | void LTOModule::addObjCClass(GlobalVariable *clgv) { |
| 210 | if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 211 | // second slot in __OBJC,__class is pointer to superclass name |
| 212 | std::string superclassName; |
| 213 | if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { |
| 214 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 215 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 216 | _undefines.GetOrCreateValue(superclassName); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 217 | if (!entry.getValue().name) { |
| 218 | const char *symbolName = entry.getKey().data(); |
Daniel Dunbar | 8d0843d | 2010-08-11 00:11:17 +0000 | [diff] [blame] | 219 | info.name = symbolName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 220 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 221 | entry.setValue(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | // third slot in __OBJC,__class is pointer to class name |
| 225 | std::string className; |
| 226 | if (objcClassNameFromExpression(c->getOperand(2), className)) { |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 227 | StringSet::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 228 | _defines.GetOrCreateValue(className); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 229 | entry.setValue(1); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 230 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 231 | info.name = entry.getKey().data(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 232 | info.attributes = (lto_symbol_attributes) |
| 233 | (LTO_SYMBOL_PERMISSIONS_DATA | |
| 234 | LTO_SYMBOL_DEFINITION_REGULAR | |
| 235 | LTO_SYMBOL_SCOPE_DEFAULT); |
| 236 | _symbols.push_back(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 242 | // Parse i386/ppc ObjC category data structure. |
| 243 | void LTOModule::addObjCCategory(GlobalVariable *clgv) { |
| 244 | if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 245 | // second slot in __OBJC,__category is pointer to target class name |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 246 | std::string targetclassName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 247 | if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) { |
| 248 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 249 | |
| 250 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 251 | _undefines.GetOrCreateValue(targetclassName); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 252 | |
| 253 | if (entry.getValue().name) |
| 254 | return; |
| 255 | |
| 256 | const char *symbolName = entry.getKey().data(); |
| 257 | info.name = symbolName; |
| 258 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 259 | entry.setValue(info); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 260 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 261 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 265 | // Parse i386/ppc ObjC class list data structure. |
| 266 | void LTOModule::addObjCClassRef(GlobalVariable *clgv) { |
| 267 | std::string targetclassName; |
| 268 | if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 269 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 270 | |
| 271 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 272 | _undefines.GetOrCreateValue(targetclassName); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 273 | if (entry.getValue().name) |
| 274 | return; |
| 275 | |
| 276 | const char *symbolName = entry.getKey().data(); |
| 277 | info.name = symbolName; |
| 278 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 279 | entry.setValue(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | void LTOModule::addDefinedDataSymbol(GlobalValue *v, Mangler &mangler) { |
| 285 | // Add to list of defined symbols. |
| 286 | addDefinedSymbol(v, mangler, false); |
| 287 | |
| 288 | // Special case i386/ppc ObjC data structures in magic sections: |
| 289 | // The issue is that the old ObjC object format did some strange |
| 290 | // contortions to avoid real linker symbols. For instance, the |
| 291 | // ObjC class data structure is allocated statically in the executable |
| 292 | // that defines that class. That data structures contains a pointer to |
| 293 | // its superclass. But instead of just initializing that part of the |
| 294 | // struct to the address of its superclass, and letting the static and |
| 295 | // dynamic linkers do the rest, the runtime works by having that field |
| 296 | // instead point to a C-string that is the name of the superclass. |
| 297 | // At runtime the objc initialization updates that pointer and sets |
| 298 | // it to point to the actual super class. As far as the linker |
| 299 | // knows it is just a pointer to a string. But then someone wanted the |
| 300 | // linker to issue errors at build time if the superclass was not found. |
| 301 | // So they figured out a way in mach-o object format to use an absolute |
| 302 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 303 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 304 | // a class was missing. |
| 305 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 306 | // from the ObjC data structures generated by the front end. |
| 307 | if (v->hasSection() /* && isTargetDarwin */) { |
| 308 | // special case if this data blob is an ObjC class definition |
| 309 | if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) { |
| 310 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 311 | addObjCClass(gv); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // special case if this data blob is an ObjC category definition |
| 316 | else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) { |
| 317 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 318 | addObjCCategory(gv); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // special case if this data blob is the list of referenced classes |
| 323 | else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) { |
| 324 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 325 | addObjCClassRef(gv); |
| 326 | } |
| 327 | } |
| 328 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | void LTOModule::addDefinedSymbol(GlobalValue *def, Mangler &mangler, |
| 333 | bool isFunction) { |
| 334 | // ignore all llvm.* symbols |
| 335 | if (def->getName().startswith("llvm.")) |
| 336 | return; |
| 337 | |
| 338 | // string is owned by _defines |
Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 339 | SmallString<64> Buffer; |
| 340 | mangler.getNameWithPrefix(Buffer, def, false); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 341 | |
| 342 | // set alignment part log2() can have rounding errors |
| 343 | uint32_t align = def->getAlignment(); |
| 344 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
| 345 | |
| 346 | // set permissions part |
| 347 | if (isFunction) |
| 348 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
| 349 | else { |
| 350 | GlobalVariable *gv = dyn_cast<GlobalVariable>(def); |
| 351 | if (gv && gv->isConstant()) |
| 352 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 353 | else |
| 354 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 355 | } |
| 356 | |
| 357 | // set definition part |
Bill Wendling | 563ef5e | 2010-09-27 18:05:19 +0000 | [diff] [blame] | 358 | if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() || |
| 359 | def->hasLinkerPrivateWeakLinkage() || |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 360 | def->hasLinkerPrivateWeakDefAutoLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 361 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 362 | else if (def->hasCommonLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 363 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 364 | else |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 365 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 366 | |
| 367 | // set scope part |
| 368 | if (def->hasHiddenVisibility()) |
| 369 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 370 | else if (def->hasProtectedVisibility()) |
| 371 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 372 | else if (def->hasExternalLinkage() || def->hasWeakLinkage() || |
| 373 | def->hasLinkOnceLinkage() || def->hasCommonLinkage() || |
| 374 | def->hasLinkerPrivateWeakLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 375 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 376 | else if (def->hasLinkerPrivateWeakDefAutoLinkage()) |
| 377 | attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 378 | else |
| 379 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 380 | |
| 381 | // add to table of symbols |
| 382 | NameAndAttributes info; |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 383 | StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 384 | entry.setValue(1); |
| 385 | |
| 386 | StringRef Name = entry.getKey(); |
| 387 | info.name = Name.data(); |
| 388 | assert(info.name[Name.size()] == '\0'); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 389 | info.attributes = (lto_symbol_attributes)attr; |
| 390 | _symbols.push_back(info); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 393 | void LTOModule::addAsmGlobalSymbol(const char *name, |
| 394 | lto_symbol_attributes scope) { |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 395 | StringSet::value_type &entry = _defines.GetOrCreateValue(name); |
| 396 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 397 | // only add new define if not already defined |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 398 | if (entry.getValue()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 399 | return; |
| 400 | |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 401 | entry.setValue(1); |
| 402 | const char *symbolName = entry.getKey().data(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 403 | uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR; |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 404 | attr |= scope; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 405 | NameAndAttributes info; |
| 406 | info.name = symbolName; |
| 407 | info.attributes = (lto_symbol_attributes)attr; |
| 408 | _symbols.push_back(info); |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 409 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 410 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 411 | void LTOModule::addAsmGlobalSymbolUndef(const char *name) { |
| 412 | StringMap<NameAndAttributes>::value_type &entry = |
| 413 | _undefines.GetOrCreateValue(name); |
| 414 | |
| 415 | _asm_undefines.push_back(entry.getKey().data()); |
| 416 | |
| 417 | // we already have the symbol |
| 418 | if (entry.getValue().name) |
| 419 | return; |
| 420 | |
| 421 | uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;; |
| 422 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 423 | NameAndAttributes info; |
| 424 | info.name = entry.getKey().data(); |
| 425 | info.attributes = (lto_symbol_attributes)attr; |
| 426 | |
| 427 | entry.setValue(info); |
| 428 | } |
| 429 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 430 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl, |
| 431 | Mangler &mangler) { |
| 432 | // ignore all llvm.* symbols |
| 433 | if (decl->getName().startswith("llvm.")) |
| 434 | return; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 435 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 436 | // ignore all aliases |
| 437 | if (isa<GlobalAlias>(decl)) |
| 438 | return; |
Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 439 | |
Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 440 | SmallString<64> name; |
| 441 | mangler.getNameWithPrefix(name, decl, false); |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 442 | |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 443 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 444 | _undefines.GetOrCreateValue(name); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 445 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 446 | // we already have the symbol |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 447 | if (entry.getValue().name) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 448 | return; |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 449 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 450 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 451 | |
| 452 | info.name = entry.getKey().data(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 453 | if (decl->hasExternalWeakLinkage()) |
| 454 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 455 | else |
| 456 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 457 | |
| 458 | entry.setValue(info); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 462 | namespace { |
| 463 | class RecordStreamer : public MCStreamer { |
| 464 | public: |
| 465 | enum State { NeverSeen, Global, Defined, DefinedGlobal, Used}; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 466 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 467 | private: |
| 468 | StringMap<State> Symbols; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 469 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 470 | void markDefined(const MCSymbol &Symbol) { |
| 471 | State &S = Symbols[Symbol.getName()]; |
| 472 | switch (S) { |
| 473 | case DefinedGlobal: |
| 474 | case Global: |
| 475 | S = DefinedGlobal; |
| 476 | break; |
| 477 | case NeverSeen: |
| 478 | case Defined: |
| 479 | case Used: |
| 480 | S = Defined; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | void markGlobal(const MCSymbol &Symbol) { |
| 485 | State &S = Symbols[Symbol.getName()]; |
| 486 | switch (S) { |
| 487 | case DefinedGlobal: |
| 488 | case Defined: |
| 489 | S = DefinedGlobal; |
| 490 | break; |
| 491 | |
| 492 | case NeverSeen: |
| 493 | case Global: |
| 494 | case Used: |
| 495 | S = Global; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | void markUsed(const MCSymbol &Symbol) { |
| 500 | State &S = Symbols[Symbol.getName()]; |
| 501 | switch (S) { |
| 502 | case DefinedGlobal: |
| 503 | case Defined: |
| 504 | case Global: |
| 505 | break; |
| 506 | |
| 507 | case NeverSeen: |
| 508 | case Used: |
| 509 | S = Used; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // FIXME: mostly copied for the obj streamer. |
| 515 | void AddValueSymbols(const MCExpr *Value) { |
| 516 | switch (Value->getKind()) { |
| 517 | case MCExpr::Target: |
| 518 | // FIXME: What should we do in here? |
| 519 | break; |
| 520 | |
| 521 | case MCExpr::Constant: |
| 522 | break; |
| 523 | |
| 524 | case MCExpr::Binary: { |
| 525 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 526 | AddValueSymbols(BE->getLHS()); |
| 527 | AddValueSymbols(BE->getRHS()); |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | case MCExpr::SymbolRef: |
| 532 | markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 533 | break; |
| 534 | |
| 535 | case MCExpr::Unary: |
| 536 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | public: |
| 542 | typedef StringMap<State>::const_iterator const_iterator; |
| 543 | |
| 544 | const_iterator begin() { |
| 545 | return Symbols.begin(); |
| 546 | } |
| 547 | |
| 548 | const_iterator end() { |
| 549 | return Symbols.end(); |
| 550 | } |
| 551 | |
| 552 | RecordStreamer(MCContext &Context) : MCStreamer(Context) {} |
| 553 | |
| 554 | virtual void ChangeSection(const MCSection *Section) {} |
| 555 | virtual void InitSections() {} |
| 556 | virtual void EmitLabel(MCSymbol *Symbol) { |
| 557 | Symbol->setSection(*getCurrentSection()); |
| 558 | markDefined(*Symbol); |
| 559 | } |
| 560 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {} |
| 561 | virtual void EmitThumbFunc(MCSymbol *Func) {} |
| 562 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 563 | // FIXME: should we handle aliases? |
| 564 | markDefined(*Symbol); |
| 565 | } |
| 566 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { |
| 567 | if (Attribute == MCSA_Global) |
| 568 | markGlobal(*Symbol); |
| 569 | } |
| 570 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {} |
| 571 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} |
| 572 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {} |
| 573 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) {} |
| 574 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 575 | unsigned Size , unsigned ByteAlignment) { |
| 576 | markDefined(*Symbol); |
| 577 | } |
| 578 | virtual void EmitCOFFSymbolType(int Type) {} |
| 579 | virtual void EndCOFFSymbolDef() {} |
| 580 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 581 | unsigned ByteAlignment) { |
| 582 | markDefined(*Symbol); |
| 583 | } |
| 584 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {} |
| 585 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {} |
| 586 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 587 | uint64_t Size, unsigned ByteAlignment) {} |
| 588 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {} |
| 589 | virtual void EmitValueImpl(const MCExpr *Value, unsigned Size, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 590 | unsigned AddrSpace) {} |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 591 | virtual void EmitULEB128Value(const MCExpr *Value) {} |
| 592 | virtual void EmitSLEB128Value(const MCExpr *Value) {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 593 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 594 | unsigned ValueSize, |
| 595 | unsigned MaxBytesToEmit) {} |
| 596 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 597 | unsigned MaxBytesToEmit) {} |
| 598 | virtual void EmitValueToOffset(const MCExpr *Offset, |
| 599 | unsigned char Value ) {} |
| 600 | virtual void EmitFileDirective(StringRef Filename) {} |
| 601 | virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 602 | const MCSymbol *LastLabel, |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 603 | const MCSymbol *Label, |
| 604 | unsigned PointerSize) {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 605 | |
| 606 | virtual void EmitInstruction(const MCInst &Inst) { |
| 607 | // Scan for values. |
| 608 | for (unsigned i = Inst.getNumOperands(); i--; ) |
| 609 | if (Inst.getOperand(i).isExpr()) |
| 610 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 611 | } |
| 612 | virtual void Finish() {} |
| 613 | }; |
| 614 | } |
| 615 | |
| 616 | bool LTOModule::addAsmGlobalSymbols(MCContext &Context) { |
| 617 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
| 618 | |
| 619 | OwningPtr<RecordStreamer> Streamer(new RecordStreamer(Context)); |
| 620 | MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm); |
| 621 | SourceMgr SrcMgr; |
| 622 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
| 623 | OwningPtr<MCAsmParser> Parser(createMCAsmParser(_target->getTarget(), SrcMgr, |
| 624 | Context, *Streamer, |
| 625 | *_target->getMCAsmInfo())); |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 626 | OwningPtr<MCSubtargetInfo> STI(_target->getTarget(). |
| 627 | createMCSubtargetInfo(_target->getTargetTriple(), |
| 628 | _target->getTargetCPU(), |
| 629 | _target->getTargetFeatureString())); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 630 | OwningPtr<TargetAsmParser> |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 631 | TAP(_target->getTarget().createAsmParser(*STI, *Parser.get())); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 632 | Parser->setTargetParser(*TAP); |
| 633 | int Res = Parser->Run(false); |
| 634 | if (Res) |
| 635 | return true; |
| 636 | |
| 637 | for (RecordStreamer::const_iterator i = Streamer->begin(), |
| 638 | e = Streamer->end(); i != e; ++i) { |
| 639 | StringRef Key = i->first(); |
| 640 | RecordStreamer::State Value = i->second; |
| 641 | if (Value == RecordStreamer::DefinedGlobal) |
| 642 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT); |
| 643 | else if (Value == RecordStreamer::Defined) |
| 644 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL); |
| 645 | else if (Value == RecordStreamer::Global || |
| 646 | Value == RecordStreamer::Used) |
| 647 | addAsmGlobalSymbolUndef(Key.data()); |
| 648 | } |
| 649 | return false; |
| 650 | } |
| 651 | |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 652 | static bool isDeclaration(const GlobalValue &V) { |
| 653 | if (V.hasAvailableExternallyLinkage()) |
| 654 | return true; |
| 655 | if (V.isMaterializable()) |
| 656 | return false; |
| 657 | return V.isDeclaration(); |
| 658 | } |
| 659 | |
| 660 | static bool isAliasToDeclaration(const GlobalAlias &V) { |
| 661 | return isDeclaration(*V.getAliasedGlobal()); |
| 662 | } |
| 663 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 664 | bool LTOModule::ParseSymbols() { |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 665 | // Use mangler to add GlobalPrefix to names to match linker names. |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame^] | 666 | MCContext Context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), |
| 667 | NULL); |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 668 | Mangler mangler(Context, *_target->getTargetData()); |
Gabor Greif | 4136e7b | 2009-09-23 02:46:12 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 670 | // add functions |
| 671 | for (Module::iterator f = _module->begin(); f != _module->end(); ++f) { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 672 | if (isDeclaration(*f)) |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 673 | addPotentialUndefinedSymbol(f, mangler); |
| 674 | else |
| 675 | addDefinedFunctionSymbol(f, mangler); |
| 676 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 677 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 678 | // add data |
| 679 | for (Module::global_iterator v = _module->global_begin(), |
| 680 | e = _module->global_end(); v != e; ++v) { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 681 | if (isDeclaration(*v)) |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 682 | addPotentialUndefinedSymbol(v, mangler); |
| 683 | else |
| 684 | addDefinedDataSymbol(v, mangler); |
| 685 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 686 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 687 | // add asm globals |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 688 | if (addAsmGlobalSymbols(Context)) |
| 689 | return true; |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 690 | |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 691 | // add aliases |
| 692 | for (Module::alias_iterator i = _module->alias_begin(), |
| 693 | e = _module->alias_end(); i != e; ++i) { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 694 | if (isAliasToDeclaration(*i)) |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 695 | addPotentialUndefinedSymbol(i, mangler); |
| 696 | else |
| 697 | addDefinedDataSymbol(i, mangler); |
| 698 | } |
| 699 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 700 | // make symbols for all undefines |
| 701 | for (StringMap<NameAndAttributes>::iterator it=_undefines.begin(); |
| 702 | it != _undefines.end(); ++it) { |
| 703 | // if this symbol also has a definition, then don't make an undefine |
| 704 | // because it is a tentative definition |
| 705 | if (_defines.count(it->getKey()) == 0) { |
| 706 | NameAndAttributes info = it->getValue(); |
| 707 | _symbols.push_back(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 710 | return false; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 714 | uint32_t LTOModule::getSymbolCount() { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 715 | return _symbols.size(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 719 | lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 720 | if (index < _symbols.size()) |
| 721 | return _symbols[index].attributes; |
| 722 | else |
| 723 | return lto_symbol_attributes(0); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 726 | const char *LTOModule::getSymbolName(uint32_t index) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 727 | if (index < _symbols.size()) |
| 728 | return _symbols[index].name; |
| 729 | else |
| 730 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 731 | } |