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