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