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" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 29 | #include "llvm/Target/Mangler.h" |
Bill Wendling | 604a818 | 2008-06-18 06:35:30 +0000 | [diff] [blame] | 30 | #include "llvm/Target/SubtargetFeature.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | ff9834a | 2009-07-16 02:41:19 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Target/TargetRegistry.h" |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetSelect.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 36 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 39 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { |
| 40 | return llvm::sys::IdentifyFileType((char*)mem, length) |
| 41 | == llvm::sys::Bitcode_FileType; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 44 | bool LTOModule::isBitcodeFile(const char *path) { |
| 45 | return llvm::sys::Path(path).isBitcodeFile(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 48 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, |
| 49 | const char *triplePrefix) { |
| 50 | MemoryBuffer *buffer = makeBuffer(mem, length); |
| 51 | if (!buffer) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 52 | return false; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 53 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 56 | |
| 57 | bool LTOModule::isBitcodeFileForTarget(const char *path, |
| 58 | const char *triplePrefix) { |
| 59 | MemoryBuffer *buffer = MemoryBuffer::getFile(path); |
| 60 | if (buffer == NULL) |
| 61 | return false; |
| 62 | return isTargetMatch(buffer, triplePrefix); |
| 63 | } |
| 64 | |
| 65 | // Takes ownership of buffer. |
| 66 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { |
Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 67 | std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); |
| 68 | delete buffer; |
| 69 | return (strncmp(Triple.c_str(), triplePrefix, |
| 70 | strlen(triplePrefix)) == 0); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | LTOModule::LTOModule(Module *m, TargetMachine *t) |
| 75 | : _module(m), _target(t), _symbolsParsed(false) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 76 | { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | LTOModule *LTOModule::makeLTOModule(const char *path, |
| 80 | std::string &errMsg) { |
| 81 | OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, &errMsg)); |
| 82 | if (!buffer) |
| 83 | return NULL; |
| 84 | return makeLTOModule(buffer.get(), errMsg); |
| 85 | } |
| 86 | |
| 87 | /// makeBuffer - Create a MemoryBuffer from a memory range. MemoryBuffer |
| 88 | /// requires the byte past end of the buffer to be a zero. We might get lucky |
| 89 | /// and already be that way, otherwise make a copy. Also if next byte is on a |
| 90 | /// different page, don't assume it is readable. |
| 91 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { |
| 92 | const char *startPtr = (char*)mem; |
| 93 | const char *endPtr = startPtr+length; |
| 94 | if (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0 || |
| 95 | *endPtr != 0) |
| 96 | return MemoryBuffer::getMemBufferCopy(StringRef(startPtr, length)); |
| 97 | |
| 98 | return MemoryBuffer::getMemBuffer(StringRef(startPtr, length)); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, |
| 103 | std::string &errMsg) { |
| 104 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
| 105 | if (!buffer) |
| 106 | return NULL; |
| 107 | return makeLTOModule(buffer.get(), errMsg); |
| 108 | } |
| 109 | |
| 110 | LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, |
| 111 | std::string &errMsg) { |
| 112 | InitializeAllTargets(); |
| 113 | |
| 114 | // parse bitcode buffer |
| 115 | OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg)); |
| 116 | if (!m) |
| 117 | return NULL; |
| 118 | |
| 119 | std::string Triple = m->getTargetTriple(); |
| 120 | if (Triple.empty()) |
| 121 | Triple = sys::getHostTriple(); |
| 122 | |
| 123 | // find machine architecture for this module |
| 124 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
| 125 | if (!march) |
| 126 | return NULL; |
| 127 | |
| 128 | // construct LTModule, hand over ownership of module and target |
| 129 | SubtargetFeatures Features; |
| 130 | Features.getDefaultSubtargetFeatures("" /* cpu */, llvm::Triple(Triple)); |
| 131 | std::string FeatureStr = Features.getString(); |
| 132 | TargetMachine *target = march->createTargetMachine(Triple, FeatureStr); |
| 133 | return new LTOModule(m.take(), target); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | const char *LTOModule::getTargetTriple() { |
| 138 | return _module->getTargetTriple().c_str(); |
| 139 | } |
| 140 | |
| 141 | void LTOModule::setTargetTriple(const char *triple) { |
| 142 | _module->setTargetTriple(triple); |
| 143 | } |
| 144 | |
| 145 | void LTOModule::addDefinedFunctionSymbol(Function *f, Mangler &mangler) { |
| 146 | // add to list of defined symbols |
| 147 | addDefinedSymbol(f, mangler, true); |
| 148 | |
| 149 | // add external symbols referenced by this function. |
| 150 | for (Function::iterator b = f->begin(); b != f->end(); ++b) { |
| 151 | for (BasicBlock::iterator i = b->begin(); i != b->end(); ++i) { |
| 152 | for (unsigned count = 0, total = i->getNumOperands(); |
| 153 | count != total; ++count) { |
| 154 | findExternalRefs(i->getOperand(count), mangler); |
| 155 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 156 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 157 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 160 | // Get string that data pointer points to. |
| 161 | bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { |
| 162 | if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { |
| 163 | Constant *op = ce->getOperand(0); |
| 164 | if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { |
| 165 | Constant *cn = gvn->getInitializer(); |
| 166 | if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) { |
| 167 | if (ca->isCString()) { |
| 168 | name = ".objc_class_name_" + ca->getAsString(); |
| 169 | return true; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 170 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 171 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 172 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Parse i386/ppc ObjC class data structure. |
| 178 | void LTOModule::addObjCClass(GlobalVariable *clgv) { |
| 179 | if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 180 | // second slot in __OBJC,__class is pointer to superclass name |
| 181 | std::string superclassName; |
| 182 | if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { |
| 183 | NameAndAttributes info; |
| 184 | if (_undefines.find(superclassName.c_str()) == _undefines.end()) { |
| 185 | const char *symbolName = ::strdup(superclassName.c_str()); |
Daniel Dunbar | 8d0843d | 2010-08-11 00:11:17 +0000 | [diff] [blame] | 186 | info.name = symbolName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 187 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 188 | // string is owned by _undefines |
| 189 | _undefines[info.name] = info; |
| 190 | } |
| 191 | } |
| 192 | // third slot in __OBJC,__class is pointer to class name |
| 193 | std::string className; |
| 194 | if (objcClassNameFromExpression(c->getOperand(2), className)) { |
| 195 | const char *symbolName = ::strdup(className.c_str()); |
| 196 | NameAndAttributes info; |
| 197 | info.name = symbolName; |
| 198 | info.attributes = (lto_symbol_attributes) |
| 199 | (LTO_SYMBOL_PERMISSIONS_DATA | |
| 200 | LTO_SYMBOL_DEFINITION_REGULAR | |
| 201 | LTO_SYMBOL_SCOPE_DEFAULT); |
| 202 | _symbols.push_back(info); |
| 203 | _defines[info.name] = 1; |
| 204 | } |
| 205 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 209 | // Parse i386/ppc ObjC category data structure. |
| 210 | void LTOModule::addObjCCategory(GlobalVariable *clgv) { |
| 211 | if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) { |
| 212 | // second slot in __OBJC,__category is pointer to target class name |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 213 | std::string targetclassName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 214 | if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) { |
| 215 | NameAndAttributes info; |
| 216 | if (_undefines.find(targetclassName.c_str()) == _undefines.end()) { |
| 217 | const char *symbolName = ::strdup(targetclassName.c_str()); |
Daniel Dunbar | 8d0843d | 2010-08-11 00:11:17 +0000 | [diff] [blame] | 218 | info.name = symbolName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 219 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 220 | // string is owned by _undefines |
| 221 | _undefines[info.name] = info; |
| 222 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 223 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 224 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 228 | // Parse i386/ppc ObjC class list data structure. |
| 229 | void LTOModule::addObjCClassRef(GlobalVariable *clgv) { |
| 230 | std::string targetclassName; |
| 231 | if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) { |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 232 | NameAndAttributes info; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 233 | if (_undefines.find(targetclassName.c_str()) == _undefines.end()) { |
| 234 | const char *symbolName = ::strdup(targetclassName.c_str()); |
Daniel Dunbar | 8d0843d | 2010-08-11 00:11:17 +0000 | [diff] [blame] | 235 | info.name = symbolName; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 236 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 237 | // string is owned by _undefines |
| 238 | _undefines[info.name] = info; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | void LTOModule::addDefinedDataSymbol(GlobalValue *v, Mangler &mangler) { |
| 245 | // Add to list of defined symbols. |
| 246 | addDefinedSymbol(v, mangler, false); |
| 247 | |
| 248 | // Special case i386/ppc ObjC data structures in magic sections: |
| 249 | // The issue is that the old ObjC object format did some strange |
| 250 | // contortions to avoid real linker symbols. For instance, the |
| 251 | // ObjC class data structure is allocated statically in the executable |
| 252 | // that defines that class. That data structures contains a pointer to |
| 253 | // its superclass. But instead of just initializing that part of the |
| 254 | // struct to the address of its superclass, and letting the static and |
| 255 | // dynamic linkers do the rest, the runtime works by having that field |
| 256 | // instead point to a C-string that is the name of the superclass. |
| 257 | // At runtime the objc initialization updates that pointer and sets |
| 258 | // it to point to the actual super class. As far as the linker |
| 259 | // knows it is just a pointer to a string. But then someone wanted the |
| 260 | // linker to issue errors at build time if the superclass was not found. |
| 261 | // So they figured out a way in mach-o object format to use an absolute |
| 262 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 263 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 264 | // a class was missing. |
| 265 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 266 | // from the ObjC data structures generated by the front end. |
| 267 | if (v->hasSection() /* && isTargetDarwin */) { |
| 268 | // special case if this data blob is an ObjC class definition |
| 269 | if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) { |
| 270 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 271 | addObjCClass(gv); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // special case if this data blob is an ObjC category definition |
| 276 | else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) { |
| 277 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 278 | addObjCCategory(gv); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // special case if this data blob is the list of referenced classes |
| 283 | else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) { |
| 284 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 285 | addObjCClassRef(gv); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // add external symbols referenced by this data. |
| 291 | for (unsigned count = 0, total = v->getNumOperands(); |
| 292 | count != total; ++count) { |
| 293 | findExternalRefs(v->getOperand(count), mangler); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | void LTOModule::addDefinedSymbol(GlobalValue *def, Mangler &mangler, |
| 299 | bool isFunction) { |
| 300 | // ignore all llvm.* symbols |
| 301 | if (def->getName().startswith("llvm.")) |
| 302 | return; |
| 303 | |
| 304 | // string is owned by _defines |
| 305 | const char *symbolName = ::strdup(mangler.getNameWithPrefix(def).c_str()); |
| 306 | |
| 307 | // set alignment part log2() can have rounding errors |
| 308 | uint32_t align = def->getAlignment(); |
| 309 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
| 310 | |
| 311 | // set permissions part |
| 312 | if (isFunction) |
| 313 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
| 314 | else { |
| 315 | GlobalVariable *gv = dyn_cast<GlobalVariable>(def); |
| 316 | if (gv && gv->isConstant()) |
| 317 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 318 | else |
| 319 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 320 | } |
| 321 | |
| 322 | // set definition part |
Bill Wendling | 563ef5e | 2010-09-27 18:05:19 +0000 | [diff] [blame] | 323 | if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() || |
| 324 | def->hasLinkerPrivateWeakLinkage() || |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 325 | def->hasLinkerPrivateWeakDefAutoLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 326 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 327 | else if (def->hasCommonLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 328 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 329 | else |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 330 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 331 | |
| 332 | // set scope part |
| 333 | if (def->hasHiddenVisibility()) |
| 334 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 335 | else if (def->hasProtectedVisibility()) |
| 336 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 337 | else if (def->hasExternalLinkage() || def->hasWeakLinkage() || |
| 338 | def->hasLinkOnceLinkage() || def->hasCommonLinkage() || |
| 339 | def->hasLinkerPrivateWeakLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 340 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 341 | else if (def->hasLinkerPrivateWeakDefAutoLinkage()) |
| 342 | attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 343 | else |
| 344 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 345 | |
| 346 | // add to table of symbols |
| 347 | NameAndAttributes info; |
| 348 | info.name = symbolName; |
| 349 | info.attributes = (lto_symbol_attributes)attr; |
| 350 | _symbols.push_back(info); |
| 351 | _defines[info.name] = 1; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 354 | void LTOModule::addAsmGlobalSymbol(const char *name) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 355 | // only add new define if not already defined |
Daniel Dunbar | f4452c3 | 2010-08-11 00:11:19 +0000 | [diff] [blame] | 356 | if (_defines.count(name)) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 357 | return; |
| 358 | |
| 359 | // string is owned by _defines |
| 360 | const char *symbolName = ::strdup(name); |
| 361 | uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR; |
| 362 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 363 | NameAndAttributes info; |
| 364 | info.name = symbolName; |
| 365 | info.attributes = (lto_symbol_attributes)attr; |
| 366 | _symbols.push_back(info); |
| 367 | _defines[info.name] = 1; |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 368 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 369 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 370 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl, |
| 371 | Mangler &mangler) { |
| 372 | // ignore all llvm.* symbols |
| 373 | if (decl->getName().startswith("llvm.")) |
| 374 | return; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 376 | // ignore all aliases |
| 377 | if (isa<GlobalAlias>(decl)) |
| 378 | return; |
Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 379 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 380 | std::string name = mangler.getNameWithPrefix(decl); |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 382 | // we already have the symbol |
| 383 | if (_undefines.find(name) != _undefines.end()) |
| 384 | return; |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 385 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 386 | NameAndAttributes info; |
| 387 | // string is owned by _undefines |
| 388 | info.name = ::strdup(name.c_str()); |
| 389 | if (decl->hasExternalWeakLinkage()) |
| 390 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 391 | else |
| 392 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
| 393 | _undefines[name] = info; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
| 397 | |
Nick Lewycky | d42b58b | 2009-07-26 22:16:39 +0000 | [diff] [blame] | 398 | // Find external symbols referenced by VALUE. This is a recursive function. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 399 | void LTOModule::findExternalRefs(Value *value, Mangler &mangler) { |
| 400 | if (GlobalValue *gv = dyn_cast<GlobalValue>(value)) { |
| 401 | if (!gv->hasExternalLinkage()) |
| 402 | addPotentialUndefinedSymbol(gv, mangler); |
| 403 | // If this is a variable definition, do not recursively process |
| 404 | // initializer. It might contain a reference to this variable |
| 405 | // and cause an infinite loop. The initializer will be |
| 406 | // processed in addDefinedDataSymbol(). |
| 407 | return; |
| 408 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 409 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 410 | // GlobalValue, even with InternalLinkage type, may have operands with |
| 411 | // ExternalLinkage type. Do not ignore these operands. |
| 412 | if (Constant *c = dyn_cast<Constant>(value)) { |
| 413 | // Handle ConstantExpr, ConstantStruct, ConstantArry etc. |
| 414 | for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i) |
| 415 | findExternalRefs(c->getOperand(i), mangler); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void LTOModule::lazyParseSymbols() { |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 420 | if (_symbolsParsed) |
| 421 | return; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 423 | _symbolsParsed = true; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 424 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 425 | // Use mangler to add GlobalPrefix to names to match linker names. |
| 426 | MCContext Context(*_target->getMCAsmInfo()); |
| 427 | Mangler mangler(Context, *_target->getTargetData()); |
Gabor Greif | 4136e7b | 2009-09-23 02:46:12 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 429 | // add functions |
| 430 | for (Module::iterator f = _module->begin(); f != _module->end(); ++f) { |
| 431 | if (f->isDeclaration()) |
| 432 | addPotentialUndefinedSymbol(f, mangler); |
| 433 | else |
| 434 | addDefinedFunctionSymbol(f, mangler); |
| 435 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 436 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 437 | // add data |
| 438 | for (Module::global_iterator v = _module->global_begin(), |
| 439 | e = _module->global_end(); v != e; ++v) { |
| 440 | if (v->isDeclaration()) |
| 441 | addPotentialUndefinedSymbol(v, mangler); |
| 442 | else |
| 443 | addDefinedDataSymbol(v, mangler); |
| 444 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 445 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 446 | // add asm globals |
| 447 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
| 448 | const std::string glbl = ".globl"; |
| 449 | std::string asmSymbolName; |
| 450 | std::string::size_type pos = inlineAsm.find(glbl, 0); |
| 451 | while (pos != std::string::npos) { |
| 452 | // eat .globl |
| 453 | pos = pos + 6; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 454 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 455 | // skip white space between .globl and symbol name |
| 456 | std::string::size_type pbegin = inlineAsm.find_first_not_of(' ', pos); |
| 457 | if (pbegin == std::string::npos) |
| 458 | break; |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 459 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 460 | // find end-of-line |
| 461 | std::string::size_type pend = inlineAsm.find_first_of('\n', pbegin); |
| 462 | if (pend == std::string::npos) |
| 463 | break; |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 464 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 465 | asmSymbolName.assign(inlineAsm, pbegin, pend - pbegin); |
| 466 | addAsmGlobalSymbol(asmSymbolName.c_str()); |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 467 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 468 | // search next .globl |
| 469 | pos = inlineAsm.find(glbl, pend); |
| 470 | } |
| 471 | |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 472 | // add aliases |
| 473 | for (Module::alias_iterator i = _module->alias_begin(), |
| 474 | e = _module->alias_end(); i != e; ++i) { |
| 475 | if (i->isDeclaration()) |
| 476 | addPotentialUndefinedSymbol(i, mangler); |
| 477 | else |
| 478 | addDefinedDataSymbol(i, mangler); |
| 479 | } |
| 480 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 481 | // make symbols for all undefines |
| 482 | for (StringMap<NameAndAttributes>::iterator it=_undefines.begin(); |
| 483 | it != _undefines.end(); ++it) { |
| 484 | // if this symbol also has a definition, then don't make an undefine |
| 485 | // because it is a tentative definition |
| 486 | if (_defines.count(it->getKey()) == 0) { |
| 487 | NameAndAttributes info = it->getValue(); |
| 488 | _symbols.push_back(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 494 | uint32_t LTOModule::getSymbolCount() { |
| 495 | lazyParseSymbols(); |
| 496 | return _symbols.size(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 500 | lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) { |
| 501 | lazyParseSymbols(); |
| 502 | if (index < _symbols.size()) |
| 503 | return _symbols[index].attributes; |
| 504 | else |
| 505 | return lto_symbol_attributes(0); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 508 | const char *LTOModule::getSymbolName(uint32_t index) { |
| 509 | lazyParseSymbols(); |
| 510 | if (index < _symbols.size()) |
| 511 | return _symbols[index].name; |
| 512 | else |
| 513 | return NULL; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 514 | } |