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