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