Protect ChangeCompareStride from situations in which it is possible
[oota-llvm.git] / tools / lto2 / LTOModule.cpp
index 2fd29be272a609aa7619def6109871df78495945..a68fa6c226201a7cad998b77e01bbc9b7e573a3c 100644 (file)
@@ -97,13 +97,13 @@ LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg)
 /// Also if next byte is on a different page, don't assume it is readable.
 MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
 {
-       const char* startPtr = (char*)mem;
-       const char* endPtr = startPtr+length;
-       if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0) 
-           || (*endPtr != 0) ) 
-               return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
-       else
-               return MemoryBuffer::getMemBuffer(startPtr, endPtr);
+    const char* startPtr = (char*)mem;
+    const char* endPtr = startPtr+length;
+    if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0) 
+        || (*endPtr != 0) ) 
+        return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
+    else
+        return MemoryBuffer::getMemBuffer(startPtr, endPtr);
 }
 
 
@@ -161,7 +161,7 @@ void LTOModule::addDefinedDataSymbol(GlobalValue* v, Mangler &mangler)
     addDefinedSymbol(v, mangler, false); 
 
     // add external symbols referenced by this data.
-    for (unsigned count = 0, total = v->getNumOperands();\
+    for (unsigned count = 0, total = v->getNumOperands();
                                                 count != total; ++count) {
         findExternalRefs(v->getOperand(count), mangler);
     }
@@ -191,17 +191,7 @@ void LTOModule::addDefinedSymbol(GlobalValue* def, Mangler &mangler,
     
     // set definition part 
     if ( def->hasWeakLinkage() || def->hasLinkOnceLinkage() ) {
-        // lvm bitcode does not differenciate between weak def data 
-        // and tentative definitions!
-        // HACK HACK HACK
-        // C++ does not use tentative definitions, but does use weak symbols
-        // so guess that anything that looks like a C++ symbol is weak and others
-        // are tentative definitions
-        if ( (strncmp(symbolName, "__Z", 3) == 0) )
-            attr |= LTO_SYMBOL_DEFINITION_WEAK;
-        else {
-            attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
-        }
+        attr |= LTO_SYMBOL_DEFINITION_WEAK;
     }
     else if ( def->hasCommonLinkage()) {
         attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
@@ -244,8 +234,13 @@ void LTOModule::findExternalRefs(Value* value, Mangler &mangler) {
     if (GlobalValue* gv = dyn_cast<GlobalValue>(value)) {
         if ( !gv->hasExternalLinkage() )
             addPotentialUndefinedSymbol(gv, mangler);
+        // If this is a variable definition, do not recursively process
+        // initializer.  It might contain a reference to this variable
+        // and cause an infinite loop.  The initializer will be
+        // processed in addDefinedDataSymbol(). 
+        return;
     }
-
+    
     // GlobalValue, even with InternalLinkage type, may have operands with 
     // ExternalLinkage type. Do not ignore these operands.
     if (Constant* c = dyn_cast<Constant>(value)) {